Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中将方法作为参数传递_Javascript_Function_Methods_Functional Programming_This - Fatal编程技术网

在JavaScript中将方法作为参数传递

在JavaScript中将方法作为参数传递,javascript,function,methods,functional-programming,this,Javascript,Function,Methods,Functional Programming,This,我正在编写一个函数,它允许我通过将另一个函数作为值传递来显示数组的值 这段代码工作得很好: function forEach(array, action) { for (var i = 0; i < array.length; i++) action(array[i]); } forEach(["foo", "bar", "qux"], alert); 函数forEach(数组、动作){ 对于(var i=0;i

我正在编写一个函数,它允许我通过将另一个函数作为值传递来显示数组的值

这段代码工作得很好:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}
forEach(["foo", "bar", "qux"], alert);
函数forEach(数组、动作){
对于(var i=0;i
但如果我试图传递一个带有属性的函数,它会抛出错误:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}
forEach(["foo", "bar", "qux"], console.log);
函数forEach(数组、动作){
对于(var i=0;i
如果我这样做:
action.log(数组[i])

它工作正常,但这只允许我传递带有
.log
属性的函数。有什么方法可以编写一个函数,允许我将另一个函数和属性作为值传递吗?

您可以使用类似

function forEach(array, action) {
for (var i = 0; i < array.length; i++)
  action(array[i]);
}
forEach(["foo", "bar", "qux"], function(x){ return console.log(x) });
函数forEach(数组、动作){
对于(var i=0;i
您可以使用

function forEach(array, action) {
for (var i = 0; i < array.length; i++)
  action(array[i]);
}
forEach(["foo", "bar", "qux"], function(x){ return console.log(x) });
函数forEach(数组、动作){
对于(var i=0;i
您可以使用

function forEach(array, action) {
for (var i = 0; i < array.length; i++)
  action(array[i]);
}
forEach(["foo", "bar", "qux"], function(x){ return console.log(x) });
函数forEach(数组、动作){
对于(var i=0;i
您可以使用

function forEach(array, action) {
for (var i = 0; i < array.length; i++)
  action(array[i]);
}
forEach(["foo", "bar", "qux"], function(x){ return console.log(x) });
函数forEach(数组、动作){
对于(var i=0;i
问题不是使用对象的方法,而是
控制台。log
特定问题。具体来说,对于
console.log
,您可以将上下文绑定到它,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);
其他方法也可以,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);

问题不是使用对象的方法,而是
console.log
特定的问题。具体来说,对于
console.log
,您可以将上下文绑定到它,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);
其他方法也可以,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);

问题不是使用对象的方法,而是
console.log
特定的问题。具体来说,对于
console.log
,您可以将上下文绑定到它,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);
其他方法也可以,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);

问题不是使用对象的方法,而是
console.log
特定的问题。具体来说,对于
console.log
,您可以将上下文绑定到它,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);
其他方法也可以,例如:

forEach(["foo", "bar", "qux"], console.log.bind(console));
var x = {
    test: function(str){
        alert(str);
    }
}

forEach(["hello", "world"], x.test);

在Chrome中,这是预期的,因为console.info预期其“this”引用是console,而不是window。另外,我发现它在Firefox32/IE中工作


在Chrome中,这是应该的,因为console.info希望其“this”引用是console,而不是window。另外,我发现它在Firefox32/IE中工作


在Chrome中,这是应该的,因为console.info希望其“this”引用是console,而不是window。另外,我发现它在Firefox32/IE中工作


在Chrome中,这是应该的,因为console.info希望其“this”引用是console,而不是window。另外,我发现它在Firefox32/IE中工作

related:-pass
console.log.bind(console)
instine.related:-pass
console.log.bind(console)
instine.related:-pass
console.log.bind(console)