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.

The Illusion of Class

37 pointsby maniatorover 12 years ago

8 comments

2murover 12 years ago
I generally write my constructor functions so I can easily forget 'new':<p><pre><code> function Point(x, y){ if (!(this instanceof Point)) { return new Point(x, y); } this.x = x; this.y = y; } </code></pre> then you can just call:<p><pre><code> var point = Point(x, y); </code></pre> HT to issacs (node, npm) who I picked that up from.
评论 #5140781 未加载
quartertoover 12 years ago
Don't use<p><pre><code> Point3d.prototype = new Point(); </code></pre> if the constructor has side effects. Instead,<p><pre><code> function fn(){}; fn.prototype = Point.prototype; Point3d.prototype = new fn; </code></pre> does what you want.
评论 #5139757 未加载
评论 #5139693 未加载
评论 #5140812 未加载
评论 #5139792 未加载
fasteddie31003over 12 years ago
The JavaScript prototype pattern is the most poorly designed aspect of the JavaScript language. Cross-browser prototype compatibility is a total headache too.<p>My solution: abstract away the headache. Use Underscore's extends function or take a look at how Prototype.js handles inheritance: <a href="http://prototypejs.org/learn/class-inheritance" rel="nofollow">http://prototypejs.org/learn/class-inheritance</a> .<p>Both solutions are much more simple and do the same thing.<p>Also, if you are going into a JavaScript interview, know the stupid prototype pattern, someone always asks about it.
评论 #5140887 未加载
curtisover 12 years ago
Whenever I find myself needing to roll my own class-based system in JavaScript I use this function:<p><pre><code> function subclass(constructor, superConstructor) { function surrogateConstructor() { } surrogateConstructor.prototype = superConstructor.prototype; var prototypeObject = new surrogateConstructor(); prototypeObject.constructor = constructor; constructor.prototype = prototypeObject; } </code></pre> Then you can say things like:<p><pre><code> subclass(DerivedConstructor, BaseConstructor); </code></pre> There's a full write-up of this approach at <a href="http://www.golimojo.com/etc/js-subclass.html" rel="nofollow">http://www.golimojo.com/etc/js-subclass.html</a>.
评论 #5141086 未加载
msluyterover 12 years ago
Question: in the object tree for Point3d, it looks like there are two __proto__ attributes at the same level of nesting. Should the second belong to Point and be indented? (I'm not that experienced with javascript so I could simply be confused.)
评论 #5139872 未加载
bjhoops1over 12 years ago
Can't help but picture GOB Bluth: "Illuuuusions, Michael!"<p>Great article though! This is a constant source of confusion for anyone not intimately familiar with JavaScript.
denysoniqueover 12 years ago
For a Class based approach -- CoffeeScript
jiggy2011over 12 years ago
What is a "classically trained programmer"?
评论 #5140051 未加载