"Notes" JavaScript prototype properties prototype

Source: Internet
Author: User

In JavaScript, the function itself is also an object that contains methods and properties.

Length returns the number of arguments.

In the 4th chapter, we have learned how to define a constructor function and use it to create a new (constructed) object. The main intent of this approach is to invoke the function with the new operator to achieve the purpose of accessing the object's this value, and then the constructor can return the object it created to us. In this way, we have a way to give new objects a certain functionality (that is, adding properties and methods to them).

add methods and properties using prototypes:function Gadget (name,color) {this.name = name; this.color = color; this.wharareyou = function () {return ' I am a ' + this. Color + ' + this.name; }}
/* Add properties and methods via 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 methods, you can also define an object and then overwrite it on the previous prototype */gadget.prototype = {price2:100, rating2:3, Getinfo:function () {return ' Rating2: ' + This.rating2 + ', Price2: ' + this.price2; }};

/* Methods and properties for using prototypes */var newtoy = new Gadget (' webcam ', ' black ');

From the results of the operation, the latter way and the previous method coexist, it will completely overwrite the previous prototype. The final thing for a prototype is that we want to understand its "dwell" (live) concept. Because in JavaScript, objects are passed by reference, because each new object entity that we create does not have a copy of its own prototype. We can modify the prototype at any time, and the objects associated with it will inherit this change. It may even affect the creation of objects before they are modified.
However, if the object is to be added one after the other, there is no case of full coverage. But partially covered:


Enumerable Properties:Newtoy.hasownproperty (' price ') falsenewtoy.hasownproperty (' name ') Truenewtoy.constructor.prototype.propertyIsEnumerable (' price '); false//True on the book
Why is there a discrepancy with the book?


Each object has a isprototypeof () method that tells us whether the current object is a prototype of another object.
var monkey = {hair:true, feeds: ' Bananas ', breathes: ' Air '};
function Human (name) {this.name = name;}
Human.prototype = Monkey;
var george = new Human (' George '); monkey.isprototypeof (George);
-True

Mysterious __proto__.__proto__ is actually a property of an entity object, and prototype is a property that belongs to the constructor function. Be sure to remember that __proto__ can only be used in the context of learning or debugging.








"Notes" JavaScript prototype properties prototype

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.