Search code examples
ecmascript-6getter-setteraurelia

es6 accessor for export class function


I started working with Aurelia with ES7 and I can't figure out how to make a function public when it needs to have an argument list.

This works:

export class dummy{
  get doSomething(){
    return "something";
  }
}

dummy.doSomething()

BUT if I modify the function to have an argument list, I get an error:

get doSomething(x){
    ...
ERR: A 'get' accessor cannot have parameters.

I tried a variety of things that didn't work and Googling it is coming up with nothing. How do I declare a public function in an exported class that accepts an argument and returns a value?

Thanks.


Solution

  • get declares a getter. They are accessed like normal properties (i.e. not methods):

    var foo = instance.doSomething;
    

    That's why getters cannot have parameters.

    If you don't want that, but want a method instead, remove it:

    export class dummy{
      doSomething(x){
        return "something";
      }
    }
    

    If you merely use the class as a "method bag", i.e. you are not planning to create multiple instances of it, use an object instead:

    export var dummy = {
      doSomething(x){
        return "something";
      }
    };