Interview Questions- Part-14

Guess the output

				
					const CopyEmployee = (emp) => {
  emp.age = 45; //emp1 changes here..below doesnt impact emp1
  //emp2 will be assigned below
  emp = {
    name: "Rahul",
    age: 20,
  };
  return emp;
};

let emp1 = {
  name: "Alok",
  age: 50,
};
let emp2 = CopyEmployee(emp1);
console.log(emp1); //{name: 'Alok', age: 45}
console.log(emp2); //{name: 'Rahul', age: 20}

//emp2//
//  emp = {
//   name: "Rahul",
//   age: 20,
// };
//emp1//
//only age gets changed... the reassign part doesnt impact emp1
				
			

Guess the output

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

learn.study("Javascript");//Anurag is learning Javascript
				
			

call

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(newStudent, "Javascript"); //Abhishek is learning Javascript
				
			

apply

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

//apply takes array in the second parameter
learn.study.apply(newStudent, ["Javascript"]); //Abhishek is learning Javascript

				
			

bind

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

//bind as below ..reference has to be stored..invoke it after that
const learnRef = learn.study.bind(newStudent);
learnRef("Advance Javascript"); //Abhishek is learning Advance Javascript
				
			

Leave a Comment