Return to Snippet

Revision: 4368
at December 2, 2007 09:24 by 1man


Initial Code
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
}

Initial URL


Initial Description
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.

Initial Title
Function call() Method

Initial Tags
function

Initial Language
JavaScript