The first argument of the call
method is used to set the this
keyword (the function context) explicitly, inside the invoked function, e.g.:
function test(data, aFunc) {
var newData = data + " Shawn";
aFunc.call(newData);
}
test("hello", function () {
alert(this); // hello Shawn
});
If you want to invoke a function without caring about the context (the this
keyword), you can invoke it directly without call
:
function test(data, aFunc) {
var newData = data + " Shawn";
aFunc(newData); // or aFunc.call(null, newData);
}
test("hello", function (data) {
alert(data);
});
Note that if you simply invoke a function like aFunc(newData);
or you use the call
or apply
methods with the this
argument set as null
or undefined
, the this
keyword inside the invoked function will refer to the Global object (window
).