Quick question on JavaScript Prototypes


#1

Hello, I´m at 14/30 trying to understand this code:

function Dog (breed) {
this.breed = breed;
};

// here we make buddy and teach him how to bark
var buddy = new Dog("golden Retriever");
Dog.prototype.bark = function() {
console.log("Woof");
};
buddy.bark();

// here we make snoopy
var snoopy = new Dog("Beagle");
/// this time it works!
snoopy.bark();

The way I get it is that prototypes can be used to include a method throughout every object of the Dog function. However if you put the bark function into main Dog function wouldn't this achieve the same thing? If so what's the purpose of prototypes?
Thanks for any explanation.


#2

@larryy14,
Here some text i compiled for myself as i was on the prototype-chain-track.....

A prototype is an early sample, model, or release of a product
built to test a concept or process or
to act as a thing to be replicated or learned from.
The word prototype derives from the Greek πρωτότυπον prototypon,
"primitive form", neutral of πρωτότυπος prototypos,
"original, primitive", from πρῶτος protos, "first" and τύπος typos, "impression".

*/

/*
Inheritance and the Prototype Chain
http://stackoverflow.com/questions/19633762/classical-inheritance-vs-protoypal-inheritance-in-javascript
http://stackoverflow.com/questions/2800964/benefits-of-prototypal-inheritance-over-classical
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
*/

/*
Prototypal Inheritance is Dynamic

One of the most important advantages of prototypal inheritance is
that you can add new properties to prototypes after they are created.
This allows you to add new methods to a prototype
which will be automatically made available
to all the objects which delegate to that prototype.

This is not possible in classical inheritance
because once a class is created you can't modify it at runtime.
This is probably the single
biggest advantage of
prototypal inheritance
over classical inheritance,
and it should have been at the top.

Conclusion

Prototypal inheritance matters.
It's important to educate JavaScript programmers
on why to abandon the constructor pattern of prototypal inheritance
in favor of the prototypal pattern of prototypal inheritance.

We need to start teaching JavaScript correctly
and that means showing new programmers how to write code
using the prototypal pattern instead of the constructor pattern.

Not only will it be it easier to explain
prototypal inheritance using the prototypal pattern,
but it will also make better programmers

*/
/*
++++++++ a String type explained ++++++

why it should be Object.prototype.hasOwnProperty....
The hasOwnProperty Method is the only method
which will be fetched from the Object.prototype WITHOUT traversing
through the prototype Chain.

In the following link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
Under the Header:
Performance

The lookup time for properties that are high up on the prototype chain can have a negative impact on performance, and this may be significant in code where performance is critical. Additionally, trying to access nonexistent properties will always traverse the full prototype chain.

Also, when iterating over the properties of an object, every property that is on the prototype chain will be enumerated.

To check whether an object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use the hasOwnProperty method which all objects inherit from Object.prototype.

hasOwnProperty is the only thing in JavaScript which deals with properties and does not traverse the prototype chain._


#3

Here an answer to including the bark-method into the snoopy-Object which is an Instance of the Object class constructor Dog.
http://www.codecademy.com/forum_questions/54809212631fe9483b007e43


#4

@larryy14,
-1
The way I get it is that prototypes can be used to include a method throughout every object of the Dog function.
Should be worded as:
Every Instance of the Dog Object class constructor
( the Instance created by new Dog(argument); )
will have access to any property defined as Dog.prototype,property
( you can at any-time expand the Dog-prototype with extra properties which will directly be accessable to all Instance's of the =Dog= object class constructor )

-2
However if you put the bark function into main Dog function wouldn't this achieve the same thing?
It would lead to every Instance having its own bark Method.
Now say you wanted to change the way they bark, like changing the "Woof" into "Woof-woof"
you would then have to modify the bark Method in each and every Instance.

-3
If so what's the purpose of prototypes?
IMHO the purpose is a greater flexabillity.


#5

@larryy14,

Here some Diagrams:

= = = = = = = = = = = = = = = =

+++ prototype chain ++ in diagram +++
But in this exercise they are trying to encourage you to think about

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 ==

They want you to set up the chain:

        OBJECT
  Object.prototype
           ^
           |
           |
  Cat.prototype ((meow Method))
                   ^
                   | 
              Cat Class-constructor
                       | with this constructor
                       |  you can create Instance's of Cat
                       |  using new Cat("aName","aBreed");
                           |
                           ^
                           |
                       /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

= = = = = = = = = = = = = = = =

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 ==

They want you to set up the chain:

        OBJECT
  Object.prototype
           ^
           |
  Animal.prototype  (( has the sayName Method))
           |                              ^
           |                              | 
           |                    Animal Class-Constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Animal
           |
           |    <<== You define this link in the Chain using
           ^            Penguin.prototype = new Animal();
           |
  Penguin.prototype ((is empty))
           |               ^
           |               | 
           |          Penguin Class-constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Penguin
   /Instance /      <---   using new Penguin("aName");
   | of      |
   | Penguin |
    The Penguin-Instance has access
     to Animal.prototype.sayName Method
     via the Penguin-prototype which is
      -chained- to the Animal-prototype.

#6

@leonhard_wettengmx_n thanks for your very detailed answer. Been studying it over the weekend and it's greatly increased my understanding. The diagram proved very useful also.