Object
对象的key值可以加空格,不过不能使用点语法进行访问和创建
const obj = {};
obj['first name'] = 'name';
console.log(obj) // { first name : 'name' }
console.log(obj['first name']) // 'name
var code = "003072c3-1d28-4ab6-b9c9-9321942eba7d"
Array
from & of
Array在es6新增了两个用于创建数据的静态方法,from和of
from用于将类数组结构转换为数组实例,of用于将一组参数转换为数组实例
Array.from的第一个参数为一个类数组对象,即任何可迭代的结构,或者有一个length属性和可索引元素的结构
第二个参数可以理解为map,将转换完的数据通过第二个参数重新处理
Array.from('123') //['1', '2', '3']
Array.from(new Set().add(1).add(2)) // [1, 2]
Array.from({ 0: 0, 1: 1, length: 2 }) // [0, 1]
// 还可以进行浅拷贝
const arr = [1, 2, 3];
const arr2 = Array.from(arr);
console.log(arr === arr2) // false
Array.from('123', v => v * 2) // [2, 4, 6]
Array.of()用于替代es6之前常用的Array.prototype.slice.call(arguments),这种将伪数组转换为数组的方法
数组空位
使用数组字面量创建数组时,可以使用一串逗号来创建空位
const options = [, , ,]; // [empty × 3]
console.log(options.length) // 3
ES6新增的方法普遍将这些空值当成undefined处理
for (const iterator of options) {
console.log(iterator === undefined) // true
}
ES6之前的方法会忽略这个空位,但是具体的行为也会因方法而异
const options = [1, , , , 5];
// map会跳过空位置
options.map(() => 6); // [6, , , , 6]
// join会将空视为空字符串
options.join('-'); // 1----5
由于行为不一致,因此在实践中应避免使用数组空位,如果需要数组空位,应显式的用undefined