我之所以提出这个问题,是因为如果第一个对象上不存在该方法,我正在寻找一种方法来解决另一个对象。它不像您所要求的那样灵活 - 例如,如果两者都缺少一个方法,那么它将失败。
我正在考虑为我拥有的一个小库执行此操作,该库有助于以一种使 extjs 对象更易于测试的方式配置它们。我有单独的调用来实际获取交互对象,并认为这可能是通过有效返回增强类型将这些调用粘在一起的好方法
我可以想到两种方法:
原型
您可以使用原型来做到这一点 - 因为如果它不在实际对象上,那么东西就会落入原型。如果您希望通过使用 this 关键字的一组函数似乎不起作用 - 显然您的对象不会知道或关心其他人知道的东西。
如果所有代码都是您自己的代码,并且您没有使用 this 和构造函数...这是一个好主意,原因有很多,那么您可以这样做:
var makeHorse = function () {
var neigh = "neigh";
return {
doTheNoise: function () {
return neigh + " is all im saying"
},
setNeigh: function (newNoise) {
neigh = newNoise;
}
}
};
var createSomething = function (fallThrough) {
var constructor = function () {};
constructor.prototype = fallThrough;
var instance = new constructor();
instance.someMethod = function () {
console.log("aaaaa");
};
instance.callTheOther = function () {
var theNoise = instance.doTheNoise();
console.log(theNoise);
};
return instance;
};
var firstHorse = makeHorse();
var secondHorse = makeHorse();
secondHorse.setNeigh("mooo");
var firstWrapper = createSomething(firstHorse);
var secondWrapper = createSomething(secondHorse);
var nothingWrapper = createSomething();
firstWrapper.someMethod();
firstWrapper.callTheOther();
console.log(firstWrapper.doTheNoise());
secondWrapper.someMethod();
secondWrapper.callTheOther();
console.log(secondWrapper.doTheNoise());
nothingWrapper.someMethod();
//this call fails as we dont have this method on the fall through object (which is undefined)
console.log(nothingWrapper.doTheNoise());
这不适用于我的用例,因为 extjs 人不仅错误地使用了“this”,他们还在使用原型和“this”的原则上构建了一个完整的疯狂的经典继承类型系统。
这实际上是我第一次使用原型/构造函数,我有点困惑你不能只设置原型——你还必须使用构造函数。对象(至少在 Firefox 中)调用 __proto 有一个魔法场,它基本上是真正的原型。似乎实际的原型字段仅在构建时使用......多么令人困惑!
复制方法
这种方法可能更昂贵,但对我来说似乎更优雅,并且也适用于使用this 的代码(例如,您可以使用它来包装库对象)。它也适用于使用函数式/闭包风格编写的东西——我刚刚用 this/constructors 说明了它,以表明它适用于类似的东西。
这是模组:
//this is now a constructor
var MakeHorse = function () {
this.neigh = "neigh";
};
MakeHorse.prototype.doTheNoise = function () {
return this.neigh + " is all im saying"
};
MakeHorse.prototype.setNeigh = function (newNoise) {
this.neigh = newNoise;
};
var createSomething = function (fallThrough) {
var instance = {
someMethod : function () {
console.log("aaaaa");
},
callTheOther : function () {
//note this has had to change to directly call the fallThrough object
var theNoise = fallThrough.doTheNoise();
console.log(theNoise);
}
};
//copy stuff over but not if it already exists
for (var propertyName in fallThrough)
if (!instance.hasOwnProperty(propertyName))
instance[propertyName] = fallThrough[propertyName];
return instance;
};
var firstHorse = new MakeHorse();
var secondHorse = new MakeHorse();
secondHorse.setNeigh("mooo");
var firstWrapper = createSomething(firstHorse);
var secondWrapper = createSomething(secondHorse);
var nothingWrapper = createSomething();
firstWrapper.someMethod();
firstWrapper.callTheOther();
console.log(firstWrapper.doTheNoise());
secondWrapper.someMethod();
secondWrapper.callTheOther();
console.log(secondWrapper.doTheNoise());
nothingWrapper.someMethod();
//this call fails as we dont have this method on the fall through object (which is undefined)
console.log(nothingWrapper.doTheNoise());
我实际上预计必须在某处使用bind,但似乎没有必要。