Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Inside the scope/body of all functions, the this and arguments values are available.
The arguments object is an
array-like object containing all of the parameters being passed to the
function. In the code below, even though we forgo specifying parameters
when defining the function, we can rely on the arguments array passed to the function to access
parameters if they are sent upon invocation.
<!DOCTYPE html><html lang="en"><body><script>
var add = function() {
return arguments[0] + arguments[1];
};
console.log(add(4, 4)); // returns 8
</script></body></html>
The this keyword, passed to all
functions, is a reference to the object that contains the function. As you
might expect, functions contained within objects as properties (i.e.,
methods) can use this to gain a
reference to the “parent” object. When a function is defined in the global
scope, the value of this is the global
object. Review the code below and make sure you understand what this is returning.