[英]console.log returns an additional undefined
I am trying to understand .bind and made the following code: 我试图理解.bind并编写了以下代码:
a simply object: 一个简单的对象:
person = {
name:"Joe",
surname:"Something",
tool:"gun",
action: function(){
console.log("shoot my wife");
}
}
a function: 功能:
function police(){
console.log("You are under arrest, " + this.name + " " + this.surname);
}
and binding the person object to the police function 并将人员对象绑定到警察功能
var newPolice = police.bind(person);
And finally I console log it: 最后,我将其记录下来:
console.log( newPolice() );
I do get the desired string ("You are under arrest, Joe Something") but I also get an undefined and I have no idea where it is coming from. 我确实得到了所需的字符串(“您已被捕,Joe Something”),但是我也得到了一个不确定的字符串,我也不知道它从何而来。 (In the code it is the console.log(newPolice()) that generates the undefined)
(在代码中是console.log(newPolice())生成未定义的)
This has nothing to do with bind
. 这与
bind
无关。
When you call newPolice()
it logs the result of this: 当您调用
newPolice()
它将记录以下结果:
console.log("You are under arrest, " + this.name + " " + this.surname);
When you call console.log( newPolice() );
当您调用
console.log( newPolice() );
, you now have two console.log
statements which, between them, log: ,您现在有两个
console.log
语句,它们之间记录:
newPolice
newPolice
的返回值 newPolice
doesn't have a return
statement, so it returns undefined
. newPolice
没有return
语句,因此它返回undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.