0

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.

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'd been playing around with that and obviously left it the wrong way around. I've marked your answer as accepted answer, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.