简体   繁体   English

如何在 JavaScript 中调用同一个类的多个函数?

[英]how to call multiple function of same class in JavaScript?

I am declaring a class Response with multiple instance method of this class like the code below but I am getting an error throwing from the output: Error: Cannot read properties of undefined (reading 'payload') .我正在使用此类的多个实例方法声明一个类 Response,如下面的代码,但我从输出中抛出错误: Error: Cannot read properties of undefined (reading 'payload') I want to get results from what I am calling to get.我想从我打电话得到的结果中得到结果。 How can I do it?我该怎么做?

 class Response { output({ message = null, payload = null, data = null }) { return { message, payload, data } } message(message) { this.output({ message }) } payload(payload) { this.output({ payload }) } data(data) { this.output({ data }) } } const response = new Response(); console.log( response.message('my message').payload('my payload').data('my data') ); // expected result: { message: 'my message', payload: 'my payload', data: 'my data' } console.log( response.message('my message').payload('my payload') ); // expected result: { message: 'my message', payload: 'my payload', data: null } console.log( response.payload('my payload') ); // expected result: { message: null, payload: 'my payload', data: null }

From what I can understand, you are trying to make chainable methods (similar to what the String class does.)据我所知,您正在尝试制作可链接的方法(类似于 String 类所做的。)

To make chainable methods, you have almost done the right thing, except in the output method, you return a plain object.要制作可链接的方法,您几乎做对了,除了在output方法中,您返回一个普通对象。 This isn't right because then it just returns an object and not an instance of the Response class .这是不对的,因为它只返回一个对象而不是Response类的实例

So, the output method should return something like:因此, output方法应该返回如下内容:

output({
    message = null,
    payload = null,
    data = null
  }) {
    return new Response({
      message,
      payload,
      data
    })
  }

Additionally, you should also have a constructor that accepts the required parameters and stores them in a global object (like a state variable).此外,您还应该有一个接受所需参数并将它们存储在全局对象(如状态变量)中的构造函数。

That would make your class look like:那会让你的班级看起来像:

class Response {

  state = {
    message: null,
    payload: null,
    data: null
  }
  
  constructor(params) {
    // be careful, params can also be null when calling the constructor with no arguments. 
    // handle it accordingly
    this.state = params
  }

  output({
    message = null,
    payload = null,
    data = null
  }) {
    return new Response({
      message,
      payload,
      data
    })
  }

  message(message) {
    this.output({
      message
    })
  }

  payload(payload) {
    this.output({
      payload
    })
  }

  data(data) {
    this.output({
      data
    })
  }

}

Additionally, to make it even more efficient, you can replace the new Response() call in the output function to be:此外,为了使其更加高效,您可以将output函数中的new Response()调用替换为:

output({
    message = null,
    payload = null,
    data = null
  }) {
    this.state = {
       message,
       payload,
       data
    }
    return this // this returns an instance of the class
}

This covers your back if you should change the class name in the future, you need not come back to the output function and change the return new Response() to return new NewClass() everytime.如果您将来要更改类名,这将覆盖您的背部,您无需返回output函数并将return new Response()更改为return new NewClass()每次。

Hope it helped!希望它有所帮助!

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

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