functionfoo () {}foo.prototype.z= 3;varobj =Newfoo (); Obj.y= 2; obj.x= 1; obj.x;//1OBJ.Y;//2Obj.z;//3typeofobj.tostring;//' function '' Z 'inchObj//trueObj.hasownproperty (' z ');//false
First, we define a function object by using function foo (). Then, the Foo () object automatically has the prototype object property, and the z attribute is added. With the new constructor, the Obj object is constructed, and the prototype of obj points to the prototype property of the constructor, which is Foo.prototype.
See example: obj.x and OBJ.Y will find the X, Y property on the Obj object, and obj has no z attribute, and when obj.z, there is no z on obj, then the properties on the prototype are found and z=3 is discovered.
The prototype of Foo.prototype is the Object.prototype,object.prototype prototype is null. The role of Object.prototype is:, typeof obj.tostring is a ' function ', and the ToString method is not found on IBJ and its prototype, and the default object on JS has the ToString method, because their prototype points to object.prototype before they point to null. ToString is the method on Object.prototype.
obj. hasownproperty (' Z ') returns false, indicating that the Z property is not a property directly for obj.
obj.z = 5;obj.hasownproperty ( ' z '); // true foo.prototype.z; // still 3 obj.z; // 5 obj.z = undefined;obj.z; // undefined delete obj.z; // true obj.z; // 3 delete obj.z; // true obj.z; // still 3!!!
As written in the code, OBJ.Z = 5; for assignment, find on the Obj object first, if not, not in the prototype chain, but add z=5;foo.prototype.z on the object is still 3.
When obj.z=undefined, the value of the Z property on the Obj object is modified to undefined, so returning undefined does not necessarily mean that it does not exist.
When delete obj.z, the value of z is the value of Z on the prototype. The z attribute on obj has been deleted.
The Enumerable (enumerable) label of most properties on the prototype chain is false, which means that only the object is displayed when Console.log an object, and many properties are visible in the console
Property tags are: value:true,writeable:true (writable), Enumerable:true (enumerable), Configurable:true (indicates whether the preceding properties can be modified)
JavaScript prototype chain Learning