javascript inheritance problem
迷茫
迷茫 2017-07-05 10:47:40
0
3
738

code show as below:

        function Father(){
            this.name = true;
            this.array = [];
        }
        Father.prototype.getFatherValue = function(){
            return this.property;
        }
        function Son(){
            this.sonProperty = false;
        }
        //继承 Father
        Son.prototype = new Father();
        var son1 = new Son();
        var son2 = new Son();

The array attribute of Father will be shared by son1 and son2, but the name attribute will not be shared. My understanding is that both son1 and son2 will go to Son.prototype to find the name attribute. The name attribute should also be shared. Why? No?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
为情所困

Because arrays are reference types, for these three instances, father(new Father()), son1, son2, they array all save references to []this array, so only one of them If it is modified, it will be modified by following the reference to find the array in the memory. What is modified is the same array. And name = true, this name is a basic type. When distinguishing new instances, an area will be opened in the memory to store its value. Therefore, the name of the above three instances correspond to different memories. The value of the area, so modifications will not affect each other.
After looking at some of the answers above, I thought about it and found that my understanding and my answer were wrong. Keep the original answer and correct it below.
There is no problem about the array. The problem is the name attribute. For son1 and son2, they do not have the attribute name, so when new, there should be no name for them. Open up memory space. Only the instance father has it. The
name values ​​of son1 and son2 are found through the prototype chain search. If son1.name is assigned a value, it is equivalent to adding the name attribute to the son1 instance. Of course, when printing son1.name again The value obtained is the name value belonging to son1, and when printing son2.name, it will go to the prototype chain to find name. At this time, what is found is the name value of Father, so there are two values Different, may give you the illusion that there is no sharing.
It is worth noting that if son1.array[0] = 1If the assignment is like this, it will have an impact on the arrays of the three instances. If it is son1.array = [1], if the assignment is like this, it will not. Because at this time, array retains a reference to the new array memory address [1].

学霸
son1.array.push(1)
son1.array // [1]
son2.array // [2]

son1.array = [2]
son1.array // [2]
son2.array // [1]

You said the array is shared?

小葫芦

Why does it say that the name attribute will not be shared?

Prototype chain inheritance means searching along the prototype chain until it finds it and returns this value. If it cannot find it, it returns undefined.

If we assign a value to Son

son1.name = 'aa';
son2.name = 'bbb';

At this point, the value of the instance is taken. Only when Son does not have corresponding attributes, it will be searched in the prototype chain.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template