Reputation: 2107
Here is my code.
It throws when creating new instance of inherited map (CustomMap).
It seems that the init
function is not available in the base constructor call when calling namespace.Map.apply(this, mapContainer);
https://jsfiddle.net/y6qg4xbn/
var namespace = window.namespace || {};
namespace.Map = function (mapContainer) {
this.mapContainer = mapContainer;
this.preinit();
this.init();
}
namespace.Map.prototype.preinit = function () {
};
namespace.Map.prototype.init = function () {
};
namespace.CustomMap = function (mapContainer) {
namespace.Map.apply(this, mapContainer);
};
namespace.CustomMap.prototype = Object.create(namespace.Map);
namespace.CustomMap.prototype.preinit = function () {
// do some stuff or even call base preinit
}
var newMap = new namespace.Map();
var inheritedMap = new namespace.CustomMap();
Upvotes: 2
Views: 58
Reputation: 707238
Change this:
namespace.CustomMap.prototype = Object.create(namespace.Map);
to this:
namespace.CustomMap.prototype = Object.create(namespace.Map.prototype);
You should base the CustomMap.prototype
on the Map.prototype
, not on Map
. There was no .init()
method in the base because you didn't include the base prototype when creating the derived prototype.
And, there's a separate problem where you need to change this:
namespace.Map.apply(this, mapContainer);
to this:
namespace.Map.call(this, mapContainer);
because mapContainer
is a specific variable, not an array of arguments which means you should use .call()
instead of .apply()
.
Upvotes: 1