ES6 — 对象扩展

es6 袁德灿

前言:

今天来总结一下es6的对象扩展,最近一直在看es6, 感觉es6真的很好用。

对象属性和方法:

1、属性简写

const foo = 'bar';
const baz = {foo};
baz // {foo: "bar"}

// 等同于
const baz = {foo: foo};

2、方法简写

const o = {
  method() {
    return "Hello!";
  }
};
 
// 等同于
 
const o = {
  method: function() {
    return "Hello!";
  }
};

3、Object.assign()方法用于合并对象

Object.assign() 用于合并对象,有利于我们去封装函数。它的使用方法和jq 的 $.extend()方法类似

const target = { a: 1 };

const source1 = { b: 2 };
const source2 = { c: 3 };

Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}

由结果可以看出,合并后的对象,如果出现同名属性,后边的会覆盖前边的。


还能输出{{restrictNumber}}个字符  
  • {{reply.author}}

    {{CommonUtil.formateDate(reply.ac_CommentDate).shortTime}}
  • 回复了{{Comments.author}} :