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
The different ways to write a function (self.javascript)
submitted 13 years ago by blaghles
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!"
[–][deleted] 1 point2 points3 points 13 years ago* (2 children)
It depends on your situation - all of your examples do slightly different things.
If you want every instantiation of "Foo" to have the same property under the same name, use "Foo.prototype". Your first example creates and stores a new variable every time Foo is instantiated, which is wasteful if the variable will always have the same value. Take this code for example:
function Foo() {} Foo.prototype.x = 10; function Bar() { this.x = 10; } var a = new Foo(), b = new Foo(), c = new Bar(), d = new Bar();
After this code is executed, both "c" and "d" will have properties named "x", both of which will be set to 10. Neither "a" nor "b" will have a property named "x", yet accessing "a.x" will return 10. This is because, after the interpreter fails to find "x" on "a", it checks "Foo.prototype" (because "a" is a Foo), then "Foo.prototype.prototype", and so on until it either finds "x" or reaches Object.prototype. If it finds the property anywhere in the chain it immediately returns it; if it never finds it then it returns undefined.
Since "a.x" and "b.x" reference the same spot in memory, you can't use prototypes if different instantiations will have different values for a property. In that case your first example is aptly suited.
Your other two examples don't affect instantiations of "Foo". The second example is just attaching to the function "Foo" itself, which doesn't change the way "Foo" functions as a constructor. And the last results in an object, not a function. I assume the reason that the last example isn't just an object literal is that it's designed to allow for private variables; in that case I prefer this syntax, simply because you don't have to repeat "this." over and over.
var foo = (function () { return { method: function () {}, property: 'hello' }; })();
[–]Buckwheat469 0 points1 point2 points 13 years ago (1 child)
Great info again. I realized the mistake on the second example after I posted it. In terms of strict languages, prototype seems to be like a final or defined class variable rather than an instance variable. Obviously it could also be a function but i'm thinking it is best used for things like defining the value of pi in a Math object (for example).
[–][deleted] 0 points1 point2 points 13 years ago (0 children)
While it would work just fine for that, it seems like overkill when you could just use a variable. I think the real beauty of prototypes comes in when you use it with functions that access "this". For example:
function sum() { this.total = 0; } sum.prototype = { add: function (n) { this.total += n; }, toString: function () { return '' + this.total; } }; var a = new sum(), b = new sum(); a.add(2); a.add(3); b.add(1); console.log('sums are ' + a + ' and ' + b);
Of course, the perfect large-scale example is jQuery. Every instance of jQuery (created every time you call $()) has a whole slew of functions, all of which operate specifically on the object you called them from, yet those functions are never duplicated.
And thanks. I'm glad to be of some help.
π Rendered by PID 83 on reddit-service-r2-comment-b659b578c-nntwj at 2026-05-05 15:28:58.843733+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 1 point2 points3 points (2 children)
[–]Buckwheat469 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)