如何在 JavaScript 中创建可变参数(具有可变长度参数列表)函数包装器

发布于 2024-08-26 09:54:56 字数 1252 浏览 6 评论 0 原文

目的是构建一个包装器,以提供在各种脚本主机上调用具有可变数量的本机函数的一致方法 - 以便脚本可以在浏览器以及 Windows 脚本主机或其他脚本引擎中执行。
我知道 3 种方法,每种方法都有其自身的缺点。

  1. eval() 方法:

    函数包装器 () {
        var str = '';
        for (var i=0; i
  2. switch() 方法:

    函数包装器 () {
        开关(参数.长度){
            案例0:
                返回[native_function](参数[0]);
                休息;
            案例1:
                返回[native_function](参数[0],参数[1]);
                休息;
            ...
            案例编号:
                返回[native_function](参数[0],参数[1],...参数[n]);
            }
        }
    
  3. apply()方法:

    函数包装器 () {
        return [native_function].apply([native_function_namespace], 参数);
        }
    

你问它们有什么问题吗?

  1. 那么,我们是否应该深入研究一下 eval() 邪恶的所有原因?还有所有的字符串连接......这不是一个可以被标记为“优雅”的解决方案。

  2. 人们永远无法知道最大n,因此无法知道要准备多少案例。这也会将脚本拉伸到巨大的比例,并违反神圣的DRY原则。

  3. 该脚本可以在不支持 apply() 方法的旧版(JavaScript 1.3 / ECMA-262-3 之前)引擎上执行。

现在的问题是:还有其他解决方案吗?

The intention is to build a wrapper to provide a consistent method of calling native functions with variable arity on various script hosts - so that the script could be executed in a browser as well as in the Windows Script Host or other script engines.
I am aware of 3 methods of which each one has its own drawbacks.

  1. eval() method:

    function wrapper () {
        var str = '';
        for (var i=0; i<arguments.lenght; i++)
            str += (str ?', ':'') + ',arguments['+i+']';
        return eval('[native_function] ('+str+')');
        }
    
  2. switch() method:

    function wrapper () {
        switch (arguments.lenght) {
            case 0:
                return [native_function] (arguments[0]);
                break;
            case 1:
                return [native_function] (arguments[0], arguments[1]);
                break;
            ...
            case n:
                return [native_function] (arguments[0], arguments[1], ... arguments[n]);
            }
        }
    
  3. apply() method:

    function wrapper () {
        return [native_function].apply([native_function_namespace], arguments);
        }
    

What's wrong with them you ask?

  1. Well, shall we delve into all the reasons why eval() is evil? And also all the string concatenation... Not a solution to be labeled "elegant".

  2. One can never know the maximum n and thus how many cases to prepare. This also would strech the script to immense proportions and sin against the holy DRY principle.

  3. The script could get executed on older (pre- JavaScript 1.3 / ECMA-262-3) engines that don't support the apply() method.

Now the question part: is there any another solution out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-09-02 09:54:56

只需使用apply()即可。对于您的过时的执行引擎,只需这样做

if ( 'undefined' == typeof Function.prototype.apply )
{
  Function.prototype.apply = function( context, args )
  {
    // whatever hacky way you want to implement it - i guess eval.
  }
}

Just use apply(). And for your antiquated execution engines, just do this

if ( 'undefined' == typeof Function.prototype.apply )
{
  Function.prototype.apply = function( context, args )
  {
    // whatever hacky way you want to implement it - i guess eval.
  }
}
如若梦似彩虹 2024-09-02 09:54:56

就像自动的“this”变量一样,有一个“arguments”变量保存传递给函数的所有参数。请参阅 javascript 变量

Just like the automatic 'this' variable, there is an 'arguments' variable that holds all the arguments passed in to the function. See javascript variadic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文