Monthly Archives October 2022

JavaScript Interview Question

1). Call, Bind & Apply:

Call:
var emp1 = {firstName: “Subhash”, lastName: “Yadav”};
var emp2 = {firstName: “Nagina”, lastName: “Yadav”};
function invite(greeting1, greeting2) { console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘, ‘ + greeting2);
}
invite.call(emp1, ‘Hello’, ‘how are you’);
invite.call(emp2, ‘Hello’, ‘how are you’);
Bind:
var emp1 = {firstName: “Subhash”, lastName: “Suman”};
var emp2 = {firstName: “Kanchan”, lastName: “Yadav”};

function invite(arg1, arg2) {
console.log(arg1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘, ‘ + arg2);
}
var emp1B = invite.bind(emp1);
var emp2B = invite.bind(emp2);
emp1B(‘Hello’, ‘how are you!!’);
emp2B(‘Hello’, ‘where are you!!’);
Apply:
var emp1 = {firstName: “Suman”, lastName: “Subhash”};
var emp2 = {firstName: “Yadav”, lastName: “Kanchan”};

function invite(greeting1, greeting2) {
console.log(greeting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘, ‘ + greeting2 + ‘!!’ );
}

invite.apply(emp1, [‘Hello’, ‘how are you’]);
invite.apply(emp2, [‘Hello’, ‘how are you’]);

  admin   Oct 17, 2022   Blog   0 Comment Read More