【问题标题】:Javascript: is the arguments array deprecated?Javascript:参数数组是否已弃用?
【发布时间】:2011-12-28 14:32:27
【问题描述】:

大多数网站都说“被调用者”作为 Function.arguments 的属性已被弃用。但是有些网站走得更远,说整个 Functions.argument 已被弃用,例如http://aptana.com/reference/api/Arguments.html 如果整个例程都死在水中,为什么只提到被调用者?我刚刚发现“论点”,它似乎非常有用例如:http://hungred.com/how-to/secret-arguments-array-javascript/

【问题讨论】:

    标签: javascript arguments optional optional-arguments


    【解决方案1】:

    Function.arguments 已被弃用,但它仅被弃用,以支持函数中可用的 vanilla arguments 对象。 (例如,使用x = arguments[i]; 而不是x = theFunc.arguments[i];

    现在这是访问收到的序数参数的首选方法(正如您所说,非常有用)。

    【讨论】:

    • 这是有道理的。如果 Javascript 想要一些多线程支持,那么这种全局有状态对象就会被扼杀。
    • 为什么 Function.arguments 被弃用了?它允许您直接访问嵌套函数内的父函数的参数,这是 arguments 对象无法做到的。
    • 我在 stackoverflow.com/a/49544338/539149 发布了针对 Node.js 和浏览器的解决方法 getArguments() 函数,因为 stackoverflow.com/a/49544338/539149 缺少功能
    【解决方案2】:

    callee 已被弃用,但参数在许多应用程序中使用。我不知道参数是否已被弃用。您可以使用它来获取函数的所有参数,即使没有在函数(参数)中定义。

    我大部分时间都是在开发 jQuery 插件时使用的。类似的东西:

    $.fn.tooltip = function( method ) {
        if ( methods[method] ) {
          return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }    
      };
    

    如您所见,只有方法作为参数传递,但如果参数在第一个值之后拆分,则在第一个值内。这样你就可以传递一个函数名,以及这个函数使用的所有参数。

    完整示例:http://docs.jquery.com/Plugins/Authoring

    【讨论】:

      【解决方案3】:

      不,参数数组在最新的 5.1 版规范中没有被弃用(参见第 60 页)。但是,caller 对象仅在代码未处于严格模式时才可用。

      【讨论】:

        【解决方案4】:

        Afaik arguments 已弃用作为 Function 的属性。见this MDN-link,或this one

        【讨论】:

          猜你喜欢
          • 2014-08-10
          • 2014-04-11
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 2021-01-06
          相关资源
          最近更新 更多