Function call() Method

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
}

source

Leave a Reply