backbone.js - What is __proto__ type Surrogate in JavaScript? - Stack Overflow

Using Backbone.js, I console logged an instance of a Backbone.View.extend({}) to find the __proto__ typ

Using Backbone.js, I console logged an instance of a Backbone.View.extend({}) to find the __proto__ type to be Surrogate.

var view = Backbone.View.extend({});
console.log(view);

This resulted in an object with type Surrogate for its __proto__

__proto__: Surrogate

What is Surrogate?

Using Backbone.js, I console logged an instance of a Backbone.View.extend({}) to find the __proto__ type to be Surrogate.

var view = Backbone.View.extend({});
console.log(view);

This resulted in an object with type Surrogate for its __proto__

__proto__: Surrogate

What is Surrogate?

Share Improve this question asked Dec 20, 2012 at 1:43 AndrewHendersonAndrewHenderson 4,9923 gold badges29 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Surrogate is a "helper" class in Backbone to set up prototype chaining. Check out the source code:

  // Helper function to correctly set up the prototype chain, for subclasses.
  // Similar to `goog.inherits`, but uses a hash of prototype properties and
  // class properties to be extended.
  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(){ 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;
  };

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744050324a4549954.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信