最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Does Javascript's new operator do anything but make life difficult? - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Nov 16, 2009 at 19:47 George MauerGeorge Mauer 122k139 gold badges396 silver badges630 bronze badges 6
  • Not to be overly critical, but note <a href="en.wikipedia.org/wiki/… programming</a> vs. <a href="en.wikipedia.org/wiki/… programming</a> Sorry, feeling picky today... – Kendrick Commented Nov 16, 2009 at 19:54
  • I'm not sure I get what you mean Kendrick, are you saying that I should have linked the term "functional"? – George Mauer Commented Nov 16, 2009 at 19:56
  • 1 Kendrick, are you saying JavaScript is not a functional language? I think it is. – Nosredna Commented Nov 16, 2009 at 20:01
  • 1 I don't see how you could do it yourself, the biggest problem with prototypal inheritance for me is dropping real construction args, it is imo that a class should enforce its creation (as much as reasonable) by its rules.. being able to create an object that is 'uninitialized' just makes it harder to discover bugs.. and the specification initialization pattern is an ugly hack. – meandmycode Commented Nov 16, 2009 at 20:18
  • 5 This has been answered really well already, including addressing Crockford's opinion on new: stackoverflow.com/questions/383402/… – Crescent Fresh Commented Nov 16, 2009 at 20:23
 |  Show 1 more comment

4 Answers 4

Reset to default 8

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();

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.

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]).

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.

发布评论

评论列表(0)

  1. 暂无评论