JavaScript调用方法没有返回正确的值



const flightName = {
airline: 'luftansa',
itaCode: 'LH',
book: function(flightname, text) {
console.log(`This is ${flightname},${text}`);
}
}
flightName.book('indigo', 'johnny');
let book1 = flightName.book;
book1.call('Johnny', 'King');

输出:

This is indigo,johnny
This is King,undefined

试试这个

const flightName ={
airline: 'luftansa',
itaCode: 'LH',
book: function(flightname,text)
{
console.log(`This is ${flightname},${text}`);
}
}
flightName.book('indigo','johnny');
let book1 = flightName.book;
book1.call(this, 'Johnny','King'); 

你正在调用你的调用函数不正确,调用函数不应该这样工作,如文档所述:你应该这样使用它:

call()
call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, arg2)

所以你需要把你的代码改成:

book1.call(flightName,'johny', 'king');

最新更新