To structure my JavaScript code I would like to use classes, the syntactical sugar for prototype inheritance new to ES6. I am having trouble though: it seems my prototype methods are not working as expected, which makes me feel I may have a fundamental misunderstanding of JavaScript classes, and/or how they work.
Take the following code, which declares a traditional constructor Cat
and modifies its prototype, and an ES6 class Dog
with a prototype method defined within it.
// Traditional Cat Constructor
// ===========================
function Cat(name) {
this.name = name;
this.sound = "Meow.";
}
Cat.prototype.speak = function() {
console.log(this.sound || "I don't make a sound.");
};
// Dog via ES6 Class
// =================
class Dog {
constructor(name) {
this.name = name;
this.sound = "Woof!";
}
speak() {
console.log(this.sound);
}
}
var cat = new Cat("Bella");
var dog = new Dog("Sparky");
In NodeJS (tested with 4.4.2 and 6.0.0-pre), when I try to get the prototype of an instance of Dog
, I get an empty object, but I am still able to call the prototype method on the instance.
Object.getPrototypeOf(cat);
// -> Cat { speak: [Function] }
Object.getPrototypeOf(dog);
// -> Dog {}
cat.speak();
// -> Meow.
dog.speak();
// -> Woof!
In Google Chrome, where I expect the result to be the same (since Node uses its engine), I instead get the expected result:
Object.getPrototypeOf(cat);
// -> Object {}
// -> constructor: function Cat(name)
// -> speak: function()
// -> __proto__: Object
Object.getPrototypeOf(dog);
// -> Object {}
// -> constructor: function(name)
// -> speak: function()
// -> __proto__: Object
I was getting an error in Node that said the prototype methods I defined in a class were not functions of instances of their classes, which is the only reason I noticed this, but unfortunately now it seems I cannot reproduce that issue.
Regardless, the above does not seams right, is there any explanation for why this code is behaving in this way?