I've been doing a lot of HTML and CSS and now I'm doing a JS tutorial. I ran into this problem though. I don't know how to access the prototype.
Instructions:
Create a class named Animal with two properties,
name
andnumLegs
. The Animal constructor should have two arguments whose values are assigned toname
andnumLegs
.Next, change the prototype of Animal and add a method sayName that prints to the console "Hi my name is
[name]
", where[name]
is the value of name.Click "Stuck? Get a hint!" for examples of how to create a class and how to add a method to an object's prototype.
Finally, we have provided the last two lines to test your constructor and
sayName
method. Don't change these!
Here is my code:
// create your Animal class here
function Animal(name, numLegs)={
this.name=name;
this.numLegs=numLegs;
this.sayName=function(){
console.log("Hi, my name is "+this.name);
}
}
// create the sayName method for Animal
// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();
I get a syntax error. What gives?