如果我叫一个JS方法,更多的参数比它定义为接受,会发生什么? [英] What happens if I call a JS method with more parameters than it is defined to accept?

查看:157
本文介绍了如果我叫一个JS方法,更多的参数比它定义为接受,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这两个定期所有功能于的家庭JS开发者(我)定义的功能,以及predefined DOM方法。就像如果我尝试调用时会发生什么IE的的attachEvent 与签名的WHATWG的的addEventListener ?例如,

elem.attachEvent('onbillgates \\'妈妈',函数(E){this.mount();},FALSE);

特别要注意的 的。请问这一趟什么都涨,即使的attachEvent 方法的签名只要求两个参数?

感谢。

 函数foo(FirstOf2,SecondOf2){
  的console.log(FirstOf2 + SecondOf2);
}富(1,2,真);


解决方案

JavaScript并没有一个固定的参数列表的概念。对于自己的函数,只要你想,你总是可以指定任意数量的参数,并传递,只要你想它曾经输入你想要的。

有关的内置功能,关联到本地code,的这取决于

您问什么它取决于:

让我们看一下 ECMA-262

约15节的内置的(不要与混淆的主机的)一般功能


  

除非在特定函数的描述,另有规定,如果本节中描述的函数或构造给予超过规定的功能要求,功能或构造,性能应当少的参数完全一样,如果它被赋予足够的额外参数,每个这样的说法是未定义的值。


好吧。如果我通过比需要更少的争论,的这取决于的对函数本身的规范(向下滚动第15条找到每个内置函数规范)。


  

除非在特定的功能的描述中,特别说明如本节中所描述的功能或构造给出比指定的功能,以允许更多的参数,所述额外的参数是由呼叫评估,然后由该函数忽略。然而,实现可以定义,只要与这样的参数执行特定行为的行为不是一个TypeError异常抛出的是一个额外的参数的presence只是pdicated $ P $。


在参数太多传球不应该引发TypeError。但仍可能会引发其他错误。再次,的这取决于的对你谈论的功能。

您被明确谈论DOM和不是内置函数。说实话,我无法找到规范的相应部分。在ECMA规范是如此容易阅读那么W3网站。

I'd like to know both for regular all-in-the-family JS developer(me)-defined functions, as well as predefined DOM methods. Like what happens if I try to call IE's attachEvent with the signature of the WHATWG's addEventListener? For instance,

elem.attachEvent('onbillgates\'mom', function(e){ this.mount(); }, false);

Specifically, note the false. Will that trip anything up, even though the attachEvent method's signature only calls for two arguments?

Thanks.

function foo(FirstOf2, SecondOf2) {
  console.log(FirstOf2 + SecondOf2);
}

foo(1, 2, true);

解决方案

JavaScript doesn't have the concept of a fixed parameter list. For your own functions you can always specify as many parameters as you want and pass in as many as you want which ever type you want.

For built-in functions, which correlate to native code, it depends.

You asked on what it depends:

Let's look at the ECMA-262

Section 15 about built-in (not to confuse with host) functions in general

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value.

Alright. If I pass in less arguments than needed, it depends on the spec of the function itself (scroll down section 15 to find the spec for each built-in function).

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given more arguments than the function is specified to allow, the extra arguments are evaluated by the call and then ignored by the function. However, an implementation may define implementation specific behaviour relating to such arguments as long as the behaviour is not the throwing of a TypeError exception that is predicated simply on the presence of an extra argument.

Passing in too many arguments should never raise a TypeError. But still it may raise other errors. Again, it depends on the function you talk about.

You were talking explicitly about the DOM and not about built-in functions. To be honest I can't find the corresponding parts of the spec. The ECMA spec is so much easier to read then the w3 website.

这篇关于如果我叫一个JS方法,更多的参数比它定义为接受,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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