JS design mode-prototype mode and constructor __ function

Source: Internet
Author: User
JS design mode-prototype mode and constructor mode 1. Prototype ModeThe prototype pattern is called the pattern of creating an object from a template based on an existing object through cloning.
We can view the prototype pattern as based on prototype inheritance, in which we create objects as prototypes for other objects. The prototype object itself is effectively used as a blueprint for each object created by the constructor.

The true prototype inheritance defined in the ECMAScript 5 standard requires the use of object.create. To remind yourself, Object.create creates an object with the specified prototype and can also contain the specified attributes (such as object.create (prototype, optionaldescriptorobjects)).

var mycar = {
 
  name: "Ford Escort",
 
  drive:function () {
    console.log ("weeee. I ' m driving! ');
  },
 
  panic:function () {
    console.log ("Wait. How do you stop this thing? "}
 
};
 
Use Object.create to instantiate a new car
var yourcar = object.create (mycar);
 
Now we can have a prototype of the other
Console.log (yourcar.name);

Object.create also allows us to easily implement advanced concepts such as differential inheritance, where objects can inherit directly from other objects. As we saw before, Object.create allows us to initialize the object properties using the second supplied parameter. For example:

var vehicle = {
  getmodel:function () {
    Console.log ("The model of this vehicle is ..." + This.model);
  }
;
 
var car = object.create (vehicle, {
 
  "id": {
    value:MY_GLOBAL.nextId (),
    //Writable:false, configurable: False by default
    enumerable:true
  },
 
  "model": {
    value: "Ford",
    enumerable:true
  }
 
} );

If we want to not use the prototype pattern Object.create directly, we can follow the example above to simulate the following pattern:
var vehicleprototype = {
 
  init:function (carmodel) {
    This.model = Carmodel;
  },
 
  getmodel:function () {
  console.log ("The model of this vehicle is ..." + This.model);
  }
;
 
 
function vehicle (model) {
 
  function F () {};
  F.prototype = Vehicleprototype;
 
  var F = new F ();
 
  F.init (model);
  return f;
 
}
 
var car = vehicle ("Ford Escort");
Car.getmodel ();

The last alternative implementation of the prototype pattern might be as follows:
var beget = (function () {
 
    function F () {} return
 
    function (proto) {
        f.prototype = proto;
        return new F ();
    };
}) ();

2. Prototype mode and constructor function pattern
For more information about constructors, see JS prototype mode and constructor pattern

The two properties of the constructor pattern in the above diagram, the instance property and the common property, because the constructor pattern can only be called the two names if the prototype method is not used, so this is called both and names for compatibility prototype. As for the prototype model, it is a property of its own and inherited from the prototype chain.
First, the way in which the two objects are created is different, the prototype pattern must have a prototype existence, and the constructor method does not have to rely on prototype.
The second is to talk about their understanding of the two. It is as if there are many members of the team in the daily development, and there will be teams inside. All members are relative to the team, and that is the constructor pattern, where the team does not refer to an individual, but to a function, all of whom we work with. In the day-to-day development, the team for our limitations is not really, only when we are facing the whole freeze, we will mention our common attributes, a team. Most of the time we're still sitting on our own things (instance properties).
And then I have a leader, we're two, and now this leader, I'm the prototype, and now the prototype is leader, and I'm working in this group there will be some leader defined working methods (inherited properties), Of course, there are some ways to deal with their own problems (own attributes)
This example may not be appropriate, more understanding I think the two focus is different, although there seems to be no difference. The constructor pattern focuses on dealing with multiple objects, just to illustrate that they have something in common, but there are no restrictions on the individual. The focus of the prototype model should be to deal with fewer objects, requiring the close contact between the prototype object and the instance object, which will have more restrictions on the instance object.
Note: Instance objects can be overridden regardless of the common properties of the constructor pattern or the inherited properties of the prototype object.

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.