对象.分配函数的重新绑定



因为我需要信誉,实际上不能对此线程发表评论:Object.assign方法不绑定';这';我打开了我自己的问题:

这能以某种方式解决或解决吗?我有一个";主";包含的对象

function init(){ console.log(this) }.bind(this)

我正在将这个主对象克隆到另一个对象中,并且我希望init函数在从对象的作用域中运行,而不是在主对象中运行。有什么选择吗?

实际上解决了我的问题,这要归功于:

https://gist.github.com/cowboy/5373000

如果URL坏了,这里有额外的代码:

Function.prototype.bind = (function(origBind) {
return function() {
var fn = origBind.apply(this, arguments);
fn.__origFn__ = this.__origFn__ || this;
return fn;
};
}(Function.prototype.bind));
Function.prototype.unbind = function() {
return this.__origFn__;
};

最新更新