试图弄清楚下面的代码是如何工作的以及它的作用......有人可以向我解释一下吗?
据我所知,['handleEvent'] 将立即执行并运行 forEach 循环一次...
我假设这与事件监听器有关,然后事件监听器执行字符串引用的方法?下面的代码位于 constructor
方法中,未分配给变量或任何内容...
class plugin{
constructor(sidebar, options = {}){
this.options = plugin.extend(DEFAULTS, options);
// Bind event handlers for referencability.
['handleEvent'].forEach( (method) => {
this[method] = this[method].bind(this);
});
// Initialize sticky sidebar for first time.
this.initialize();
}
最佳答案
['handleEvent'].forEach( (method) => {
this[method] = this[method].bind(this);
});
完全等同于
this['handleEvent'] = this['handleEvent'].bind(this);
想必作者这样写是为了方便以后添加新的字符串。如果数组是“用一次就扔掉”的对象,则不需要分配该数组。
关于javascript - 数组未分配给变量?它是如何工作的以及它到底在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46849375/