Javascript 为什么要用空对象扩展函数?
我在查看时看到,Javascript 为什么要用空对象扩展函数?,javascript,Javascript,我在查看时看到,SpringSystem函数正在使用一个空对象进行扩展 var SpringSystem = rebound.SpringSystem = function SpringSystem() { this._springRegistry = {}; this._activeSprings = []; this._listeners = []; this._idleSpringIndices = []; this._boundFrameCallb
SpringSystem
函数正在使用一个空对象进行扩展
var SpringSystem = rebound.SpringSystem = function SpringSystem() {
this._springRegistry = {};
this._activeSprings = [];
this._listeners = [];
this._idleSpringIndices = [];
this._boundFrameCallback = bind(this._frameCallback, this);
};
extend(SpringSystem, {});
其中extend
如下所示:
function extend(target, source) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
第一个扩展
通过扩展空对象实现了什么?我的理解是空对象没有任何hasOwnProperty(key)
我错过了什么 您没有遗漏任何内容,代码没有任何作用。它看起来像是类模板的剩余部分,请注意,其他类和所有原型都使用此模式
这也是为什么。我的答案是,这个函数的目的是通过复制javascript的
proto
将这个函数“转换”为一个对象。我不确定,但这将是我此行的唯一目标。我不确定这对我来说是否有意义。我在node中的经验是,空对象没有任何自己的属性。这在浏览器中会有所不同吗?事实上,代码什么都不做。我的猜测是,它只是未来代码修改的一个占位符,因此,如果有任何额外的成员应该添加到对象中,它应该在那里发生。感谢您指出这一点。第一次看到这个,我开始怀疑自己到底学到了什么。