/ Published in: JavaScript
This method allows you to call a function as a method of another object. The first argument the call method expects is the object it is to operate on. Any others are part of the function. Note how the this keyword now refers to the comp object, so a result property is created inside comp.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function containerScope(){ var comp = { //create a new object a: 22, //set some properties b: 33 }; function square(x){ //create a new function this.result = x*x;//this refers to the object when used in call() } square.call(comp, 4); //call the square function as a method of comp object console.dir(comp); //note how a comp.result property has now been created }