Why using Javascript object and not prototype

2019-04-14 16:18发布

问题:

Yo,

My question is about javascript object.

I've read backbone.js code and i see model and object are using javascript object to define an object.

Like that

Backbone.Model.extend({
    initialize: function() { ... },
    author: function() { ... },
    coordinates: function() { ... },
    allowedToEdit: function(account) {
        return true;
    }
});

Why not using prototype ? Because it's redefined method for every class ? Because each object created take more space than backboneJS ?

If someone can explain it to me when and why it's interesting to use prototype ?

回答1:

The extend method you use to create object in Backbone USE the prototype, you just don't see it.
As for the other question, I guess you got it right the way you asked your first questions :). Also, from some benchmarks I saw, using prototype is faster if you instantiate many objects. That said, if you use singleton, you may want to use static properties (extend(protoProp, staticProp)).

Related Backbone's code (extend function definition):

var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;

// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);


回答2:

You seem to misunderstand. Here's the source code:

var extend = function(protoProps, staticProps) {
    var parent = this;
    var child;

    // The constructor function for the new subclass is either defined by you
    // (the "constructor" property in your `extend` definition), or defaulted
    // by us to simply call the parent's constructor.
    if (protoProps && _.has(protoProps, 'constructor')) {
      child = protoProps.constructor;
    } else {
      child = function(){ return parent.apply(this, arguments); };
    }

    // Add static properties to the constructor function, if supplied.
    _.extend(child, parent, staticProps);

    // Set the prototype chain to inherit from `parent`, without calling
    // `parent`'s constructor function.
    var Surrogate = function(){ this.constructor = child; };
    Surrogate.prototype = parent.prototype;
    child.prototype = new Surrogate;

    // Add prototype properties (instance properties) to the subclass,
    // if supplied.
    if (protoProps) _.extend(child.prototype, protoProps);

    // Set a convenience property in case the parent's prototype is needed
    // later.
    child.__super__ = parent.prototype;

    return child;
};

It might be confusing, but the essence here is that Backbone's .extend method creates new function, assigns passed object to it's prototype and returns it.

As for the second question: always use prototype if you are dealing with multiple number of objects sharing the same functionalities.



回答3:

Here you are extending a model, which is fine to use a JS object for. But if you want to implement an OOP class, interface or a library go for the JS prototype.