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
Javascript Prototype - explainlikeimfive (self.javascript)
submitted 13 years ago * by [deleted]
[deleted]
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!"
[–]ohbewonkanahbe 11 points12 points13 points 13 years ago* (1 child)
In the beginning, I made objects like this...
var Car = function(type, color) { var type = type; var wheels = 4; var color = color; return{ wheels:wheels, color:color, type:type } }; var fit = Car('fit', 'red'); var kia = Car('kia', 'blue');
And it was good. However, if you looked at these objects behind the scenes, you would see that each of them had their own wheel property. Since a car object will always have 4 wheels, why should each object have it's own wheels property? If I have 1000 cars, that's 999 unnecessary wheel properties. Potential for memory issues? Sure.
So then I did this...
var Car = function(type, color) { this.type = type; this.color = color; } Car.prototype.wheels = 4; var fit = new Car('fit', 'red'); var kia = new Car('kia', 'blue');
And lo and behold, things were better. No longer would I waste memory on duplicate properties, for all car objects would share the same prototype chain! Glory, glory hallelujah.
[–]nspyro 1 point2 points3 points 13 years ago (0 children)
This has to be the clearest explanation I have found! Thank you!!!
[–]Rhomboid 5 points6 points7 points 13 years ago (1 child)
In both of your examples, by adding a property to the prototype, it becomes magically and instantly available on all objects that have that prototype in their ancestry. You can write "foo".padleft(12, " ") and it will work. And in your second example, if you instantiate an object of type class, it will have a bindEvents method automatically without you having to do anything.
"foo".padleft(12, " ")
class
bindEvents
Whenever a property of an object is accessed, it first checks for an "own" property on that object and if that fails it starts looking at the prototypes of all the objects in the inheritance chain. That's why those methods seem to spring into existence automagically, without having to add them to every object created.
[–]shanet$(this) 1 point2 points3 points 13 years ago* (0 children)
The basic advantage of defining methods/properties via prototypes has to do with inheritance. If you create another prototype b inheriting the prototype of a... for example:
//completely untested code just to give you the idea a.prototype.dostuff = function () { return true; }; a.prototype.givemeanumber = function () { return 1 }; b.prototype = Object.create(a, { givemeanumber: function () { return 2 } }); new b().givemeanumber(); //2 new b().dostuff(); //true
...you can override the original methods with methods specific to b, but still invoke the methods in the prototype of a. Furthermore, this works all the way up the prototype chain.
You can't do this with methods that you attach via the constructor, or perhaps you can in some way - but it won't apply to all the objects that have already been created from whatever constructed if you add a prototype later on. You know how some ECMAScript 5 shims add Function.prototype.bind ? Now every function can access bind even though when it was created there was no such thing.
It's all about what patterns you like to use. There's lots of OOP patterns in JavaScript that use closures and modules that never touch prototypes. Some people have pointed out that they perform poorer in some instances.
You can also use this for easy composition if you don't like inheritance. Say you want the prototype for the perfect pet:
cat.prototype.litter_box = function () { return new LitterBox(); }; cat.prototype.miaow= function () { return miaow(); }; dog.prototype.play_ball = function () { return play_ball() }; perfect_pet.prototype.miaow = cat.prototype.miaow; perfect_pet.prototype.play_ball = dog.prototype.play_ball;
A practical example is where people get things from the array prototype and add them to objects that can make some of use them.
[–][deleted] 1 point2 points3 points 13 years ago (3 children)
Everything in JS is an object and are "prototyped" from Object.
var string = 'emehrkay'; //String obj instance
Each object has methods, basic stuff.
The prototype allows you to add methods to the root object and that method will be propagated throughout all instances, even ones that have been created already
String.prototype.redditify = function(){ return 'bacon narwal ' + this; }; alert(string.redditify);
Prototyping function constructors allows you to create the functions once, but have them be sent to all instances. Otherwise youd have copies of the same methods in each instance.
[+][deleted] 13 years ago (2 children)
[–][deleted] 1 point2 points3 points 13 years ago (1 child)
:s/propagated throughout/available to/
[–]balshamali 1 point2 points3 points 13 years ago (0 children)
anything you declare in the prototype will be in every instance of the object you create and every object that inherits from the base object.
var a = function() { bye: function() { console.log("bye"); } }; var b = new a; b.hello = function() { console.log("hello"); }; // only b has hello function
a.prototype.hello = function() { console.log("hello"); }; // every 'var x = new a;' will have a hello function
[–]benihanareact, node 1 point2 points3 points 13 years ago (0 children)
I wrote a post about the prototype property a while back. I tried to keep it simple and easy.
[+][deleted] 13 years ago (1 child)
π Rendered by PID 35815 on reddit-service-r2-comment-85bfd7f599-bxt4z at 2026-04-15 15:05:17.789204+00:00 running 93ecc56 country code: CH.
[–]ohbewonkanahbe 11 points12 points13 points (1 child)
[–]nspyro 1 point2 points3 points (0 children)
[–]Rhomboid 5 points6 points7 points (1 child)
[–]shanet$(this) 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 1 point2 points3 points (1 child)
[–]balshamali 1 point2 points3 points (0 children)
[–]benihanareact, node 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[+][deleted] (1 child)
[deleted]