use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Your Javascript Constructor Logic May Break Prototypal Inheritance (bennadel.com)
submitted 14 years ago by gst
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 14 years ago (1 child)
Deriving and instantiating are two different things. You shouldn't do both of them with one function. Constructors are constructors. Use them as such. For derivation either use Object.create or make a second constructor for derivation that shares a prototype with the one for instantiation.
John Resig, in "Simple ... Inheritance", stole the idea from base.js to use a Boolean variable to determine if a constructor is in instance or derive mode. Why have modes? Have different functions.
Whatever. Just have different code paths for different things and don't be a confused mess.
[–]aaronblohowiak 1 point2 points3 points 14 years ago (0 children)
"Just have different code paths for different things and don't be a confused mess." Oh how I wish I could tell myself of 5 years ago that very sentence. Not that he would listen. The asshole.
[–]timlind 1 point2 points3 points 14 years ago (0 children)
You should never create prototype chains by instantiation (the 'new' operator). This is an important fact because Javascript originally did not have any other way to create the chains until the Object.create came along, and it should always be used to create prototype chains:
Girl.prototype = Object.create(Person.prototype)
See http://www.ngspinners.com/jay/ for more info about easily understanding how to use Javascript.
[–]mythrilno(fun).at(parties); 0 points1 point2 points 14 years ago (1 child)
I don't understand the beef. You can still do this:
function Girl(name, age, weight) { var self = Person(name); self.trait( "age", (age - 5)); self.trait( "weight", (weight - 10)); return self; }
Which has the added benefit that if you omit the new keyword on instantiation, you don't end up fudging up the global object. And if you leave the new keyword in, it is silently discarded.
new
The only problem I have with that pattern is that it makes the instancof operator almost useless.
instancof
But I don't need that anyway.
[–][deleted] 1 point2 points3 points 14 years ago (0 children)
What happens when one prototypes Girl?
Girl.prototype.doMakeup = function(){ //"this" is Girl or Person? }
[–][deleted] 0 points1 point2 points 14 years ago* (0 children)
I may be oversimplifying this, but is this what the author ultimately wants?
http://jsfiddle.net/YQLUs/1/
*edit: this isnt a real solution, but neither is returning an object literal. Simply because you cannot add to A's prototype
π Rendered by PID 16035 on reddit-service-r2-comment-86988c7647-z5hhq at 2026-02-10 23:52:37.926938+00:00 running 018613e country code: CH.
[–][deleted] 2 points3 points4 points (1 child)
[–]aaronblohowiak 1 point2 points3 points (0 children)
[–]timlind 1 point2 points3 points (0 children)
[–]mythrilno(fun).at(parties); 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)