TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stop Using Constructors in JS (2012)

55 pointsby outdooriconover 10 years ago

18 comments

phpnodeover 10 years ago
&quot;Stop writing prescriptive articles about software development when you&#x27;re speaking from a position of ignorance&quot;<p>There are many reasons to keep on using `new`, the biggest one is probably performance, `new Foo` will always outperform `Object.create(FooProto)`.<p>Really though, this kind of article just grinds my gears. There are so few absolutes in software development, articles like:<p><pre><code> &quot;You should always X&quot; &quot;Stop doing Y&quot; </code></pre> almost always really mean<p><pre><code> &quot;I don&#x27;t actually understand X or Y&quot;</code></pre>
评论 #8567426 未加载
评论 #8567243 未加载
评论 #8567520 未加载
评论 #8568301 未加载
评论 #8567533 未加载
评论 #8567521 未加载
davedxover 10 years ago
No.<p>Stop over-engineering. YAGNI!<p>Most JS codebases are light on polymorphism. I think I&#x27;ve used one UI framework that had any kind of inheritance at all. I&#x27;ve never (ever) had a bug caused by &quot;forgetting to type new&quot;.<p>Use strict. Lint your code. Use a decent build toolchain. Stop over-engineering your code unless you have a damn good justification for it.
评论 #8567518 未加载
评论 #8567059 未加载
评论 #8567090 未加载
cwmmaover 10 years ago
Timeline of learning JavaScript<p>1. Constructer&#x27;s are great it&#x27;s just like language x<p>2. uncanny valley of realizing it&#x27;s not actually like language x<p>3. understanding constructors, use them everywhere<p>4. get burned by subtler points of this in async functions and forgetting new, decide to go all functional all the time<p>5. have enough experience to avoid the foot guns, use constructors all the time as they tend to be the fastest method and have the best support.<p>This article is at stage 4
评论 #8567303 未加载
评论 #8567955 未加载
评论 #8567342 未加载
aikahover 10 years ago
Here is the problem with Javascript.<p>Basically there is so many different ways to create objects that the only thing that matters is to actually document any piece of code.<p>Dont use constructors if you want.But somewhere you&#x27;ll have to write &quot;new FileReader&quot; in the browser or &quot;new XMLHttpRequest&quot; because that&#x27;s how an api you are consuming works!!!<p>Learn how javascript constructors and prototypes work,because you just CANT ignore them,wether you want it or not.<p>Truth is everybody wants to see what they wants to see in javascript.Some want to write pure Java like OOP,some think it looks like Haskell enough to try to write Haskell in Javascript. Javascript is no Java nor Haskell,one cant ignore one part of Javascript just because it looks &quot;ugly&quot; or whatever. Javascript is going to get classes and python like features and meta features like proxies. Javascript doesnt fit one paradigm, and never did.
skywhopperover 10 years ago
0. Language X dominates development, but rigidly enforced design patterns and other universal conventions make the code confusing, hard to learn, hard to refactor, and generally slow to respond to new requirements.<p>1. New language Y pops up, thrilling small-scale devs, early adopters, and inexperienced hotshots with faster, cleaner ways of getting their jobs done without having to deal with all the cruft of old language X.<p>2. Language Y gains mindshare, inexperienced hotshots start running into age-old problems and reinvent a few wheels.<p>3. Language Y achieves dominance over the zeitgeist. Experienced software engineers start learning it. They see all the places where design patterns and rigid conventions could solve the potential problems they see, having run into it all before. They proselytize how these improvements will ensure high quality code and save all kinds of time in the future. These new patterns become standard and adoption of Language Y grows, even in larger corporate environments.<p>4. Language Y dominates development, but rigidly enforced design patterns and other universal conventions make the code confusing, hard to learn, hard to refactor, and generally slow to respond to new requirements.<p>5. Language Z pops up...
_greim_over 10 years ago
Here&#x27;s a counter-argument.<p><pre><code> var x = new Foo() </code></pre> ...is almost universally recognizable, and leaves little guesswork about the intent and meaning of the code. There&#x27;s a lot more ambiguity encountering this:<p><pre><code> var x = foo() </code></pre> What am I getting back? A primitive value? An instance? A singleton? I have to go peruse the docs. `new` can definitely be abused to do non-intuitive things, but it&#x27;s still a powerful signal to future readers of the code.
评论 #8568250 未加载
rogualover 10 years ago
I stopped using constructors (and prototypes) a couple of years ago and it&#x27;s improved my relationship with JS tremendously.<p>A couple more benefits:<p>1. You never need to see &quot;this&quot; again. You can refer to a method as object.method, and when you call that reference, you&#x27;re calling the method on that object, just like in, say, Python. No &quot;apply&quot; madness needed.<p>2. If you decide your object might take some time to construct, you can change your &quot;makeObject&quot; function to be asynchronous (using promises or Node-style callbacks). With a constructor you just can&#x27;t do this.<p>I&#x27;ve come to think of the constructor&#x2F;prototype system as one of those bits that was bolted on to the rather clean &quot;base&quot; language of JS to meet Netscape&#x27;s demand for a &quot;java-like&quot; language. You can really do without it.
评论 #8567284 未加载
评论 #8566973 未加载
评论 #8567607 未加载
kieranajpover 10 years ago
I&#x27;ve lost track of everything we&#x27;re meant to have stopped using in JS by now
评论 #8567402 未加载
mattdeslover 10 years ago
I wouldn&#x27;t say constructors are the problem -- the problem is with the &quot;new&quot; keyword.<p>I tend to use constructors like this: <a href="https://github.com/mattdesl/module-best-practices/blob/master/README.md#constructor-best-practices" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mattdesl&#x2F;module-best-practices&#x2F;blob&#x2F;maste...</a><p>Which leads to clear debugging (named constructors and their prototypes showing in console) and also works well in the off chance that you need to use &quot;inherits&quot; (eg on node EventEmitter).<p>Regarding case; it comes down to preference. I tend to name my factories CamelCase or createCamelCase, so that the return value &quot;camelCase&quot; is clearly an instance.
评论 #8567116 未加载
评论 #8567222 未加载
VeejayRampayover 10 years ago
The curse of Javascript:<p>1) Someone explains how using modern techniques can make for more maintainable code with less coupling and more reusability.<p>2) Someone pops in and notes that &quot;Feature X&#x2F;Y&#x2F;Z is not available for browsers A, B, C&quot;<p>In the end, people will keep on using the old constructors because they work all the time everywhere. And this is bad because this article does make a whole lot of sense.<p>I often have the very same problem when I look at <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Referen...</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a> where lots of the most interesting functions of said prototypes are marked as &quot;experimental&quot; and most likely won&#x27;t be universally available before 2016 or so.
评论 #8567054 未加载
评论 #8566983 未加载
drderidderover 10 years ago
Crockford has good recommendations on when to use `new`, or not, in his 2006 post on the topic[1]. I like most of what the author says re. classical OO in JS, but would stop short of not using `new` at all. Its a long-established standard and the open&#x2F;close principle[2] is probably not enough of a concern in most applications to warrant using a different &#x2F; slower &#x2F; sometimes unsupported method. And there are functional programming libraries for extending objects[3]. Just encourage people to use idiomatic JS and then layer on solutions when and if they become a problem.<p>The article uses stories of obscure bugs as justification. These stories get used an awful lot to justify dogma on everything from strict typing to Promises to whatever. I feel that&#x27;s misguided - there will always be enough rope to hang oneself with in any language, and those errors should have been mitigated by diligent use of static code analysis (linting) and unit testing. The author&#x27;s book <i>Programming JavaScript Applications</i>[4] (which looks excellent otherwise) doesn&#x27;t cover testing, either, but devotes a chapter to using <i>logging</i> for debugging. I&#x27;d rather advocate using lint-on-save, test-driven development, test coverage, and continual integration.<p>Also, for a deep dive into OO criticism, Thomas Neimann&#x27;s article <i>Nuts to OOP!</i> is a must-read. [5]<p>[1] <a href="http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/" rel="nofollow">http:&#x2F;&#x2F;yuiblog.com&#x2F;blog&#x2F;2006&#x2F;11&#x2F;13&#x2F;javascript-we-hardly-new-...</a><p>[2] <a href="http://en.wikipedia.org/wiki/Open/closed_principle" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Open&#x2F;closed_principle</a><p>[3] <a href="http://underscorejs.org/#extend" rel="nofollow">http:&#x2F;&#x2F;underscorejs.org&#x2F;#extend</a><p>[4] <a href="http://shop.oreilly.com/product/0636920033141.do" rel="nofollow">http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920033141.do</a><p>[5] <a href="http://www.embedded.com/design/prototyping-and-development/4216597/Nuts-to-OOP" rel="nofollow">http:&#x2F;&#x2F;www.embedded.com&#x2F;design&#x2F;prototyping-and-development&#x2F;4...</a>
评论 #8568426 未加载
vgallurover 10 years ago
Another interesting point of view about constructors:<p><a href="http://www.2ality.com/2013/07/defending-constructors.html" rel="nofollow">http:&#x2F;&#x2F;www.2ality.com&#x2F;2013&#x2F;07&#x2F;defending-constructors.html</a>
评论 #8567461 未加载
评论 #8567415 未加载
cphooverover 10 years ago
I use style X of coding so your style is wrong!!! God this needs to stop.
gibbitzover 10 years ago
The biggest reason for using &quot;new&quot; in my practice is to keep the value of the &quot;this&quot; keyword on the instance within the function scope. Ihave to agree with the other commenters here. We all know JavaScript isn&#x27;t OO. I just wish OO programers would stop trying to force their concepts and patterns on it. It makes maintenance a nightmare for those who didn&#x27;t write it and didn&#x27;t code Java&#x2F;C#&#x2F;Lisp or whatever OO language their concept originated in before picking up JavaScript.
benastonover 10 years ago
This article is so wrong I don&#x27;t know where to start. Sorry.
okhudeiraover 10 years ago
Note: this is from 2012-09-17 -- title should be updated.
评论 #8567023 未加载
projectileboyover 10 years ago
It makes me so glad to see that the Javascript community will spend the next decade making all of the same stupid mistakes of useless overdesign that were made by the Java community (and probably others). Tangentially, what is it about the software community that makes us waste time with new and different - but not necessarily better - implementation technologies? Is this common in other fields?
评论 #8567497 未加载
samselikoffover 10 years ago
Anybody else find that text shadow on the headings unbearable?