简体   繁体   English

通过方法引用在类上调用方法

[英]Call method on class by method reference

I have a reference called func that links to a static method called method , and when I call func() , the method cannot find the static method _open . 我有一个称为func的引用,该引用链接到一个称为method的静态方法,并且当我调用func() ,该方法找不到静态方法_open I am not sure why, but I assume it is because func is a method and I am calling it as such. 我不确定为什么,但是我认为是因为func是一种方法,因此我将其称为。

It looks something like this: 看起来像这样:

class A {
  static method() {
    this._open()
  }
  static _open() {
    // Do stuff
  }
}

I have tried to call the method like this: 我试图调用这种方法:

/**
 * Finds the data in a object/array if it exists
 * `a.b.c` -> `{a: {b: {c: 'some value'}}}`
 */
function find(query, data) {
  return query.split('.').reduce((obj, val) => {
    return obj ? obj[val] : obj
  }, data)
}

// func is a reference to `method`
let func = find('A.method', {A: A})
func.constructor.prototype[func](...params)

However, it gives me this error: 但是,它给了我这个错误:

TypeError: func.constructor.prototype[func] is not a function TypeError:func.constructor.prototype [func]不是函数

When logging func it looks like this: 在记录func它看起来像这样:

console.log(func.toString())
// Output:
// method() {
//   this._open()
// }

How can I call a static method on a class with only the reference to the method? 如何在仅引用方法的类上调用静态方法?

You need to ensure that find returns the method, as bound to A, otherwise, when method is called, this will refer to the global this ( window or undefined), and then you can just call func() : 你需要确保find返回的方法,如绑定到一个,否则,当method被调用时, this将涉及全球thiswindow或不确定的),然后你可以调用func()

 class A { static method(...args) { console.log('args', args); this._open() } static _open() { console.log('opening'); } } const find = () => A.method.bind(A); const func = find('method'); func('abc', 'def'); 

If you only have a reference to func , and not to the class A, there's no way to get to A again. 如果您仅引用func ,而不引用A类,则无法再次访问A。 Referencing func.constructor would only make sense if func was an instance (in which case referencing its .constructor would take you to A ) - but func is a method of the class, not an instance of the class. 仅当func实例 (在这种情况下,引用其.constructor会将您带到A )时,引用func.constructor才有意义-但是func是类的方法,而不是类的实例。

If you need to dynamically detect what the last object before the return value from find is, so you can bind the function if necessary, add a bit more logic to the find function, to get to the last object and the last key: 如果需要动态检测find的返回值之前的最后一个对象是什么,那么可以在必要时绑定该函数,向find函数添加更多的逻辑,以获取最后一个对象和最后一个键:

 class A { static method() { this._open() } static _open() { console.log('opening'); } } function find(query, data) { const keys = query.split('.'); const lastKey = keys.pop(); const lastObj = keys.reduce((obj, val) => { return obj ? obj[val] : obj }, data); const ret = lastObj[lastKey]; return typeof ret === 'function' ? ret.bind(lastObj) : ret; } // func is a reference to `method` let func = find('A.method', { A: A }); func('param 1', 'param 2') 

Not really sure, what you are doing this for: 不太确定,您要为此做什么:

 class A { static method() { return this._open(...arguments) } static _open() { return arguments; // Do stuff } } function find(classHere, method){ return classHere[method].bind(classHere); } var func = find(A, 'method'); console.log(func('test', 'is', 'this', 'what', 'you', 'want')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM