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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.