扩展语法的替代方法 [英] Alternatives of spread syntax
问题描述
我看到扩展语法的几种用法在一个代码中.例如:
I see several uses of spread syntax in a code. For example:
function tree2table(tree) {
var children = tree["children"];
if (children === undefined) return [];
var result = [];
for (var i = 0; i < children.length; i++) {
var child = children[i];
var link = [child["name"], tree["name"], child["size"]];
result.push(link);
result.push(...tree2table(child))
}
return result
}
但是,IE不支持传播语法.有谁知道改变result.push(...tree2table(child))
使其像跨浏览器一样高效的最佳方法是什么?
However, spread syntax is not supported in IE. Does anyone know what is the best way to change result.push(...tree2table(child))
such that it becomes cross-browser and as efficient as before?
推荐答案
Differenz
Array.prototype.push.apply(result, values)
修改而不是复制数组
Differenz
Array.prototype.push.apply(result, values)
modifies instead of making a copy of an array
const result = []
const values = ['a', 'b']
Array.prototype.push.apply(result, values)
console.log(result)
function tree2table(tree) {
var children = tree["children"];
if (children === undefined) return [];
var result = [];
for (var i = 0; i < children.length; i++) {
var child = children[i];
var link = [child["name"], tree["name"], child["size"]];
result.push(link);
Array.prototype.push.apply(result, tree2table(child))
}
return result
}
兼容
基于版本信息
以下文档模式支持:怪癖,Internet Explorer 6标准,Internet Explorer 7标准,Internet Explorer 8标准,Internet Explorer 9标准,Internet Explorer 10标准,Internet Explorer 11标准.
Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards.
这篇关于扩展语法的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!