17.11.2015 Views

JavaScript_Succinctly

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

parameters when defining the function, we can rely on the arguments array passed to<br />

the function to access parameters if they are sent upon invocation.<br />

Sample: sample82.html<br />

<br />

var add = function () {<br />

return arguments[0] + arguments[1];<br />

};<br />

console.log(add(4, 4)); // Returns 8.<br />

<br />

The this keyword, passed to all functions, is a reference to the object that contains the<br />

function. As you might expect, functions contained within objects as properties (i.e.<br />

methods) can use this to gain a reference to the parent object. When a function is<br />

defined in the global scope, the value of this is the global object. Review the following<br />

code and make sure you understand what this is returning.<br />

Sample: sample83.html<br />

<br />

var myObject1 = {<br />

name: 'myObject1',<br />

myMethod: function () { console.log(this); }<br />

};<br />

myObject1.myMethod(); // Logs 'myObject1'.<br />

var myObject2 = function () { console.log(this); };<br />

myObject2(); // Logs window.<br />

<br />

The arguments.callee property<br />

The arguments object has a property called callee, which is a reference to the<br />

function currently executing. This property can be used to reference the function from<br />

within the scope of the function (e.g., arguments.callee)—a self-reference. In the<br />

following code, we use this property to gain a reference to the calling function.<br />

94

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!