前言
ECMAScript2017发布于
2017年的
6月份,本文主要是介绍
ECMAScript 2017 新增的语言特性,
主要有以下三个方面的Object ,String ,function
1.Object. values
let obj2
= {
name
: "loki",
age
: 20,
sex
: "female"
};
console
.log(Object
.values(obj2
));
result
=>[ 'loki', 20, 'female' ]
2.Object. entries
以数组形式返回对象中的键值对
let obj2
= {
name
: "loki",
age
: 20,
sex
: "female"
};
log(Object
.entries(obj2
));
result
=>[ [ 'name', 'loki' ], [ 'age', 20 ], [ 'sex', 'female' ] ]
这样的话可以直接通过 forof方式直接遍历对象了
for (const [key
, value] of Object
.entries(obj2
)) {
log(key
, value);
}
result
=> name loki
result
=> age
20
Map(3) { 'name' => 'loki', 'age' => 20, 'sex' => 'female' }sex female
由于map就是需要这种格式的数据我们可以把对象直接转换成map
log(new Map(Object
.entries(obj2
)));
result
=> Map(3) { 'name' => 'loki', 'age' => 20, 'sex' => 'female' }
3.Object.getOwnProperyDescriptors
获取对象的完整描述信息,演示如下
const obj
= {
firstName
: "handsome",
lastName
: "Wang",
age
: 23,
get name() {
return `$
{this.firstName
} $
{this.lastName
}`
;
},
};
const obj2
= Object
.assign({}, obj
);
obj2
.firstName
= "snoopy";
console
.log(obj2
.name
);
const descriptors
= Object
.getOwnPropertyDescriptors(obj
);
console
.log(descriptors
);
const obj3
= Object
.defineProperties({}, descriptors
);
obj3
.firstName
= "Loki";
console
.log(obj3
.name
);
String.property.padStart / String.property.padEnd
参数
(count
, str
)
用指定的字符填充到指定字符数,常用于输出格式化
const books
= {
html
: 5,
css
: 16,
javascript
: 128,
};
for (const [name
, count
] of Object
.entries(books
)) {
const padName
= name
.padEnd(16, "-");
const padCount
= count
.toString().padStart(3, 0);
console
.log(padName
, padCount
);
}
允许函数最后一个参数带逗号
减少 git diff
,便于调整参数顺序
const add = (params1
, params2
, ) => {
return params1
+ params2
};
log(add(10, 20))
谢谢观看,如有不足,敬请指教