The question of generating instance object by js constructor

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.


as far as I understand it, functions compare pointers in memory, while primitive types only compare values.


< H2 > C < / H2 >
 

the upstairs is right, the comparison of basic types is only compared to individual values, and the string is constant in js. I am not particularly sure whether only one constant of the same value will be stored. What I want to say is that if two variables refer to two strings with equal values, then the two strings may indeed be the same string, and they occupy the same storage space.

use functions to construct objects, and dynamically add class methods to the constructor. Each execution of the constructor generates anonymous functions as class methods, so every time new comes out of an anonymous function with the same name that occupies different memory space. When comparing them with = , you are actually comparing their address, or "whether it is the same object". When comparing the basic types of data such as strings and numbers with = , you compare their values. Otherwise, how do you judge that two numbers are equal?

Menu