Answer the question
In order to leave comments, you need to log in
What is the functional difference between constructor and set in JavaScript classes? Why should a class description contain both?
And what is _dogName for, if there is just dogName?
class Dog
{
constructor(dogName)
{
this.dogName = dogName;
}
set dogName(value)
{
this._dogName = value;
}
get dogName()
{
return this._dogName;
}
}
let dog = new Dog("Rex");
console.log(dog.dogName);
Answer the question
In order to leave comments, you need to log in
Why should a class description contain both?Why do you think so? There may be neither one nor the other.
What is the functional difference between constructor and set in JavaScript classes?Setters are responsible for setting the value of one particular property; constructor - for the initialization of the object as a whole.
And what is _dogName for, if there is just dogName?A property object (_dogName) and a setter method (dogName) cannot have the same name.
class Dog
{
constructor(dogName)
{
this.dogName = dogName;
}
}
let dog = new Dog("Rex");
console.log(dog.dogName);
And what is _dogName for, if there is just dogName?
const blackList = ['Мурзик', 'Васька', 'Эпифантий'];
// ...
set dogName(value)
{
if (!value || !_.isString(value)) {
throw new Error('value must be a string!') ;
}
if (blackList.includes(value) {
throw new Error('wrong value!') ;
}
this._dogName = value;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question