views:

344

answers:

4

I come from the traditional web developer background where I can by no means claim to really know anything about Javascript, however I am trying.

I currently have what I would describe as a fairly novice understanding of JQuery, a slightly better understanding of closures, and I've read through, and feel like I am fairly clear on Douglas Crockford's "Javascript: The Good Parts".

I've been building up some fairly javascript intensive pages lately and I'm actually pretty satisfied with the results. One thing that stands to notice is that I managed to do the whole thing with almost no global functions and without using the new operator even once.

As a matter of fact, from my reading of the above-mentioned book, the operator does nothing that you could not do another simpler way and forces you to hack the 'this' variable.

So is there something I'm missing? Does the new operator actually serve a purpose or is it just a trick to make OO programmers comfortable in what is at heart a functional language? Would I be better off striking it entirely from my JS vocabulary?

+4  A: 

First of all, kudos on reading Javascript: The Good Parts it's a great book on the language.

To answer your question, the new operator is required if you want to make use of prototypal inheritance. There is no other way to have an object "inherit" from another. That being said, you can copy properties from one object to another that would remove the need for the operator in some cases.

For example, a classical approach would use the following:

function MyClass() {};
MyClass.prototype.sayHello = function() {
   alert('hello');
};


var o = new MyClass();
o.sayHello();

You can achieve relatively the same thing as follows:

function MyClass() {
   return { 
      sayHello: function() {
         alert('hello');
      }
   };
}

var o = MyClass();
o.sayHello();
Bryan Kyle
Actually, you can avoid using new by setting the prototype property directly on an object. This has the unfortunate side effect of causing o.hasOwnProperty("prototype") to return true, where it would return false if new was used.
Frank Schwieterman
Thanks Bryan. Wouldn't you just have your function return the function you want to "inherit" from and add whatever new functionality you want directly? Isn't that the more natural way of achieving the same thing?
George Mauer
Frank: Actually assigning the prototype property on an object does not make the object inherit from that prototype. You're correct in that o.hasOwnProperty('prototype') would indeed return true.
Bryan Kyle
George: Returning the function would simply return a reference to the function not a new distinct object. If you were to add methods to that function then all references to that function would see them.The typical functional inheritance pattern is the "base" function return an object. Inheritees simply call the base function and augment that object returned with new functionality.
Bryan Kyle
jvenema
jvenema: You are absolutely correct. Not only are there memory implications there are also performance implications as well. It's much cheaper to hook up a single pointer than it is to build up a large structure.
Bryan Kyle
+3  A: 

You brought up Crockford's "JavaScript: The Good Parts."

New is a bad part. Page 114. I don't use it. (Well, almost never.)

Crockford does use it (in his beget() method).

Definitely don't use it when you can use something else. Create objects and arrays with object and array literals.

Nosredna
Haha. Actually for a book with "The Good Parts" in the title there's an awful lot of discussion of all the bad parts (as in, most of the book). Thanks for the response.
George Mauer
Just to clarify the usage in `beget` for those who haven't read the book, since there's no (standard) way to set an object's prototype, `new` is the only way to do it. The `beget` function uses `new` to create an object with a specified prototype. ECMAScript 5 will have a built-in function called `Object.create` that does the same thing.
Matthew Crumley
+2  A: 

Here's a great article on prototype.js's implementation of the typical OO structuring. This wouldn't be possible without the "new" operator.

http://prototypejs.org/learn/class-inheritance

I highly recommend reading the prototype.js source. jQuery is amazing, and highly popular, but some of the things that have been done in prototype just have no comparison** elsewhere, in particular their class structuring.

**Some people might argue that some of what prototype does shouldn't be found elsewhere - that's a different question. But for sheer understanding of what's possible with javascript, prototype is the way to go, IMHO.

jvenema
+2  A: 

There's nothing wrong with the new operator. To me, Crockford is unconvincing on the subject. Whenever you create an object you will either use new somewhere in your code or use it implicitly by creating literals (e.g. object literals such as {a: 1} or array literals such as [a, b, c]).

Tim Down
That is not at all true Tim. Like I said, I've managed to do just fine without it. You use a closure instead.
George Mauer
@George: Could you show an example?
Tim Down
@Tim Here you go: http://pastebin.com/f1d6664e0 each time you call NotificationsDisplay() you get back an object/function with its own private variables, methods and even a public method.
George Mauer
"When you create an array usingvar a = [];You're telling the interpreter to create a new runtime array. No extra processing necessary at all. Done.If you use:var a = new Array();You're telling the interpreter, I want to call the constructor "Array" and generate an object. It then looks up through your execution context to find the constructor to call, and calls it, creating your array." http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr/1273936#1273936
Nosredna
@George: Yes, you can define functions without the `new` keyword, and it's true that I didn't mention that. However, you certainly wouldn't want to create all your (not necessarily callable) objects as functions just to avoid using the `new` keyword, would you?@Nosredna: I didn't think I was arguing about the relative merits of `var a = new Array()` versus `var a = []`. I'm certainly not arguing for the former over the latter. I was pointing out that the latter syntax is defined in the ECMAScript spec as doing the same thing as `new Array()`.
Tim Down