Reputation: 287
My code is like following:
function Example(){}
Example.myMethod = function(){};
function SpecificExample(){}
SpecificExample.prototype = Object.create(Example.prototype);
SpecificExample.prototype.constructor = SpecificExample;
I want SpecificExample
extends Example
. Why this code doesn't work?
Upvotes: 1
Views: 38
Reputation: 17340
Example.myMethod is a function attached to the constructor function/object, but it is not part of its prototype. This could be called a static function (and in ES6 it is exactly that). To make them part of your prototype, add them to the prototype itself:
function Example(){}
Example.prototype.myMethod = function(){};
function SpecificExample(){}
SpecificExample.prototype = Object.create(Example.prototype);
// As @JaredSmith said, you shouldn't change the constructor, and why would you?
// function SpecificExample(){} IS your constructor. So...
// SpecificExample.prototype.constructor = SpecificExample;
// should simply be removed.
Upvotes: 2