今天在学习时看到几段代码, 让我感叹 JS 的灵活, 特此一记:
- let stateObj = {
- a:1,b:3
- };
- let newObj = {
- b:13,c:4
- } ;
- stateObj = {
- ...stateObj, ...newObj
- } ;
- console.log(stateObj);
- // 输出 {
- a: 1, b: 13, c: 4
- }
- let {
- a = 0
- } = {
- a:1,b:3
- } ;
- a = a + 1 ;
- console.log(a) ;
- // 输出 2
- //ES6 允许直接写入变量和函数, 作为对象的属性和方法. 这样的书写更加简洁.
- const foo = 'bar';
- const baz = {
- foo
- };
- console.log(baz);
- // 输出 {
- foo: "bar"
- }
来源: http://www.bubuko.com/infodetail-2828652.html