JavaScript 中函数类的默认处理程序 [英] Default handler for a functional class in JavaScript

查看:26
本文介绍了JavaScript 中函数类的默认处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以让函数类具有默认调用方法.让我详细说明一下.

I wanted to know if it is possible to have a functional class have a default call method. Let me elaborate.

function Foo(){
  /*some logic*/
  Foo.prototype.aletUser = function alertUser(message) {
    alert(message);
  }
}

假设上面的代码是我的函数类(如果我对上面结构的命名有误请告诉我),并且我实例化了这个结构之一.

Assume that the code above is my functional class (if my naming for the above structure is wrong please tell me), and i instantiated one of this structure.

let param = new Foo();

现在,我希望调用以下代码并希望它调用我的原型 Foo.alertUser

Now, i wish to call the following and hope for it to call my prototype Foo.alertUser

param('Hey There')

如何实现?提前致谢.

我的方法背后的更多见解

我正在使用 React Native,我希望 param('Hey There') 在函数内部使用一个钩子,但我没有想法,我知道如何让它与 JavaScript 一起工作类,但遗憾的是无法在类组件内部调用钩子.

I am working with React Native and i wish to have param('Hey There') use a hook inside the function but i am out of ideas, i know how to make it work with JavaScript classes, but unfortunately hooks can not be called inside class components.

推荐答案

回答您实际提出的问题,这是可能的,但可能不是一个好主意.一种方法是让 new Foo 创建一个函数,然后改变它的原型(这通常不是一个好主意),像这样:

Answering the question you actually asked, it's possible but probably not a good idea. One way to do it is to have new Foo create a function and then change its prototype (which is normally not a good idea), like this:

function Foo() {
    // Create a function, the change its prototype to
    // `Foo.prototype` and return it
    const retval = function alertUser(param) {
        console.log(param);
    };
    Object.setPrototypeOf(retval, Foo.prototype);
    return retval;
}
// Make `Foo.prototype` use `Function.prototype` as its prototype
Foo.prototype = Object.create(Function.prototype);
Foo.prototype.constructor = Foo;


const param = new Foo();
param("Hi there");

但是,同样,这可能不是一个好主意,原因如下:

But, again, that's probably not a good idea, for a couple of reasons:

  • 更改对象的原型可能会破坏 JavaScript 引擎对其的实现.现在,你用它来做什么可能并不重要,这取决于它是否是关键路径(以及去优化的明显程度),所以它当然是一个选择.

  • Changing an object's prototype can deoptimize the JavaScript engine's implementation of it. Now, it may not matter for what you're using this for, it depends on whether it's critical path (and the degree that the deoptimization is noticeable), so it's certainly an option.

您已经说过您正在尝试这样做,以便您可以在类组件中使用钩子.正如我在评论中所说的那样,就是可能行不通,即使行得通,也可能不是您的最佳解决方案

You've said you're trying to do this so you can use a hook in a class component. As I said in a comment, that's probably not going to work and even if it does, it's probably not your best solution

我想退一步说,如果您发现自己想要从类组件调用钩子,那么您最好的选择是:

I would step back and say if you find yourself wanting to call a hook from a class component, your best bet is to either:

  1. 构建与钩子等效的基于类的功能,不要使用钩子,或者

  1. Build class-based functionality equivalent to the hook and don't use the hook, or

将类组件更改为功能组件,以便您可以正常使用钩子

Change the class component to a functional component so you can use the hook in the normal way

这篇关于JavaScript 中函数类的默认处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆