Hitesh
Hitesh

Reputation: 31

Call Method of JavaScript Not Returning Correct Value

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');

Output:

This is indigo,johnny

This is King,undefined

Upvotes: 0

Views: 66

Answers (2)

Mamdasan
Mamdasan

Reputation: 323

you are calling your call funciton inccorect, call function is not supposed to work that way, as document stated: you should use it in this manner:

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

so you need to change your code to:

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

Upvotes: 2

mak15
mak15

Reputation: 438

Try this

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'); 

Upvotes: 2

Related Questions