Monday, February 13, 2012

Function.apply and Function.call in JavaScript

Function.apply and Function.call in JavaScript: "var x = 10;
var o = { x: 15 };

function f()
{
    alert(this.x);
}

f();
f.call(o);
The first invocation of f() will display the value of 10, because this references the global object. The second invocation (via the call method) however, will display the value 15. 15 is the value of the x property inside object o. The call() method invokes the function and uses its first parameter as the this pointer inside the body of the function. In other words - we've told the runtime what object to reference as this while executing inside of function f(). "

'via Blog this'