This is the 18th day of my participation in Gwen Challenge

Related articles:

  • The prototype
  • new
  • inheritance

The prototype pattern

A prototype pattern is called a pattern in which an object is created by cloning a template based on an existing object. We can think of the stereotype pattern as based on prototype inheritance, where 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.

True prototype inheritance, as defined in the ECMAScript 5 standard, requires the use of Object.create. To remind ourselves that the Object. The create create a Object with specified prototype, and can also contain the specified properties (such as the Object. The 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 see that one is a prototype of the other
console.log( yourCar.name );
Copy the code

Object.create also makes it easy to implement advanced concepts such as differential inheritance, where objects can be inherited directly from other objects. As we saw earlier, object.create allows us to initialize Object properties with the second supplied argument. Such as:

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}});Copy the code

If we wish not to use the prototype pattern Object.create directly, we can emulate the pattern as shown in the above example:

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();
Copy the code

A final alternative implementation of the Prototype pattern might look like this:

var beget = (function () {
 
    function F() {}
 
    return function ( proto ) {
        F.prototype = proto;
        return newF(); }; }) ();Copy the code

Stereotype pattern versus constructor pattern

The two properties of the constructor pattern above, instance and common, are only called by these names if the constructor pattern does not use the Prototype method, so we’ll just call them and their names for compatibility with the prototype method. In the case of stereotype patterns, there are their own attributes and attributes inherited from the stereotype chain.

First of all, the way objects are created is different. Prototype mode must have the presence of Prototype, while constructor mode does not have to rely on Prototype.

The second is to talk about their understanding between the two. Just as there are many members of the team in daily development, there are also groups within the team. All members relative to team is the constructor pattern, where team is not an individual, but rather a function, and all of our members work together because of the team. In daily development, teams don’t limit us very much, only mentioning our common attributes, a team, when we’re looking at the entire freeze. Most of the time we sit on our own stuff (instance properties).

Now the leader is equivalent to the prototype mode. Now the prototype object is the leader. When I work in this group, I must have some working methods defined by the leader (inherited attributes), and of course, there are some ways to deal with problems by myself (own attributes).

I think the emphasis is different, although there seems to be no difference. The constructor pattern focuses on working with multiple objects and is intended to show that these objects have something in common, but are not very restrictive to individuals. The focus of the prototype pattern should be to deal with fewer objects, requiring a close relationship between the prototype object and the instance object, which will have more restrictions on the instance object.

Instance objects can be overridden either for common properties of the constructor schema or for inherited properties of the prototype object.