isConcatSpreadable

221 阅读1分钟

concat方法默认打平数组, 在参数数组上把[Symbol.isConcatSpreadable]设置为false可以阻止打平数组。

const colors = ['red'];
const colors1 = ['blue', 'green'];
colors.concat(colors1);   // ['red', 'blue', 'green']
colors1[Symbol.isConcatSpreadable] = false;
colors.concat(colors1);  //  ['red', Array(2)]