In-depth understanding of the JavaScript series (42): Prototyping patterns for design patterns

Source: Internet
Author: User
Tags shallow copy

Introduced

Prototype mode (prototype) refers to the kind of object that is created with a prototype instance, and creates a new object by copying the prototypes.

Body

For prototype mode, we can take advantage of the JavaScript-specific prototype inheritance feature to create an object, that is, an object created as a prototype property value for another object. The prototype object itself is an efficient use of objects created by each constructor, for example, if a constructor's prototype contains a name attribute (see the following example), the object created by the constructor will have this property.

Looking at the definition of prototype pattern in the existing literature, there is no JavaScript, you may find that many of the explanations are about classes, but the reality is that the prototype-based JavaScript completely avoids the concept of class. We are simply copying from an existing object to create the object.

Real prototype inheritance is proposed as the latest version of the ECMASCRIPT5 standard, Use the Object.create method to create an object that creates a specified object whose object's prototype has the specified object (that is, the first parameter object that the method passes into), or it can contain other optional specified properties. For example Object.create (prototype, optionaldescriptorobjects), this usage can also be seen in the following example:

//  because it is not a constructor, you do not have to capitalize    var  somecar = {drive: function  () {}, Name: ' Mazda 3 '};

Object.create run you inherit directly from other objects, using the second parameter of the method, you can initialize additional properties. For example:

varVehicle = {Getmodel:function() {console.log (' The mold of the vehicle is: ' + This. model); }};varCar = object.create (vehicle, {' id ': {value:MY_GLOBAL.nextId (), Enumerable:true //default Writable:false, Configurable:false}, ' model ': {value: ' Ford ', Enumerable:true}});

Here, you can use object literals in the second argument of Object.create to pass in the additional properties to initialize, with the syntax of the Object.defineproperties or Object.defineproperty method type. It allows you to set properties such as enumerable, writable, or configurable.

If you want to implement prototype mode yourself, instead of using object.create directly. You can use code like the following to achieve the above example:

varVehicleprototype = {init:function(Carmodel) { This. model = Carmodel; }, Getmodel:function() {console.log (' vehicle mould is: ' + This. model); }};functionVehicle (model) {functionF () {}; F.prototype = Vehicleprototype;varf =NewF (); F.init (model);returnF;}varCar = vehicle (' ford Escort '); Car.getmodel ();
Summarize

Prototype mode in JavaScript is almost everywhere, many other models are also based on prototype, not much to say, here is still a shallow copy and deep copy of the problem, so as to avoid the citation problem.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

In-depth understanding of the JavaScript series (42): Prototyping patterns for design patterns

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.