在JavaScript函数中使用忽略的参数的目的/好处是什么? [英] What is the purpose/benefit of using ignored parameters in a JavaScript function?

查看:88
本文介绍了在JavaScript函数中使用忽略的参数的目的/好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此没有任何误解,这个问题不是关于在JS函数中允许使用可选参数.

Just so there is no misunderstanding, this question is not about allowing for optional parameters in a JS function.

我的问题是由jQuery parseXML函数激发的,该函数在jQuery.js中定义如下:

My question is motiviated by the jQuery parseXML function, which is defined in jQuery.js as follows:

// Cross-browser xml parsing
// (xml & tmp used internally)
parseXML: function( data, xml, tmp ) { 
   ... 
} 

在函数主体中,参数xmltmp均在使用前已分配.这意味着它们被用作局部变量,因此该函数可以这样定义:

Within the body of the function, the parameters xml and and tmp are both assigned before they are used. That means they are being used as local variables, so the function could have been defined like this:

parseXML: function(data) { 
   var xml, tmp;
   ... 
}

除了在jQuery.js的缩小版中保存一些字符之外,第一种方法有什么好处?

What is the benefit of doing it the first way, other than saving a few characters in the minified version of jQuery.js?

推荐答案

如果我们定义两个函数...

If we define two functions...

function a ( foo ) { }
function b ( foo, bar, baz ) {}

...他们将报告不同的 length s ...

...they'll report different lengths...

console.log( [a.length, b.length] ); // logs [1, 3]

很难看到此鲜为人知的功能 javascript.

It's very rare to see this little known feature of javascript used.

但是除了要减少最小文件大小几个字节之外,这是我能想到的唯一其他原因.

But apart from shaving a couple of bytes off the minified file-size, this is the only other reason that I can think of.

这篇关于在JavaScript函数中使用忽略的参数的目的/好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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