Sunday, May 12, 2013

The superpowers of constructors

2:37 AM Posted by Unknown No comments
Invoking a function as a constructor is a powerful feature of JavaScript, because when a constructor is invoked, the following special actions take place: A new empty object is created. This object is passed to the constructor as the this parameter, and thus becomes the constructor’s function context. In the absence of any explicit return value, the new object is returned as the constructor’s...

Scoping and functions

2:16 AM Posted by Unknown No comments
In JavaScript, scopes are declared by functions, and not by blocks. The scope of a declaration that’s created inside a block isn’t terminated (as it is in other languages) by the end of the block. Consider the following code: if (window) {   var x = 213; } alert(x); In most other languages, one would expect the scope of the declaration for x to terminate at the end of the block created...