javascript在需要该类对象中的另一个函数的类中传递函数

javascript在需要该类对象中的另一个函数的类中传递函数,javascript,function,parameter-passing,Javascript,Function,Parameter Passing,我正在将类或var对象中的函数作为参数传递给另一个函数。 从类中获取函数的函数执行该函数。 它可以正常工作,但是该类的函数调用该类的另一个函数。 控制台输出在类函数中调用的函数未定义的错误 下面可能会更好地说明这一点 //the class variable in someClass.js function(params...){ getSomethingInClass: function(){ // return some variable } functionThat

我正在将类或var对象中的函数作为参数传递给另一个函数。 从类中获取函数的函数执行该函数。 它可以正常工作,但是该类的函数调用该类的另一个函数。 控制台输出在类函数中调用的函数未定义的错误

下面可能会更好地说明这一点

//the class variable in someClass.js
function(params...){
  getSomethingInClass: function(){
     // return some variable
  }

  functionThatIsPassed: function(arg){
    var theCalledFunction = getSomethingInClass();
    //do something with theCalledFunction
  }
}

//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
  FunctionThatTakesFunction(this.someClassVar.functionThatIsPassed);
}


//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(callbackFun){
  callbackFun(someArg);
}
如果我将其更改为传递整个对象someClass对象,则上述操作将起作用。传递对象是一种糟糕的编程实践,因为接受函数的函数需要知道其参数的函数 比如说

//THIS WORKS!
//other stuff is same 

//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
  FunctionThatTakesFunction(this.someClassVar);
}


//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(object){
  object.functionThatIsPassed(someArg);
}

下面是将一个函数传递给另一个函数的一些示例:(这里是Fiddle:)

我使用闭包:

class Sample() {
    constructor() {
        this.id = 'Sample';
    }

    method(text) {
        console.log(this.id + text)
    }

    getMethod() {
        const ref = this;
        return function(text) {
            ref.method(text);
        }
    }
}
其他地方:

someFunc() {
    const sample = new Sample();
    useFunc(sample.getMethod());
}

useFunc(func) {
    func('HI');
}
输出:
SampleHI

您的问题是什么?我只想从类中传递函数并调用它,而不是传递类对象并在该函数之后调用函数,该函数接受函数(this.someClassVar.bind(this));
someFunc() {
    const sample = new Sample();
    useFunc(sample.getMethod());
}

useFunc(func) {
    func('HI');
}