简体   繁体   English

命名参数与常规参数

[英]Named parameters vs regular ones

I am wondering if there is some difference in performance between javascript's regular parameters 我想知道javascript的常规参数之间在性能上是否有所不同

call(var1, var2);

and jQuery style: 和jQuery样式:

call({var1: '1', var2: '2'});

I am using the both depenging on current situation. 我正在同时使用这两种情况。

Maybe there is some standard? 也许有一些标准?

Thank you 谢谢

That's not jQuery style , but just a Javascript Object Literal actually. 那不是jQuery风格 ,实际上只是Java语言对象文字。

You choose literals when you have to pass a map of named values, but you don't know which ones and how many will be passed actually, eg jQuery CSS setter: 在必须传递命名值的映射时,您可以选择文字,但是您不知道实际上将传递哪些值,以及将传递多少,例如jQuery CSS setter:

$(el).css({
  'color' : 'red',
  'font-weight' : 'bold'
});

The function body will take care of inspecting the argument object, and do stuff accordingly 函数体将检查参数对象,并做相应的填充

You choose a plain list of arguments [ function(arg1, arg2, ... argN) ] when you possibly know all the parameters needed by the function, and you are able to provide them: 当您可能知道函数所需的所有参数并可以提供它们时function(arg1, arg2, ... argN)可以选择简单的参数列表[ function(arg1, arg2, ... argN) ]。

var distance = function(p1, p2){

  // calculate distance between points p1 and p2

}

IMO, It all depends on what your scenario is. IMO,这完全取决于您的情况。 If you have var1, var2, ... varN you would obviously either use array or JSON. 如果您有var1, var2, ... varN ,则显然可以使用数组或JSON。 I do not think there is any issue with the performance. 我认为性能没有任何问题。

If I have only one parameter in my function, Why would i want to use json (creation of json might use some resource). 如果我的函数中只有一个参数,为什么我要使用json(创建json可能会使用一些资源)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM