Allows you to call (execute) a method of another object in the context of a different object (the calling object).
var result = objectName.methodName.call(thisArg[, arg1[, arg2[, ...]]]);
thisArg determines the value of this inside the method. If thisArg is null or undefined, this will be the global object.
arg1, arg2, … are arguments for the method.
You can assign a different this object when calling an existing function. this refers to the current object, the calling object. With call, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
Using call to chain constructors for an object
function product(name, value){
this.name = name;
if(value >= 1000)
this.value = 999;
else
this.value = value;
}
function prod_dept(name, value, dept){
this.dept = dept;
product.call(this, name, value);
}
prod_dept.prototype = new product();
var o1 = {testvar:22, fun:function() { alert('o1: ' + this.testvar); }};
var o2 = {testvar:33, fun:function() { alert('o2: ' + this.testvar); }};
Lets assume that function fun is the same for both o1 and o2, we would not want to type for each of our object, but, let's say, o1.fun is a very clever and long function you've been developing over the last week and now it finally works. Imagine that it has 100 lines scattered with variable this all over the code. So how would you call o1.fun but have this point to o2? Try the following:
o1.fun.call(o2);
You have forced variable this to point to o2 while executing the method fun of o1, in other words, more scholarly: the method o1.fun is running in the scope of object o2.
You can think of scope as of value of variable this while running a function, a method of an object.





