es6 对象的扩展

Posted 宝清老窖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es6 对象的扩展相关的知识,希望对你有一定的参考价值。

var foo = ‘aaaa‘;
var baz = {foo};  //{foo: ‘aaaa‘}
foo = ‘bbbb‘;
baz;  //{foo: ‘bbbb‘}
function f(x, y){
  return {x, y}
}

function f(x, y) {
  return { x: x, y: y };
}

 
var o ={
  f(){
    alert(1)
  }
}

var o = {
  f: function f() {
    alert(1);
  }
};

 

var borth = ‘1987-02-02‘
var Person = {
  name: ‘zhangsan‘,
  borth,
  hello(){
    console.log(‘my name is ‘ + this.name)
  }
}

==

var borth = ‘1987-02-02‘;
var Person = {
  name: ‘zhangsan‘,
  borth: borth,
  hello: function hello() {
    console.log(‘my name is ‘ + this.name);
  }
};

 模块模式

var ms = {}

function getItem(key){
  return key in ms? ms[key]: null;
}

function setItem(key, value){
  ms[key] = value;
}

function clear(){
  ms = {}
}

module.export = {getItem, setItem, clear}
==
module.export = {
    getItem: getItem,
    setItem: setItem,
    clear: clear
}

 

var cart = {
  _wheels: 4,
  get wheels(){
    console.log(‘get‘)
    return this._wheels;
  },
  set wheels(value){
    console.log(‘set‘);
    this._wheels = value;
  }
}

 

以上是关于es6 对象的扩展的主要内容,如果未能解决你的问题,请参考以下文章

ES6 对象的扩展

ES6 - 对象扩展(增强字面量)

es6~扩展运算符后续

es6数组的扩展

ES6正则扩展

函数的扩展--ES6