Lost on prototypes, I think


I have no idea what prototypes are or what they do. Would a class be a prototype too? Can someone explain prototypes to me fully in Lamens terms please?


OK after reading a.....lengthy....article this is what I understand., correct me if i'm wrong. prototypes are any set of values that define an object with a class name. ALSO ever y object, even if not defined by the programmer, has a prototype properyt which points to the parent prototype. Called a prototype chain. The reason prototypes have prototype properties is because prototypes are also object. From my knowledge every object has a prototype. Getting near the end of a prototype chain the prototype property will bring up NULL because there is no parent prototype to prototype prototype. Correct?


More often referred to as the top of the prototype chain and the first object to not have a prototype, allowing the seek process to terminate. null is not an object per se, hence it has no constructor, either. But it is representative, unlike undefined which is just a response, not a representation that can be assigned to a variable.

var a = prompt("Click Cancel or Press Esc");
console.log(a);         // null

The only way to represent undefined is to either declare a variable without defining it, or to invoke a function that has no return value.

var b;
console.log(b);         // undefined
var c = function () {};
console.log(c());       // undefined

Anyway, better I don't confuse you further and let you mull this over.



A good article


thanks. Good to know difference between undefined and NULL thanks again for helping.


so another question then. If prototypes can be defined by saying

objectName.prototype.prototypeProperty = prototypePropertyValue;

Which I have mostly seen prototypePropertyValue to be functions.....methods. As I was if prototypes can be defined that way, can they be defined this way too?

var objectName ={
prototype : {
prototypeProperty: prototypePropertyValue

I have yet to try and it seems that these Codecademy lessons are limited. I can't extend my imagination in these lessons. If so you could still define the prototype class with...

objName.prototype = new prototypeClass();

It seems the given way might be more practical but meh. I just want to know.

Also can multiple prototypes be assigned to one object? SO rather than being a chain(well it's still a chain) can it be more like a tree? If it IS possible it's probably not practical due to system consumption but nonetheless I like to know the extent of my abilities. I just like to know.


A prototype is an object, with properties like any other object. They permit us to extend all instances of a class without bundling everything into the class definition, meaning there are less duplicated strings in memory. A prototype is one copy of a string (loosely speaking), whereas a method in a constructor definition is a string in every instance.

The biggest advantage of prototypes is that they are dynamic in nature. Update the prototype and you update every instance of the class, new or old. Update the constructor and only the newer instances reflect the update. Older instances of the class are not updated.

We assign prototypes to the class constructor object. That way all instances inherit them, dynamcially.


Also what prototype is info assigned to without declaring an objects prototype? For example we can say

Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);

Yet, we never have to declare a particular prototype class name for Animal. It is clearly being stored in some sort of prototype, what is the name of this?


lol thanks your answer dynamically answered all of my questions there. Yes they can but due to the purpose of prototypes it's redundant.


When we declare a new instance of the Animal class, it will inherit the sayName() method.

var myCat = new Animal("Tigger", 4);
myCat.sayName();                     // Tigger
console.log(myCat.numLegs);          // 4


OOOOOOKAY so sayName property is not owned by any prototype until defined by a constructor or called on. so if myCat is owned by the Animal class then the properties and values of what we have made are assigned to the Animal class?
Animal.prototype.prototypeProperty = value;

automatically assigns the properties and values to Animal until told otherwise. Beyond that point would be NULL correct? Yet if we say:

Animal.prototype = new newPrototypeName();

we have assigned it a valid prototype to store the data. Have I understood correctly?


Say we have a class to which many methods and attributes could be assigned. Which of those attributes need to be instance based, and which need to be class based? That's the question. This is not about privacy, mind, just public attributes and methods.

If every instance is to have a widgets property set to a constant value, then that relates to class, not instance. Likewise with most methods. They relate to class, not instance, only every instance 'owns' them within their own unique execution context.

myCat 'owns' sayName() within the context of its own environment. This means the method sees only its owner object, and no other. this is confined to that context in alias form particular to that object..

I need to know that we're on the right track, so will give leave for your assent to continue from here...


I'm basically finished now and I understand. You have answered alot of my quesions so I appreciate it very much.God bless Person["name"].


J a v a S c r i p t and it's == p r o t o t y p e - C h a i n ==

  Cat.prototype ((meow Method))  Cat.prototype.meow =
           |                      function() { console.log("Meow!"); };
           |          Cat Class-constructor
           |              with this constructor
           |              you can create Instance's of Cat
           |                |
           |                V
           |                |
           |              using new Cat("aName","aBreed");
           |                |
           |                V
           |                |
           |            /Instance's / 
           |            | of      |
           |            | Cat |
           ^     The Cat-Instance has access
           |     to Cat.prototype.meow Method
           |     via the Cat-prototype which is
           ^     -chained- to the Cat-prototype.
      + - -+- - - - -+
      |              |
 cheshire          gary
 Instance         Instance