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

Source: Internet
Author: User
Tags shallow copy

Introduction to Prototype mode (prototype) refers to the kind of objects that are created with prototype instances, and creates new objects by copying them. Text for prototype mode, we can take advantage of JavaScript-specific prototype inheritance features to create objects, that is, the creation of an object as another object's prototype property values. 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. True prototype inheritance was 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 capitalizevarSomecar ={drive:function() {}, Name:' Mazda 3 '};//create a new car with object.create xvarAnothercar =object.create (Somecar); Anothercar.name= ' Toyota Camry '; 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 (); Summary prototype mode in JavaScript use 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, lest there is a citation problem. Synchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing. 

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.