javascript - Adding a method to a Class protoype, error -
quick question adding method object.
why errors back? checked syntax , seem correct. new javascript.
// create animal class here function animal(name, numlegs) { this.name = name; this.numlegs = numlegs; } // create sayname method animal animal.prototype.sayname = function() { console.log("hi name " + this.name); }; // test var penguin = new animal("captain cook", 2); penguin.sayname();
i error when trying run code
typeerror: object #<animal> has no method 'sayname'
yeah, because you've declared method sayname
small n
letter.
javascript case sensitive language.
Comments
Post a Comment