I js
novice, recently encountered a little question when learning the content of the object, as follows:
personal understanding: every time an instance object is generated using the new
command, all the properties in the constructor will be defined on the instance object, that is, the properties in any two instance objects are different and cannot be shared, but if that property is not the method , it still seems to be "the same", see code
var Cat = function() {
this.color = "red"
this.say = function() {
console.log("miao")
}
}
var c1 = new Cat()
var c2 = new Cat()
console.log(c1.color === c2.color) // true
console.log(c1.say === c2.say) // false
for say
this method is because the two instance object methods are different, which is why you use prototype
to inspire the method, but why the color
attribute shows the result as true
? The two instance objects, also as properties, should be different at the time of generation, but why is the result true
? I don"t really understand
I hope to have a senior to give me an answer. Thank you.