Javascript 按方法引用对类调用方法

Javascript 按方法引用对类调用方法,javascript,Javascript,我有一个名为func的引用,它链接到一个名为method的静态方法,当我调用func()时,该方法找不到静态方法\u open。我不知道为什么,但我认为这是因为func是一个方法,我这样调用它 它看起来像这样: class A { static method() { this._open() } static _open() { // Do stuff } } /** * Finds the data in a object/array if it exist

我有一个名为
func
的引用,它链接到一个名为
method
的静态方法,当我调用
func()
时,该方法找不到静态方法
\u open
。我不知道为什么,但我认为这是因为
func
是一个方法,我这样调用它

它看起来像这样:

class A {
  static method() {
    this._open()
  }
  static _open() {
    // Do stuff
  }
}
/**
 * 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)
console.log(func.toString())
// Output:
// method() {
//   this._open()
// }

我尝试过这样调用该方法:

class A {
  static method() {
    this._open()
  }
  static _open() {
    // Do stuff
  }
}
/**
 * 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)
console.log(func.toString())
// Output:
// method() {
//   this._open()
// }

但是,它给了我这个错误:

TypeError:func.constructor.prototype[func]不是函数

记录
func
时,如下所示:

class A {
  static method() {
    this._open()
  }
  static _open() {
    // Do stuff
  }
}
/**
 * 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)
console.log(func.toString())
// Output:
// method() {
//   this._open()
// }


如何对只引用该方法的类调用静态方法?

您需要确保
find
返回绑定到a的方法,否则,当调用
method
时,
this
将引用全局
this
窗口
或未定义),然后您可以只调用
func()

A类{
静态方法(…args){
console.log('args',args);
这个
}
静态_open(){
控制台日志(“打开”);
}
}
const find=()=>A.method.bind(A);
const func=find('method');

func('abc','def')
您需要确保
find
返回绑定到A的方法,否则,当调用
method
时,
this
将引用全局
this
窗口
或未定义),然后您可以调用
func()

A类{
静态方法(…args){
console.log('args',args);
这个
}
静态_open(){
控制台日志(“打开”);
}
}
const find=()=>A.method.bind(A);
const func=find('method');

func('abc','def')不太确定,您这样做是为了什么:

A类{
静态方法(){
返回此参数。_open(…参数)
}
静态_open(){
返回参数;
//做事
}
}
函数查找(classHere,method){
返回classHere[method].bind(classHere);
}
var func=查找(一种“方法”);

log(func('test','is','this','what','you','want')不太确定,您这样做是为了什么:

A类{
静态方法(){
返回此参数。_open(…参数)
}
静态_open(){
返回参数;
//做事
}
}
函数查找(classHere,method){
返回classHere[method].bind(classHere);
}
var func=查找(一种“方法”);

log(func('test','is','this','what','you','want')
你不应该总是使用
A.\u open()
而不是
this.\u open()
调用静态方法\u open吗?你不需要这样做,因为它们都是静态的
A.method.constructor
函数
不是
A
。如果只引用了
方法
,那么如果没有对
方法的引用
,就没有任何东西可以将
方法
连接到它。它不“知道”它是一个方法。你不应该总是用
a.\u open()
而不是
这个来调用静态方法\u open。\u open()
?你不需要,因为它们都是静态的
a.method。构造函数
函数
不是
a
。如果只引用了
方法
,那么如果没有对
方法的引用
,就没有任何东西可以将
方法
连接到它。它不“知道”这是一个方法。我已经添加了我的
find
函数,如果这是一个帮助的话。所以我想我没有直接的参考资料。太棒了!我已经在我自己的项目工作了!我已经添加了我的
find
函数,如果这是一个帮助的话。所以我想我没有直接的参考资料。太棒了!我已经在我自己的项目工作了!