bla0009 bla0009 - 1 month ago 14
Javascript Question

Uncaught TypeError not a function - javascript class with prototyping

I am getting not a function error from javascript.

This is part of my object that I define:

Product.Config.prototype.configureSubscribe = function(funct)
{
this.configureObservers.push(funct);
};


After that I create another object:

Product.ConfigurableSwatches = Class.create();
Product.ConfigurableSwatches.prototype = {
initialize: function(productConfig, config) {
// redefine some default options if configured
if (config && typeof(config) == 'object') {
this.setConfig(config);
}
this.productConfig = productConfig;
// Store configurable attribute data
var attributes = [];
for (var i in productConfig.config.attributes) {
attributes.push(productConfig.config.attributes[i]);
}
this.configurableAttributes = attributes;
// Run it
this.run();
return this;
},
run: function() {
// HERE IT BREAKS
this.productConfig.configureSubscribe(this.onSelectChange.bind(this));
}
}


I am getting following error:

Uncaught TypeError: this.productConfig.configureSubscribe is not a function


Can someone advice what is the problem?

Answer

The line this.productConfig = productConfig should be

this.productConfig = new productConfig()

to create a new Object with its prototypes