[英]Accessing bound functions from array of objects in javascript
I have an array of objects containing references (bound by the .bind()
method) to my class functions. 我有一个对象数组包含引用(由
.bind()
方法绑定)到我的类函数。 When I access them directly, like array[3].myFunction
, everything works fine. 当我直接访问它们时,比如
array[3].myFunction
,一切正常。 But the strange behavior occurs when I try to access these function iterating over the array. 但是当我尝试访问迭代数组的这些函数时,会发生奇怪的行为。 I've tried by
Array.forEach()
, for-in, for-of and Array.map()
function, but the result is always the same - I get the first function four times. 我已经尝试过
Array.forEach()
,for-in, Array.map()
和Array.map()
函数,但结果总是一样的 - 我得到第一个函数四次。 What am I doing wrong here? 我在这做错了什么? Thanks in advance.
提前致谢。
var Container = function() { this.function1 = function() { console.log('function 1 invoked'); }; this.function2 = function() { console.log('function 2 invoked'); }; this.function3 = function() { console.log('function 3 invoked'); }; this.function4 = function() { console.log('function 4 invoked'); }; this.array = [ { key: '1', myFunction: this.function1.bind(this) }, { key: '2', myFunction: this.function2.bind(this) }, { key: '3', myFunction: this.function3.bind(this) }, { key: '4', myFunction: this.function4.bind(this) }, ]; }; var container = new Container(); // Just printing the results below console.log('direct access:'); console.log(container.array[3].myFunction); console.log('forEach:'); container.array.forEach(el => { console.log(el.myFunction); }); console.log('for in:'); for (let i in container.array) { console.log(container.array[i].myFunction); } console.log('map:') container.array.map(el => { console.log(el.myFunction); });
PLNKR: http://plnkr.co/edit/mn8iGh4F3GcJXTNWXMiJ?p=preview PLNKR: http ://plnkr.co/edit/mn8iGh4F3GcJXTNWXMiJ?p =preview
Have a look below. 看看下面。 All seems to be working.
一切似乎都在起作用。
When you do console.log(el.myFunction)
, it will actually print the handle
and not executing it where all handle looks same as function () { [native code] }
当你执行
console.log(el.myFunction)
,它实际上会打印handle
而不执行它,其中所有句柄看起来与function () { [native code] }
When you invoke the function instead el.myFunction()
, you can see all they are calling the right functions and print the results respectively. 当您调用函数而不是
el.myFunction()
,您可以看到它们正在调用正确的函数并分别打印结果。
You can check the function invocation below. 您可以检查下面的函数调用。
var Container = function() { this.function1 = function() { console.log('function 1 invoked'); }; this.function2 = function() { console.log('function 2 invoked'); }; this.function3 = function() { console.log('function 3 invoked'); }; this.function4 = function() { console.log('function 4 invoked'); }; this.array = [ { key: '1', myFunction: this.function1.bind(this) }, { key: '2', myFunction: this.function2.bind(this) }, { key: '3', myFunction: this.function3.bind(this) }, { key: '4', myFunction: this.function4.bind(this) }, ]; }; var container = new Container(); // Just printing the results below console.log('direct access:'); container.array[3].myFunction(); console.log('forEach:'); container.array.forEach(el => { el.myFunction(); }); console.log('for in:'); for (let i in container.array) { container.array[i].myFunction(); } console.log('map:') container.array.map(el => { el.myFunction(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.