Vin*_*C M 2 javascript arrays function
考虑我有四个功能:
function first() {
console.log("This is the first function");
}
function second() {
console.log("This is the second function");
}
function third() {
console.log("This is the third function");
}
function fourth(name) {
console.log("This is the fourth function " + name);
}
Run Code Online (Sandbox Code Playgroud)
我试图将上面的函数列表传递给函数:
var list_of_functions = [first, second, third, fourth];
executeFunctions(list_of_functions);
Run Code Online (Sandbox Code Playgroud)
这是executeFunction:
function executeFunctions(list_of_functions) {
console.log("inside new executeFunctions");
list_of_functions.forEach(function(entry) {
entry();
});
}
Run Code Online (Sandbox Code Playgroud)
如何fourth在数组本身中传递函数的name参数?有没有办法做到这一点?
例如,我想做这样的事情:
var list_of_functions = [first, second, third, fourth("Mike")];
Run Code Online (Sandbox Code Playgroud)
显然,上述说法是错误的.有没有办法做到这一点?
你可以使用这个bind功能:
var list_of_functions = [first, second, third, fourth.bind(this, "Mike")];
Run Code Online (Sandbox Code Playgroud)
的第一个参数bind是你想要的this是内部fourth功能(可this,null或任何其他物体).
| 归档时间: |
|
| 查看次数: |
36 次 |
| 最近记录: |