RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 848546
Accepted
Kir_Antipov
Kir_Antipov
Asked:2020-06-30 18:21:44 +0000 UTC2020-06-30 18:21:44 +0000 UTC 2020-06-30 18:21:44 +0000 UTC

是否可以使用不同数量的参数创建多个 JS 类方法重载?

  • 772

这似乎是一个平庸的问题,但我遇到了一个事实,我不知道如何摆脱这种情况:假设我们有这样一个“类”:

function TestClass() {
    this.method = function(index) {
        alert("index");
    };
    this.method = function(x, y) {
        alert("x, y");
    };
}

让我解释一下我想要做什么:

var test = new TestClass();
test.method(1); // Вылезает alert с текстом "index"
test.method(1, 1); // Вылезает alert с текстом "x, y"

也就是说,我正在尝试创建一个具有多个重载的内部方法。很明显,上面的语法只是简单地覆盖了这个即兴类引用的函数。试图这样做:

function TestClass() {
    function method(index) {
        alert("index");
    }
    function method(x, y) {
        alert("x, y");
    }
    this.method = method;
}

但是,仍然使用TestClass().method(...)最后指定的重载。

也许这个问题已经被问过了,但我无法谷歌,所以请指导我或分享解决此问题所需的知识)

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Mike Papou
    2020-06-30T18:36:27Z2020-06-30T18:36:27Z

    你不必做重载。在 JS 的任何函数中,arguments 变量都是可用的,它包含一个带有传递参数的类数组对象。也就是说,您可以检查 arguments.length 并通过索引访问参数,就像在数组中一样。

    • 2
  2. Best Answer
    Diskyp
    2020-06-30T18:53:54Z2020-06-30T18:53:54Z

    js 中的方法名必须是唯一的,或者你可以即兴重载:

    let qwe=new class{
      method(...args){
        let overloads={
          1:()=>alert('Single argument: '+args[0]),
          2:()=>alert('Two arguments: '+args[0]+' and '+args[1])
        }[args.length]()
      }
    }()
    qwe.method('Celestia')
    qwe.method('Celestia','Rarity')

    • 1
  3. Qwertiy
    2020-06-30T20:18:59Z2020-06-30T20:18:59Z

    function TestClass() {
    }
    
    TestClass.prototype.method = function(a, b) {
      if (b === undefined) {
        var i = a;
        console.log("index:", i);
      } else {
        var x = a, y = b;
        console.log("xy:", x, y);
      }
    };
    
    var test = new TestClass();
    test.method(1);
    test.method(1, 1);

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5