call(), apply() and bind() methods are used to invoke functions and bind objects.
apply(): apply method can be used to invoke a function with arguments as array.
1 2 3 4 5 6 7 8 9 10 11 |
var myObject, myArray; function Product(price, name){ this.price = price; this.name = name; return (this.name+":- "+this.price); } myArray = [10, "Soap"] myObject = Product.apply(myObject, myArray); alert(myObject); |
call(): call method can be used to invoke a function with a list of arguments.
1 2 3 4 5 6 7 |
function add (a, b) { return a + b; } alert(add(1, 2));// Outputs: 3 alert(add.call(this, 1, 2));// Outputs: 3 |