[Note] prototype and prototype of javascript prototype

Source: Internet
Author: User
Tags hasownproperty

[Note] prototype and prototype of javascript prototype
In javascript, a function itself is an object that contains methods and attributes.

Length returns the number of parameters.

In Chapter 4th, we have learned how to define constructors and use them to create (construct) objects. The main purpose of this approach is to call a function through the new operator to access the object's this value. Then, the constructor can return the object it created to us. In this way, we have a method that gives new objects certain functions (that is, adding attributes and methods to them.

Use the prototype to add methods and attributes: function Gadget (name, color) {this. name = name; this. color = color; this. wharAreYou = function () {return 'I am a' + this. color + ''+ this. name ;}}
/* Add attributes and methods through prototype */Gadget. prototype. price = 100; Gadget. prototype. rating = 3; Gadget. prototype. getInfo = function () {return 'rating: '+ this. rating + ', price:' + this. price ;};
/* In addition to the above method, you can also define an object and overwrite it to the previous prototype */Gadget. prototype = {price2: 100, rating2: 3, getInfo: function () {return 'rating2: '+ this. rating2 + ', price2:' + this. price2 ;}};

/* Use the prototype method and attributes */var newtoy = new Gadget ('webcam ', 'black ');

According to the running results, when the following methods coexist with the previous ones, the previous prototype will be completely overwritten. For prototype, we need to understand its concept of "live. In javascript, objects are transmitted by reference, because each new object entity we create does not have its own prototype copy. We can modify the prototype at any time, and all objects related to it will inherit this change. It may even affect the creation of objects before modification.
However, if the object is added one by one, it will not be completely overwritten. But partially covered:


Enumerated properties: newtoy. hasOwnProperty ('price') falsenewtoy. hasOwnProperty ('name') truenewtoy. constructor. prototype. propertyIsEnumerable ('price'); false // true
Why is there a discrepancy with that in the book?


Each object has an isPrototypeOf () method, which tells us whether the current object is a prototype of another object.
Var monkey = {hair: true, feeds: 'banas', breathes: 'air '};
Function Human (name) {this. name = name ;}
Human. prototype = monkey;
Var george = new Human ('George '); monkey. isPrototypeOf (George );
-> True

The mysterious _ proto ____ proto _ is actually the attribute of an object, while prototype is the attribute of the constructor function. Remember, __proto _ can only be used in a learning or debugging environment.








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.