MVVM(1):理解Object.definePropety

2020-11-27  本文已影响0人  晓露_0d5c

Object.defineProperty()方法

1、mdn地址

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

2、Object.definePropery默认:

3、对象属性的可枚举性 enumerable

1、for...in 和 Object.keys

  let Parent = function () {
      this.name = '888',
        this.say = function () {
          console.log('say---')
        }
    }

    let Children = function () { }
    Children.prototype = new Parent()

    let p = new Parent()
    let c = new Children()

    p.say()
    c.say()

    for (let item in p) {
      console.log("item", item)
      // 打印结果
      // 第一次:item name
      // 第二次:item say
    }
    for (let cItem in c) {
      console.log("cItem", cItem)
      // 打印结果
      // 第一次:item name
      // 第二次:item say
    }

    console.log("p keys", Object.keys(p))
    // 打印结果
    // p keys   ["name","key"]


    console.log("c keys", Object.keys(c))
    // 打印结果
    // c keys   []

2、JSON.stringify

    let Father = function () {
      this.lastName = 'zhang'
      this.country = 'China'
    }
    let Children = function () {
    }

    Children.prototype = new Father()

    let f = new Father()
    let c = new Children()

    console.log(f)
    /*
    * country: "China" 
    * lastName: "zhang" 
    * __proto__: Object
    */

    console.log(c)
    /*
    * __proto__: Father
    */

    console.log(JSON.stringify(f)) //{"lastName":"zhang","country":"China"}
    console.log(JSON.stringify(c)) //{}

    let f2 = JSON.parse(JSON.stringify(f))
    let c2 = JSON.parse(JSON.stringify(c))

    console.log("f2 lastName:", f2.lastName) //zhang
    console.log("c2 lastName:", c2.lastName) // undefined

3、Object.assign()

let a = Object.assign({}, { a: 3 })
    console.log("a:", a)


    let Father = function () {
      this.lastName = "liu"
      this.country = "China"
    }
    let Children = function () { }

    Children.prototype = new Father()

    let f = new Father()
    let c = new Children()


    console.log("f", JSON.stringify(f))
    console.log("c", JSON.stringify(c))

    let f1 = JSON.parse(JSON.stringify(f))
    let c1 = JSON.parse(JSON.stringify(c))
    console.log("f1", f1.lastName) // liu
    console.log("c1", c1.lastName) // undefined

    let r1 = Object.assign(f, { dd: 34 })
    console.log("r1:", r1)
    console.log(r1.lastName) //liu

    let r2 = Object.assign(c, { dd: 33 })
    console.log("r2:", r2)
    console.log(r2.lastName) // liu

4、writable默认是false

    let obj = { a: 333 }
    console.log("obj:", obj.a) //333

    obj.a = 444
    console.log("obj", obj.a) //444



    let obj2 = {}
    Object.defineProperty(obj2, 'a', {
      value: 333
    })
    console.log("obj2:", obj2.a) //333

    obj2.a = 444
    console.log("obj2:", obj2.a) //333

    // 由此可以看出 Object.defineProperty默认的writable是false

5、get和set

<!-- get和set方法理解 -->
    let obj = {}
    Object.defineProperty(obj, 'a', {
      set(val) {
        console.log("set a")
      },
      get() {
        return 333
      }
    })

    console.log(obj.a) //333

    obj.a = 444 //  set a

    console.log(obj.a) //333
let obj = {}
    Object.defineProperty(obj, 'a', {
      // value: 333, // 有get和set,写value描述符会报错
      // writable: true, // 有get和set,写writable描述符会报错
      configurable: true,
      enumerable: true,
      get() {
        return this.value
      },
      set(val) {
        this.value = val
      }
    })

    console.log(obj.a) // undefined

    obj.a = 333

    console.log(obj.a) // 333
上一篇下一篇

猜你喜欢

热点阅读