一、Object.keys Object.create Ob

2020-06-28  本文已影响0人  Timmy小石匠

一、Object.assign()

二、Object. create()

      const person = {
        isHuman: false,
        printIntroduction: function() {
          console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`)
        }
      }

      const me = Object.create(person)

      me.name = "Matthew" // "name" is a property set on "me", but not on "person"
      me.isHuman = true // inherited properties can be overwritten

      me.printIntroduction()
      // expected output: "My name is Matthew. Am I human? true"

三、Object. keys()

// simple array
var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']

// getFoo is a property which isn't enumerable
var myObj = Object.create({}, {
  getFoo: {
    value: function () { return this.foo; }
  } 
});
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']
上一篇 下一篇

猜你喜欢

热点阅读