简体   繁体   English

在JavaScript函数中使用忽略参数的目的/好处是什么?

[英]What is the purpose/benefit of using ignored parameters in a JavaScript function?

Just so there is no misunderstanding, this question is not about allowing for optional parameters in a JS function. 就这样没有误解,这个问题不是关于在JS函数中允许可选参数。

My question is motiviated by the jQuery parseXML function, which is defined in jQuery.js as follows: 我的问题是由jQuery parseXML函数parseXML ,它在jQuery.js定义如下:

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

Within the body of the function, the parameters xml and and tmp are both assigned before they are used. 在函数体内,参数xmltmp都是在使用之前分配的。 That means they are being used as local variables, so the function could have been defined like this: 这意味着它们被用作局部变量,因此函数可以像这样定义:

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

What is the benefit of doing it the first way, other than saving a few characters in the minified version of jQuery.js ? 除了在jQuery.js的缩小版本中保存几个字符之外,第一种方式的好处是什么?

If we define two functions... 如果我们定义两个函数......

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

...they'll report different length s ... ......他们会报告不同的length ......

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

It's very rare to see this little known feature of javascript used. 很难看到这个鲜为人知的javascript 功能

But apart from shaving a couple of bytes off the minified file-size, this is the only other reason that I can think of. 但除了缩小文件大小的几个字节之外,这是我能想到的唯一其他原因。

In general, you might add unused parameters to a function to conform to some pre-agreed function signature, if you're going to pass this function to another function as a callback or continuation, and the API contract says "I call your callback with these parameters", and you don't need all the parameters to do what you want to do in the callback. 通常,您可以将未使用的参数添加到函数中以符合某些预先约定的函数签名,如果您要将此函数作为回调或延续传递给另一个函数,并且API契约说“我打电话给您的回调函数这些参数“,你不需要所有参数来做你想要在回调中做的事情。 (This would apply to any language, not just JavaScript.) (这适用于任何语言,而不仅仅是JavaScript。)

In this specific case, I don't know. 在这个具体案例中,我不知道。 How is parseXML used; 如何使用parseXML; is it called directly, or used as an argument to other functions which might expect a 3-argument function? 是直接调用,还是用作可能期望3参数函数的其他函数的参数?

(xml & tmp used internally)

You misunderstand the meaning. 你误解了意思。 They do not mean "internally" within the function. 它们并不意味着在函数内部“内部”。 They mean internally within the library. 它们指的是图书馆内部。 The public API of this function has one parameter (data). 该函数的公共API有一个参数(数据)。 The private API of this function has 3 parameters. 此函数的私有API有3个参数。

This is common throughout jQuery. 这在jQuery中很常见。 In general these functions can work with and without side effects. 通常,这些功能可以有或没有副作用。 The API without side effects is public and jQuery itself will pass in more parameters to cause side effects that you as a user should not be doing. 没有副作用的API是公共的,jQuery本身将传递更多参数,以引起您作为用户不应该做的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中使用(function(){...})()有什么好处 - What is benefit of using (function(){…})() in JavaScript 在函数中覆盖的javascript参数的用途是什么? - What's the purpose of javascript parameters that are overwritten in the function? Javascript:使用函数上下文与传递作为参数有什么好处 - Javascript: What is the benefit of using function context vs passing as parameter 在JavaScript中使用〜的目的是什么? - What is the purpose of using ~ in JavaScript? 在javascript中的函数声明中命名函数的好处是什么? - What is the benefit of naming a function in a function declaration in javascript? 在 javascript 中使用异步时,function 内部的参数的目的是什么? - What is the purpose of the parameter to the function inside the then function while using async in javascript? 这个JavaScript函数的目的是什么? - What's the purpose of this JavaScript function? 在javascript函数中将此作废的目的是什么 - What is the purpose of void this in the javascript function 在JavaScript中使用Function.call.apply的目的是什么? - What's the purpose of using Function.call.apply in JavaScript? 在JavaScript中分配变量时使用命名函数的目的是什么? - What is the purpose using a named function when assigning to a variable in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM