Javascript advanced-prototype, prototype

Source: Internet
Author: User

Javascript advanced-prototype, prototype

I. Understanding of javascript prototype

Many programming languages have the concept of classes. We can compare prototypes and classes to see the differences and similarities between them.

1. Class: class is the abstraction of a specific thing, so class is an abstract thing. In object-oriented mode, classes can be used to construct objects. This is an abstract-specific process. In real life, it is like drawing a car.

2. prototype: it is a specific-specific process to construct an object using prototype in javascript. In real life, it is like a certain brand of cars-other brands of cars.

 

Ii. Set the object prototype

1. Object. create (proto [, propertiesObject])

Proto an object as the prototype of the newly created object.

Instance:

// Create a prototype object car var car = {name: 'cart', start: function () {console. log (this. logo) ;}}// use the prototype Object to create a new Object var Bensz = Object. create (car); Bensz. logo = 'bensz '; // call the start method bensz with the new object Bensz. start ();

The running result of the above Code is printed 'bensz '.

2. Constructor

The constructor can use the prototype property to set the prototype and use new to create an object.

Instance:

// First, the constructor carfunction Car (logo) {this. logo = logo | 'unkown name';} // sets the prototype attribute of the car. This attribute is an object Car. prototype = {strat: function () {console. log (this. logo) ;}} var bensz = new Car ('bensz '); bensz. strat ();

The above code can also print 'bensz'

Graphic Process:

 

There are three steps to create a new object bensz with the new keyword. 1. Create a new object bensz. 2. Set bensz's _ proto _. This is the _ proto _ attribute pointing to car. prototype, 3, Car. apply (bensz, []), bensz performs the logo assignment operation on the Car, then the bensz object has the logo attribute.

 

Iii. prototype chain

Instance:

// First, the constructor carfunction Car (logo) {this. logo = logo | 'unkown name';} // sets the prototype attribute of the car. This attribute is an object Car. prototype = {strat: function () {console. log (this. logo) ;}}// create a constructor benszfunction Bensz (serialno) {this. serialno = serialno;} // set the prototype attribute of bensz to Bensz, an object of the car type. prototype = new Car ('bensz '); var bensz1 = new bensz (12345); // defines two constructors. The prototype of the second constructor is not a common Object, it is a Car type object.

Process: 1. Create a Car constructor and set its prototype attribute. 2. Create a Bensz constructor whose prototype attribute is a Car-type object. Because it is an object created by the new Keyword, this object has the _ proto _ attribute, this attribute points to the Car. prototype. 3. Create a bensz1 object because it is created by the new Keyword. It also has a _ proto _ attribute, which points to the prototype attribute of the constructor Bensz, namely Bensz. prototype.

In addition, we can find that Car. prototype can be created using new Object. Because it is an Object, it also has the _ proto _ attribute and points to Object. prototype.

So the entire prototype chain is: 1. bensz1. _ proto _ -- 2. Bensz. prototype (new Car ('bensz '), bensz. prototype. _ proto _ -- 3. Car. prototype, Car. prototype. _ proto _ -- 4, Object. prototype.

 

Access attribute: first find the object. If the object is not found along the prototype chain

Modify and delete attributes: Only attributes of objects can be modified or deleted.

 

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.