[英]Why console.log doesn't work as an argument?
In javascript there is a callback, and functions are first-class objects right? 在javascript中有一个回调,函数是一流的对象吗? so i can pass a function as an argument however if i pass console.log() it doesn't work why is that isn't it a function as well?
所以我可以传递一个函数作为参数但是如果我通过console.log()它不起作用为什么它不是一个函数呢?
setTimeout(function(){console.log("hello")},5000);
this code is valid however 但是这段代码是有效的
setTimeout(console.log("hello"),5000);
produces an error, why is that? 产生错误,为什么?
When you call console.log
with some argument, the argument is printed to console and the function returns undefined
. 当您使用某个参数调用
console.log
时,该参数将打印到控制台,并且该函数将返回undefined
。
So when you do: 所以当你这样做时:
setTimeout(console.log("hello"),5000);
"hello"
will be printed, but what you're essentially doing is: 将打印
"hello"
,但你基本上做的是:
setTimeout(undefined, 5000);
In the other example (that works) you create a new function, but you do not call it. 在另一个示例(有效)中,您可以创建一个新函数,但不要调用它。 So you pass this new function to
setTimeout
, and that is why it works. 所以你将这个新函数传递给
setTimeout
,这就是它工作的原因。
The reason that the following code 以下代码的原因
setTimeout(console.log("hello"),5000);
fails is because console.log()
is invoked directly inside the setTimeout parameter and it always returns undefined (more info: MDN Documentation on Console.log ). 失败是因为
console.log()
直接在setTimeout参数内调用,它总是返回undefined(更多信息: Console.log上的MDN文档 )。 This means you are essentially running setTimeout(undefined,5000);
这意味着你实际上是在运行
setTimeout(undefined,5000);
You need to provide a function declaration that is not invoked. 您需要提供未调用的函数声明。 If you wrap the code inside of a function declaration and do not invoke it, setTimeout will invoke it for you after the specified time lapses.
如果将代码包装在函数声明中并且不调用它,则setTimeout将在指定的时间过去后为您调用它。 This is why your first statement works, you provide a function declaration:
这就是你的第一个语句有效的原因,你提供了一个函数声明:
setTimeout(function(){console.log("hello")},5000);
If you had invoked the function you provided in the first parameter it would also return undefined and "hello" would be output immediately. 如果你调用了你的第一个参数提供的功能也将返回不确定的,“你好”将立即输出。
So if you provide a function declaration it will work: 因此,如果您提供函数声明,它将起作用:
setTimeout(()=>{console.log("hello")},5000);
Another way to use console.log directly without a function declaration is to bind it to a variable (more info: MDN Documentation on Function.prototype.bind ). 在没有函数声明的情况下直接使用console.log的另一种方法是将其绑定到变量(更多信息: Function.prototype.bind上的MDN文档 )。 An example using
.bind
prototype: 使用
.bind
原型的示例:
setTimeout(console.log.bind(null,"hello"),5000);
The code above binds "hello" to invocation of console.log. 上面的代码将“hello”绑定到console.log的调用。 The first parameter is
null
in this example but it is the this
context. 在此示例中,第一个参数为
null
,但它是this
上下文。
setTimeout also allows you to pass variables that you want the function to be invoked with. setTimeout还允许您传递要调用函数的变量。 See MDN Documentation on setTimeout
请参阅setTimeout上的MDN文档
For example to pass a variable: 例如,传递一个变量:
setTimeout(console.log,5000,'hello');
In the above example you are telling setTimeout to invoke console.log in 5 seconds with the variable (in this case a sting) of 'hello'. 在上面的例子中,您告诉setTimeout在5秒内使用'hello'变量(在本例中为sting)调用console.log。
Invoking console.log('hello')
will return undefined
so you are not really passing it to setTimeout
, It will print "hello" though but not inside a callback. 调用
console.log('hello')
将返回undefined
因此你并没有真正将它传递给setTimeout
,它会打印“hello”但不在回调中。
In most cases it wont throw an error (as you can see in the example below). 在大多数情况下,它不会抛出错误(正如您在下面的示例中所见)。
What you can do however is pass console.log
(the function) and a 3rd argument the string "hello" in our case. 然而,你可以做的是传递
console.log
(函数)和第三个参数 ,在我们的例子中是字符串“hello”。
Running example with all 3 cases: 所有3个案例的运行示例:
setTimeout(console.log("hello"),500); setTimeout(function(){console.log("hello2")},500); setTimeout(console.log,500,"hello3");
它会产生一个错误,因为它会计算console.log(...)
,它的计算结果为undefined
,因此不是函数。
setTimeout accepts the function as a parameter, not the function call. setTimeout接受函数作为参数,而不是函数调用。 console.log() is basically invoking the function/method but setTimeout requires the reference or more specifically called the callback.
console.log()基本上是调用函数/方法,但setTimeout需要引用或更具体地称为回调。
In your example:- 在你的例子中: -
setTimeout(function(){console.log("hello")},5000);
you can call it like 你可以称之为
var callback = function(){console.log("hello")};
setTimeout(callback,5000);
The callback will be called by setTimeout later in future. 将来稍后将通过setTimeout调用回调。 I hope it clears everything.
我希望它能清除一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.