Accessor Properties

It is not possible to define an accessor property explicitly; you must use Object.defineProperty().

  1. // Define object with pseudo-private member 'year_'
  2. // and public member 'edition'
  3. let book = {
  4. year_: 2017,
  5. edition: 1
  6. };
  7. Object.defineProperty(book, "year", {
  8. get() {
  9. return this.year_;
  10. },
  11. set(newValue) {
  12. if (newValue > 2017) {
  13. this.year_ = newValue;
  14. this.edition += newValue - 2017;
  15. }
  16. }
  17. });
  18. Object.defineProperty(book, "aaa", {
  19. value: 0
  20. });
  21. book.year = 2018;
  22. console.log(book.edition); // 2

拓展:Vue数据响应式

Defining Multiple Properties

Object.defineProperties()定义多个properties

  1. let book = {};
  2. Object.defineProperties(book, {
  3. year_: {
  4. value: 2017
  5. },
  6. edition: {
  7. value: 1
  8. },
  9. year: {
  10. get() {
  11. return this.year_;
  12. },
  13. set(newValue) {
  14. if (newValue > 2017) {
  15. this.year_ = newValue;
  16. this.edition += newValue - 2017;
  17. }
  18. }
  19. }
  20. });

拓展:Vue之computed和watch

getter / setter

  1. let obj = {
  2. b: 'h',
  3. get a(){
  4. return 0;
  5. },
  6. set a(value){
  7. this.b = value;
  8. }
  9. }
  10. obj.a // 0
  11. obj.a = "hello"
  12. obj.b // "hello"

Object.assign()

相同属性会覆盖

  1. dest = { id: 'dest' };
  2. result = Object.assign(dest, { id: 'src1', a: 'foo' }, { id: 'src2', b: 'bar' });
  3. // Object.assign will overwrite duplicate properties.
  4. console.log(result); // { id: src2, a: foo, b: bar }

getter/setter

  1. /**
  2. * Getters and setters
  3. */
  4. dest = {
  5. set a(val) {
  6. console.log('Invoked dest setter with param ${val}');
  7. }
  8. };
  9. src = {
  10. get a() {
  11. console.log('Invoked src getter');
  12. return 'foo';
  13. }
  14. };
  15. Object.assign(dest, src);
  16. // Invoked src getter
  17. // Invoked dest setter with param foo
  18. // Since the setter does not perform an assignment,
  19. // no value is actually transferred
  20. console.log(dest); // { set a(val) {...} }

Error handling

  1. dest = {};
  2. src = {
  3. a: 'foo',
  4. get b() {
  5. // Error will be thrown when Object.assign()
  6. // invokes this getter.
  7. throw new Error();
  8. },
  9. c: 'bar'
  10. };
  11. try {
  12. Object.assign(dest, src);
  13. } catch(e) {}
  14. // Object.assign() has no way of rolling back already performed changes,
  15. // so set operations already performed on the destination object before
  16. // the error is thrown remain:
  17. console.log(dest); // { a: foo }

Object.is()

比较两个值是否相同

  1. Object.is(true, 1) // false
  2. Object.is({}, {}) // false
  3. Object.is("2", 2) // false
  4. // Correct 0, -0, +0 equivalence/nonequivalence:
  5. Object.is(+0, -0) // false
  6. Object.is(+0, 0) // true
  7. Object.is(-0, 0) // false
  8. // Correct NaN equivalence:
  9. Object.is(NaN, NaN) // true

Enhanced Object Syntax

属性简写

  1. let name = 'Matt';
  2. let person = {
  3. name // name:name简写
  4. };
  5. console.log(person); // { name: 'Matt' }

计算属性

  1. const nameKey = 'name';
  2. const ageKey = 'age';
  3. const jobKey = 'job';
  4. let uniqueToken = 0;
  5. function getUniqueKey(key) {
  6. return '${key}_${uniqueToken++}';
  7. }
  8. let person = {
  9. [getUniqueKey(nameKey)]: 'Matt',
  10. [getUniqueKey(ageKey)]: 27,
  11. [getUniqueKey(jobKey)]: 'Software engineer'
  12. };
  13. console.log(person); // { name_0: 'Matt', age_1: 27, job_2: 'Software engineer' }

方法简写

  1. let person = {
  2. // sayName: function(name) {} 简写
  3. sayName(name) {
  4. console.log('My name is ${name}');
  5. }
  6. };
  7. person.sayName('Matt'); // My name is Matt

计算属性和方法简写混合使用

  1. const methodKey = 'sayName';
  2. let person = {
  3. [methodKey](name) {
  4. console.log('My name is ${name}');
  5. }
  6. }
  7. person.sayName('Matt'); // My name is Matt

解构赋值

  1. let person = {
  2. name: 'Matt',
  3. age: 27
  4. };
  5. let { name, age } = person;
  6. console.log(name); // Matt
  7. console.log(age); // 27
  1. let personName, personAge;
  2. let person = {
  3. name: 'Matt',
  4. age: 27
  5. };
  6. // 注意括号
  7. ({name: personName, age: personAge} = person);
  8. console.log(personName, personAge); // Matt, 27

解构传参

  1. let person = {
  2. name: 'Matt',
  3. age: 27
  4. };
  5. function printPerson(foo, {name, age}, bar) {
  6. console.log(arguments);
  7. console.log(name, age);
  8. }
  9. function printPerson2(foo, {name: personName, age: personAge}, bar) {
  10. console.log(arguments);
  11. console.log(personName, personAge);
  12. }
  13. printPerson('1st', person, '2nd');
  14. // ['1st', { name: 'Matt', age: 27 }, '2nd']
  15. // 'Matt', 27
  16. printPerson2('1st', person, '2nd');
  17. // ['1st', { name: 'Matt', age: 27 }, '2nd']
  18. // 'Matt', 27