Javascript:参数数组是否已弃用?

发布于 2024-12-15 03:43:37 字数 570 浏览 3 评论 0原文

大多数网站都说 callee 作为 Function.arguments 的属性已被弃用。但有些网站更进一步,说整个 Functions.argument 已被弃用,例如 https://web.archive.org/web/20130313045745/http://aptana.com/reference/api/Arguments.html 。如果整个例程都死了,为什么只提到被调用者呢?我刚刚发现了arguments,它似乎非常有用,例如:http://hungred.com/how-to/secret-arguments-array-javascript/

Most sites say callee as a property of Function.arguments is deprecated. But some sites go further and say the whole of Functions.argument is deprecated E.g. https://web.archive.org/web/20130313045745/http://aptana.com/reference/api/Arguments.html . Why only mention callee if the whole routine is dead in the water? I only just discovered arguments and it seems incredibly useful E.g: http://hungred.com/how-to/secret-arguments-array-javascript/

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

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

发布评论

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

评论(5

凝望流年 2024-12-22 03:43:37

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

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

Function.arguments is deprecated, but it's only deprecated in favor of the vanilla arguments object that's available within a function. (e.g. using x = arguments[i]; instead of x = theFunc.arguments[i];)

That's now the preferred (and as you say, extremely useful) method for accessing the ordinal arguments received.

ヅ她的身影、若隐若现 2024-12-22 03:43:37

Afaik arguments 作为 Function 的属性已被弃用。。请参阅此 MDN 链接,或这个

Afaik arguments is deprecated as a property of Function. See this MDN-link, or this one

喜爱皱眉﹌ 2024-12-22 03:43:37

Javascript “参数数组” — 不是已弃用,但我想说截至 2021 年已过时:

“arguments”关键字是保留关键字,不能用作参数名称或在函数体内用作局部变量。此功能旨在允许在调用函数时使用任意数量的参数,而无需在函数定义中指定任意数量的参数。它也是一种过时的利用多个参数的方法,并且比现代的剩余参数方法效率较低。

以下是显示首选实践的示例:

function eat(...food) {
  for (let item in food) {
    console.log("eating " + item);
  } 
  // log the rest parameter
  console.log(food);
}
eat("eggs", "toast", "bacon");
// => eating eggs
// => eating toast
// => eating bacon
// => [ 'eggs', 'toast', 'bacon' ]

上面的引用和代码示例来自标题为 JavaScript 参数和参数

The Javascript "argument array" — not deprecated, but I would say outdated as of 2021:

The “arguments” keyword is reserved and cannot be used as a parameter name or within the function body as a local variable. This feature was designed to allow an arbitrary amount of arguments when invoking a function without having to specify an arbitrary amount of parameters in the function definition. It is also an outdated way to utilize multiple arguments and is less efficient than the modern rest parameters method.

Here is an example showing preferred practice:

function eat(...food) {
  for (let item in food) {
    console.log("eating " + item);
  } 
  // log the rest parameter
  console.log(food);
}
eat("eggs", "toast", "bacon");
// => eating eggs
// => eating toast
// => eating bacon
// => [ 'eggs', 'toast', 'bacon' ]

The above quote and code sample are from an article titled JavaScript Parameters and Arguments

撩心不撩汉 2024-12-22 03:43:37

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

大多数时候我在开发 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

callee is deprecated, but the arguments is used in many applications. I don't know if arguments is deprecated. You can use it to get all the parameters of a function, even if there not defined within function( params ).

Most of the time I used when I develop a jQuery plugin. Something like:

$.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' );
    }    
  };

As you can see only method is passed as a parameter, but within the first if the arguments is split after the first value. This way you can pass a function name, and all parameters used by this function.

Full example: http://docs.jquery.com/Plugins/Authoring

万劫不复 2024-12-22 03:43:37

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

No, the arguments array is not deprecated in the latest 5.1 version of the specification (see page 60). The caller object will however only be available if the code is not in strict mode.

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