It is my first question here, so apologise if I missed anything. Perhaps solution for my problem could be find, but I have no idea how to search for it, no clues about how to ask it in google or something.
I have a following problem, let's have such constructor
const Product = function (color) {
let self = {};
self.setColor = function(colorToSet) {
color = colorToSet;
}
self.getColor = function() {
return color;
}
return self;
};
now when I tried to use:
let self = {}, color;
in chrome console I received error that color has been already declared, so I removed color field and after this (with a snippet code above), magic happened that I cannot explain.
let's say I will write something like this:
let a = Product("Yello");
a.getColor() ----> "Yellow"
a.setColor("red");
a.getColor() ----> "red";
If it returns color for the first time, then it has to be declared somehow. I do not know where is color field, I cannot find it in the object nor in its prototype, nowhere actually. Could you explain me why ? and where it is ?
I know that I can just declare color for example: self.color = color; But I want to know how is working the example above and what happened with color field.
Also could you tell me if I can use let with declaring values from parameters in such way ?
const Product = function (color) {
let self = {}, color;
self.setColor = function(colorToSet) {
color = colorToSet;
}
self.getColor = function() {
return color;
}
return self;
};