This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by jwalker25
over 8 years

16/30 exercise help

I am not understanding where my problem is. I keep getting an error for unexpected token. Can someone help. Here is my code :

// create your Animal class here function Animal(name,numLegs){ this.name = name; this.numLegs = numLegs; };

// create the sayName method for Animal

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

// provided code to test above constructor and method var penguin = new Animal(“Captain Cook”, 2); penguin.sayName();

Answer 55a26f2c93767645150000d9

4 votes

Permalink

// create your Animal class here function Animal(name,numLegs) { this.name = name; this.numLegs = numLegs; };

// create the sayName method for Animal

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

// provided code to test above constructor and method var penguin = new Animal(“Captain Cook”, 2); penguin.sayName();

points
Submitted by manirul007
over 8 years

2 comments

Nelson over 8 years

why …. + this.name and not … + Animal[name] <—- i got lost there

cloudSolver19059 over 8 years

@Nelson from what i understand, it is because when you put —> Animal[name] <— you are calling directly to the Animal constructor from the beginning of the code. So when you run it like that it will go directly to Animal and not penguin. when you use “this,” it will use whatever you assign to that…..Does that make sense? lol

Answer 55a2011f76b8fe23380008ef

0 votes

Permalink

new function should be function

points
Submitted by Leon
over 8 years

1 comments

jwalker25 over 8 years

Thanks! But why do we need only the keyword function? why wont new function work also? And you also need parenthesis afterwards. Is this because we are defining a prototype to be a function?

Answer 55a2012b76b8febf3b0007a0

0 votes

Permalink

Reference

google search == the Book == javascript [your question] site:developer.mozilla.org

== discussions / opinions == javascript [your question] site:stackoverflow.com

== guidance == www.crockford.com http://javascript.crockford.com/code.html http://javascript.crockford.com/survey.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

points
Submitted by Leon
over 8 years

Answer 55a35a69d3292fa4270005f9

0 votes

Permalink

@jwalker25, 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

points
Submitted by Leon
over 8 years

Answer 55a35dd29113cb3f8f0005fe

0 votes

Permalink

@jwalker25,

I

With

function Animal(name,numLegs){
     this.name = name;
     this.numLegs = numLegs;
 };

you have created a template with which you can =create= new objects. These object’s are called an Instance of the Class constructor Animal. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype [quote] All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors’ prototypes override the constructor property and provide their own toString() methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain. [end quote] It is therefor that as soon as you create an object you will have access to the properties and methods of the Object prototype.

points
Submitted by Leon
over 8 years

Answer 55a36092d3292f0ba2000056

0 votes

Permalink

@jwalker25,

II

If you would have used

// create your Animal class here
 function Animal(name,numLegs){
 this.name = name;
 this.numLegs = numLegs;
 };
 var penguin = new Animal("Captain Cook", 2);

    OBJECT
  Object.prototype
           ^
           |
           |              Animal Class-Constructor
           ^            | with this constructor
           |            |  you can create Instance's of Animal
           |                               |
           |                               |
 +----------------+                        V
 | penguin Object |     <<== new Animal("Captain Cook", 2);
 +----------------+

you would have the penguin object being an Instance of the Animal Object Class constructer being a direct descendant from the Object-prototype

points
Submitted by Leon
over 8 years

Answer 55a3619a9113cb50540004cd

0 votes

Permalink

@jwalker25,

III

If you use

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

 var penguin = new Animal("Captain Cook", 2);
 penguin.sayName();

    OBJECT
  Object.prototype
           ^
           |
  Animal.prototype  (( has the sayName Method))
           |
           |              Animal Class-Constructor
           ^            | with this constructor
           |            |  you can create Instance's of Animal
           |                               |
           |                               |
 +----------------+                        V
 | penguin Object |     <<== new Animal("Captain Cook", 2);
 +----------------+

you have created the so-called prototype chain and therefor if you call a method on the penguin-object the first place looked for in this chain is in Animals prototype and if the method is not found it will be tried in the Objects prototype and when even then not found you will get an error.

points
Submitted by Leon
over 8 years

Answer 55a36518d3292f2ecc0004fa

0 votes

Permalink

@Jwalker25,

IV

In the next section you will even go further and will get something like:

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
           ^
           |
  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.  
points
Submitted by Leon
over 8 years