[英]console.log returns an additional undefined
我試圖理解.bind並編寫了以下代碼:
一個簡單的對象:
person = {
name:"Joe",
surname:"Something",
tool:"gun",
action: function(){
console.log("shoot my wife");
}
}
功能:
function police(){
console.log("You are under arrest, " + this.name + " " + this.surname);
}
並將人員對象綁定到警察功能
var newPolice = police.bind(person);
最后,我將其記錄下來:
console.log( newPolice() );
我確實得到了所需的字符串(“您已被捕,Joe Something”),但是我也得到了一個不確定的字符串,我也不知道它從何而來。 (在代碼中是console.log(newPolice())生成未定義的)
這與bind
無關。
當您調用newPolice()
它將記錄以下結果:
console.log("You are under arrest, " + this.name + " " + this.surname);
當您調用console.log( newPolice() );
,您現在有兩個 console.log
語句,它們之間記錄:
newPolice
的返回值 newPolice
沒有return
語句,因此它返回undefined
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.