Built-in Functions
call()
apply()
call() method takes arguments separately.
apply() method takes arguments as an array.
call() accepts an list of arguments
apply() accepts a single array of arguments.
individual arguments of varying type
array input with similar elements
function intro(name, profession){
console.log(name + ', ' + profession);
console.log(this);
}intro('Steve', 'Captain America');into.call(undefined, 'Tony', 'Iron Man');intro.apply(undefined, ['Natasha', 'Black Widow']);With
call()andapply()method, you can write a method that can be used on different objects.With
call()andapply()method, we call an existing function and change the function context i.e. the value of thethisobject.
bind()
bind() allows us to make a copy of the function and then change the value of the this object.
let person1 = {
name: 'John',
getName: function(){
return this.name;
}
}
let person2 = { name : 'Jane' };let getNameCopy = person1.getName.bind(person2);
console.log(getNameCopy()); //JaneLast updated
Was this helpful?