I am attempting to build a plugin system where I do not know the plugins beforehand. Initialising an object using 'require' from browserify. As follows:
class.coffee:
class MyClass
name: "my-class"
constructor: ->
@getName: ->
return @name
exports.plugin = new MyClass
Then from the calling file I have:
pluginName = # from a config file
{ plugin } = require './#{pluginName}.coffee'
console.log plugin
console.log plugin.getName()
The first logger call gives me:
MyClass{ name="my-class" }
The second one fails however with plugin.getName is not a function.
Any help/guidance appreciated. I'm not a JS developer and am new to coffeescript/node.js as well.
Thanks.
You should not have the @ before getName.
Having the @ is equivalent to this in JS
MyClass.getName = function(){
return this.name;
};
But in this case, getName is a function on the class itself, not the MyClass instance.
Without the @, like this, getName: -> the JS is like this:
MyClass.prototype.getName = function(){
return this.name;
}