Exploring the 'new' Operator in Javascript
In Javascript, there’s an interesting operator called new. In JavaScript: The Good Parts, new is listed as a not recommended operator. Let’s explore the usage of new. Consider the following code: function test() { var foo1 = new function () { this.i = 1; } var foo2 = function () { this.i = 2; } M.dis(foo1.i); // 1 M.dis(foo2.i); // undefined M.dis(this.i); // undefined foo2(); M.dis(foo2.i); // undefined M.dis(this.i); // 2 M.dis(foo1.prototype); // undefined M.dis(foo2.prototype); // [object Object] } In the code, M.dis() is equivalent to document.writeln(). The above code clearly illustrates the difference between using and not using the new operator. Below are my understandings based on the results of using and not using the new operator. ...