gpt4 book ai didi

javascript - 什么是 JavaScript 中的 __proto__ 类型代理?

转载 作者:数据小太阳 更新时间:2023-10-29 04:09:17 25 4
gpt4 key购买 nike

使用 Backbone.js,我通过控制台记录了一个 Backbone.View.extend({}) 的实例,以找到要作为代理项的 __proto__ 类型。

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

这导致其 __proto__ 的类型为 Surrogate 的对象

__proto__: Surrogate

什么是代理?

最佳答案

Surrogate 是 Backbone 中的一个“助手”类,用于设置原型(prototype)链。查看源代码:

  // 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;
};

关于javascript - 什么是 JavaScript 中的 __proto__ 类型代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com