Crockford's Prototypical Inheritance - Usage

后端 未结 2 1279
星月不相逢
星月不相逢 2020-12-24 09:02

I\'ve been building a small JS framework for use at my job, and I\'d like to employ Douglas Crockford\'s prototypical inheritance patterns. I think I get the general idea o

2条回答
  •  我在风中等你
    2020-12-24 09:38

    Vehicles.Airplane = Object.create(Vehicles.Vehicle());
    

    That line is wrong. You seem to want to use new Vehicles.Vehicle - never call a constructor without new!

    Still, I'm not sure which pattern you want to use. Two are coming to my mind:

    Classical Pattern

    You are using constructor functions just as in standard JS. Inheritance is done by inheriting the prototype objects from each other, and applying the parent constructor on child instances. Your code should then look like this:

    Vehicles.Vehicle = function () {
        // instance-specific properties and methods,
        // initialising
    }
    Vehicles.Vehicle.prototype.go = function () {
         //go forwards
    };
    Vehicles.Vehicle.prototype.stop = function () {
        //stop
    };
    
    Vehicles.Airplane = function() {
        // Vehicles.Vehicle.apply(this, arguments);
        // not needed here as "Vehicle" is empty
    
        // maybe airplane-spefic instance initialisation
    }
    Vehicles.Airplane.prototype = Object.create(Vehicles.Vehicle.prototype, {
        constructor: {value:Vehicles.Airplane, configurable:true}
    }); // inheriting from Vehicle prototype, and overwriting constructor property
    
    Vehicles.Airplane.prototype.takeOff = function () {
       //take off stuff
    };
    
    // usage:
    var airplane = new Vehicles.Airplace(params);
    

    Pure Prototypical Pattern

    You are using plain objects instead of constructor functions - no initialisation. To create instances, and to set up inheritance, only Object.create is used. It is like having only the prototype objects, and empty constructors. instancof does not work here. The code would look like this:

    Vehicles.Vehicle = {
        go: function () {
             //go forwards
        },
        stop: function () {
             //stop
        }
    }; // just an object literal
    
    Vehicles.Airplane = Object.create(Vehicles.Vehicle); // a new object inheriting the go & stop methods
    Vehicles.Airplane.takeOff = function () {
       //take off stuff
    };
    
    // usage:
    var airplane = Object.create(Vehicles.Airplane);
    airplane.prop = params; // maybe also an "init" function, but that seems weird to me
    

提交回复
热议问题