Thoughts on the original JavaScript model and objects

Source: Internet
Author: User

First, make sure that the instance cannot access the attributes and methods in the prototype object, not only because the prototype object is rewritten to cut off the relationship between the constructor and the original prototype. In JavaScript Zhongyuan and objects, I have mentioned how instances can access the attributes and methods of rewritten prototype objects.

Function person () {} var person = new person (); person. prototype = {// constructor: person, name: 'zxs ', age: 24, sayname: function () {alert (this. name)} person. sayname ();

The prototype of the constructor is empty when the above Code instantiates an object. It does not have any attributes other than the default attributes. The prototype of the rewrite constructor does cut off the relationship between the constructor and the original prototype.

Attributes and methods in the prototype object of the constructor after the new operator are added to the person object. Because the method above adds new attributes and methods for the function prototype is not dynamic, the person cannot access the newly added attributes and methods.

After rewriting the prototype object, it is like the following code:

VaR o = {Name: 'zxs '} var OBJ = O; O = {} console. Log (O. Name );

The output value is undefined, because the object is a reference type, "=" is a value assignment operator, and its operation order is from right to left. O ={} indicates that the direction of O has changed and is an empty object.

Person. prototype. mothed = function () {} and person. prototype = {mothed: function () {} is similar to ARR = [] and ARR. like push (), the former changes itself, and the latter completely changes itself.

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.