JS Call, Apply and Bind

.bind() when you want that function to later be called with a certain context( Returns function )
.call() or .apply() when you want to invoke the function immediately ( Returns value )

var object = {num:2};

var myFunction = function(arg1, arg2, arg3){
     return this.num + arg1 + arg2 + arg3;
};

console.log(myFunction.call(object, 1, 2, 3));

console.log(myFunction.apply(object, [1, 2, 3]));

var bound = myFunction.bind(object);
console.log(bound(1, 2, 3));

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Interview Question on Ruby on Rails