ES6-对象的扩展-属性的简介表示法

来源:互联网 发布:共有计价软件下载 编辑:程序博客网 时间:2024/06/05 23:52

ES6 允许直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁。

const foo = 'bar';const baz = {foo};baz // {foo: "bar"}// 等同于const baz = {foo: foo};
上面代码表明,ES6 允许在对象之中,直接写变量。这时,属性名为变量名,属性值为变量的值。下面另外一个例子。

function f(x, y) {  return {x, y};}// 等同于function f(x, y) {  return {x: x, y: y};}f(1, 2) // Object {x: 1, y: 2}
除了属性简写,方法也可以简写。

const o = {  method() {    return "Hello!";  }};// 等同于const o = {  method: function() {    return "Hello!";  }};
下面是一个实例的例子

let birth = '2000/01/01';const Person = {  name: '张三',  //等同于birth: birth  birth,  // 等同于hello: function ()...  hello() { console.log('我的名字是', this.name); }};
这总写法用于函数的返回值,将会非常方便。

function getPoint() {  const x = 1;  const y = 10;  return {x, y};}getPoint()// {x:1, y:10}
CommonJS 模块输出一组变量,就非常合适使用简介写法。

let ms = {};function getItem (key) {  return key in ms ? ms[key] : null;}function setItem (key, value) {  ms[key] = value;}function clear () {  ms = {};}module.exports = { getItem, setItem, clear };// 等同于module.exports = {  getItem: getItem,  setItem: setItem,  clear: clear};
属性的赋值器(setter)和取值器(getter),事实上也是采用这种写法。

const cart = {  _wheels: 4,  get wheels () {    return this._wheels;  },  set wheels (value) {    if (value < this._wheels) {      throw new Error('数值太小了!');    }    this._wheels = value;  }}
注意,简介写法的属性名总是字符串,这会导致一些看上去比较奇怪的结果。

const obj = {  class () {}};// 等同于var obj = {  'class': function() {}};
上面代码中,class是字符串,所以不会因为它属于关键字,而导致解析报错。

如果某个方法的值是一个 Generator 函数,前面需要加上星号。

const obj = {  * m() {    yield 'hello world';  }};

原创粉丝点击