javascript - Object.assign 重新绑定函数
问题描述
因为我需要声誉并且实际上无法对此线程发表评论:Object.assign 方法不绑定“this” 我打开我自己的问题:
这可以以某种方式解决或变通吗?我有一个“主”对象,其中包含
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__;
};
推荐阅读
- react-redux - 未调用 mapStateToProps 时如何处理这种 Redux Store 场景
- ios - 按 @ 时包括电话号码和显示名称
- c - C Array 创建木马?
- java - 如何从 firebase 检索图像和文本到回收视图?
- javascript - JavaScript 转置数据
- android - RxJava observeOn / subscribeOn 仅在测试环境中有效
- javascript - 在有多个条目的Webpack v4中,我的splitChunks没有优化,如果我的两个页面包含core-js,它们都会得到一个副本
- javascript - 用带条件的数字升序填充数组
- jquery - 我正在使用 Jquery 添加一个 src 属性,但它的工作方式不同。为什么?
- php - 无法将 html css 和 js 应用程序部署到 heroku,“注意:composer.lock 中不需要运行时;使用 PHP ^7.0.0”