how to use javascript Object.defineProperty

前端 未结 10 1164
不知归路
不知归路 2020-11-29 14:21

I looked around for how to use the Object.defineProperty method, but couldn\'t find anything decent.

Someone gave me this snippet of code:

Object.def         


        
10条回答
  •  温柔的废话
    2020-11-29 14:56

    Object.defineProperty(Array.prototype, "last", {
      get: function() {
        if (this[this.length -1] == undefined) { return [] }
        else { return this[this.length -1] }
      }
    });
    
    console.log([1,2,3,4].last) //returns 4

提交回复
热议问题