skip to Main Content

I’ve been around javascript for a long time, but am pretty rusty with javascript Classes. For a particular project I’m working on, things are starting to get unwieldy and I’d like to start cutting up my code into separate files. I don’t want to have to import all my functions explicitly, so I’ve started wrapping them up into a Class (which makes sense for other reasons also). Some of my functions return callbacks (functions). That is:

return functionName;

Which I can use like:

var callBack = functionReturningFunction();
...
callBack();

I cannot find any information about returning and passing methods, rather than functions what is the syntax there? Ideally I’d like to pass in methods as arguments to the constructor so I can say:

const myObject = new MyClass(methodName);

So I don’t have to do:

const myObject = new MyClass();
myObject.methodName();

2

Answers


  1. Chosen as BEST ANSWER

    The stackoverflow fairy brought me the answer as I was finishing typing out the question. Following the pattern in my question, I have:

    return this.methodName;
    

    Which I can use like:

    var callBack = myObject.methodReturningMethod();
    ...
    callBack();
    

    From outside my Class. Importantly I should have:

    methodReturningMethod(){
     ...
     return this.aMethod.bind(this);
    }
    

    inside methodReturningMethod() if aMethod() accesses any object properties because javascript is * special * like that (and will get all confused if I don't).

    Inside class methods I can have stuff like:

    this.callBackMethod = this.methodReturningMethod();
    ...
    this.callBackMethod();
    

    which will call whichever method this.methodReturningMethod() returns.

    I don't think its possible to specify an object method as a parameter to that object's constructor because object methods don't exist before the object has been constructed. Shout out if you know a way. After the object has been created I can do things like:

    myObject.methodReceivingMethod(myObject.method);
    

  2. Just basic common info for your:

    I don’t want to have to import all my functions explicitly

    You don’t need class to provide them in NamesSpaces

    You can create file helper.js:

    export function one() {}
    export function two() {}
    export function three() {}
    

    and then basicly include them from:

    import * as Helper from "./helper.js"
    Helper.one()
    Helper.two()
    Helper.three()
    
    1. If you like Classes and want to create new isntance, then you should know about "static" feature.
    export class Helper {
      static one() {}
      static two() {}
      static three() {}
    }
    

    and then …

    import { Helper } from "./helper.js"
    Helper.one()
    Helper.two()
    Helper.three()
    
    1. Probably if you can tell more about your case I can figure our more ways to solve it.

    Good Luck with your project! 😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search