What is __proto__ type Surrogate in JavaScript?

Using Backbone.js, I console registered an instance of Backbone.View.extend({}) to find the __proto__ type for the surrogate.

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

The result is an object of type Surrogate for __proto__

 __proto__: Surrogate 

What is a surrogate?

+8
javascript
source share
1 answer

A surrogate is a β€œhelper” class in Backbone to set up a prototype chain. 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 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` 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 prototype is needed // later. child.__super__ = parent.prototype; return child; }; 
+11
source share

All Articles