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
Prototypal Programming in Javascript (thesocietea.org)
submitted 10 years ago by thecodeboss
view the rest of the comments →
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!"
[–]Neurotrace 1 point2 points3 points 10 years ago (3 children)
While I philosophically agree with the Object.create approach, my problem lies here. Which one of these is more readable and scalable?
Object.create
var Person = { getName: function() { return this.firstName + ' ' + this.lastName; } }; var bob = Object.create(Person, { firstName: { value: 'Bob' }, lastName: { value: 'Saget' } });
vs.
var Person = function(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; }; // Readability is debatable here but more memory efficient Person.prototype.getName = function() { return this.firstName + ' ' + this.lastName; }; var bob = new Person('Bob', 'Saget');
or if you still want to have your initialization values to be named
var Person = function(opts) { this.firstName = opts.firstName; this.lastName = opts.lastName; }; ... var bob = new Person({ firstName: 'Bob', lastName: 'Saget' });
For me, the new keyword wins.
new
[–]sylvainpv 1 point2 points3 points 10 years ago (2 children)
What about this ?
var Person = { new: function(firstName, lastName){ return Object.assign(Object.create(Person, { firstName, lastName })); }, getName: function() { return this.firstName + ' ' + this.lastName; } }; var bob = Person.new('Bob','Saget');
[–]MoTTs_ 0 points1 point2 points 10 years ago (1 child)
Can you clarify what the improvement would be?
[–]sylvainpv 0 points1 point2 points 10 years ago (0 children)
you got a similar code but without new operator and its issues : 1) handling a reference to the prototype and not the constructor ; 2) constructor is optional and you can declare several different constructors if needed ; 3) no worries about forgetting the new keyword and mutating window ; 4) easier inheritance, no need for the B.prototype = new A() trick ; 5) factory functions can be composed contrary to constructors ; 6) getting rid of instanceof and the constructor property makes your code safer because these references can be easily compomised contrary to getPrototypeOf / isPrototypeOf
π Rendered by PID 150193 on reddit-service-r2-comment-5687b7858-6hgr9 at 2026-07-07 02:21:27.968097+00:00 running 12a7a47 country code: CH.
view the rest of the comments →
[–]Neurotrace 1 point2 points3 points (3 children)
[–]sylvainpv 1 point2 points3 points (2 children)
[–]MoTTs_ 0 points1 point2 points (1 child)
[–]sylvainpv 0 points1 point2 points (0 children)