This forum is now read-only. Please use our new forums! Go to forums
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
// 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();
Answer 55a2011f76b8fe23380008ef
1 comments
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
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
Answer 55a35a69d3292fa4270005f9
@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
Answer 55a35dd29113cb3f8f0005fe
@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.
Answer 55a36092d3292f0ba2000056
@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
Answer 55a3619a9113cb50540004cd
@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.
Answer 55a36518d3292f2ecc0004fa
@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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
why …. + this.name and not … + Animal[name] <—- i got lost there
@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