在javascript中使用rest参数和spread操作符 [英] Usage of rest parameter and spread operator in javascript

查看:99
本文介绍了在javascript中使用rest参数和spread操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,在ECMAScript 5中,您可以执行以下操作来获取一组参数:从第二个元素:

  // ES 5 
store('Joe','money');
store('Jane','letters','certificates');
函数store(name){
var items = [] .slice.call(arguments,1); // ['money'] in first case
items.forEach(function(item){
vault.customer [name] .push(item);
});
}

,这将等同于ECMAScript 6中的以下代码:

  // ES 6 
store('Joe','money');
store('Jane','letters','certificates');
函数store(name,... items){
items.forEach(function(item){
vault.customer [name] .push(items)
});
}

它们之间的区别只是语法,还是有性能问题?



另外对于spread运算符(...)

  // in ecmascript5 
var max = Math.max.apply(null,[14,3,77]);
//但是在ecmascript6
var max = Math.max(... [14,3,77]);

这是否只是语法更改或性能问题?

解决方案

除了@ kangax的回应之外,我会详细说明,性能和正确性是很多次调用参数对象的问题。如果您将参数对象传递给另一个函数,则可以通过它修改范围中相应的局部变量的权限。

  function foo(arg){
bar(arguments);
return arg;
}

函数栏(args){
args [0] = 20;
}

foo(10)// 20

此功能的存在使功能中的本地推理无效,这使得JIT优化更加困难,并引入某些安全隐患。为速度而设计的程序必须通过比 Array.prototype.slice.call(arguments) idiom更复杂的样板代码解决这个问题,在安全上下文中,必须严格禁止参数的传递。



消除使用参数对象提取可变参数是新语法的许多优点之一。


What's the usage of rest parameter that will be added in ECMAScript 6?

For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element:

// ES 5
store('Joe', 'money');
store('Jane', 'letters', 'certificates');
function store(name) {
  var items = [].slice.call(arguments, 1); //['money'] in first case
  items.forEach(function (item) {
    vault.customer[name].push(item);
  });
}

and that will be equivalent to the following code in ECMAScript 6:

// ES 6
store('Joe', 'money');
store('Jane', 'letters', 'certificates');
function store(name, ...items) {
  items.forEach(function (item) {
    vault.customer[name].push(items)
  });
}

Is the difference between them is just syntax or there's a performance issue?

Also for spread operator (...)

//in ecmascript5
var max = Math.max.apply(null, [14, 3, 77]);
//but in ecmascript6
var max = Math.max(...[14, 3, 77]);

Is this just syntax change or performance issue?

解决方案

In addition to @kangax’s response, I would elaborate that performance and correctness are problematic many times the arguments object is invoked. If you pass the arguments object to another function, you pass with it the right to modify the corresponding local variables in your scope.

function foo(arg) {
    bar(arguments);
    return arg;
}

function bar(args) {
    args[0] = 20;
}

foo(10) // 20

The existence of this feature invalidates local reasoning within a function, which makes JIT optimization more difficult and introduces certain security hazards. Programs that are designed for speed must work around this problem with far more elaborate boilerplate code than the Array.prototype.slice.call(arguments) idiom, and in security contexts, the passing of arguments must be strictly disallowed.

Eliminating the need to use the arguments object to extract variadic arguments is one of the many advantages to the new syntax.

这篇关于在javascript中使用rest参数和spread操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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