JavaScript prototype chain Learning

Source: Internet
Author: User
Tags hasownproperty

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.