> On 23.12.2010 18:28, Joel Dart wrote: > new is a bad part primarily because of its dangerous potential for adding > global variables. > Specifically, using the previous example > > > function A(x) { > > this.x = x; > > } > > > A.prototype.foo = function () { > > return this.x; > > }; > > > // and instance of the constructor "A" > > var a = new A(10); > > If instead you accidentally forgot new: > > var a = A(10); > > A would be a normal function call and, inside A, “this” would point to the > global object. You’d create a new global variable x which is obviously > unintended. Using the Object.create pattern you have a consistent experience > and don’t have to validate if you and your coworkers properly used “new.”
Or simply do this in your constructors and it becomes an irrelevant point: function Point(x, y){ if(!(this instanceof Point) return new Point(x, y); this.x = x; this.y = y; } Plus when using the constructor pattern, Java style "super" calls can be emulated with apply() -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com