Javascript literal object, reference to itself

前端 未结 4 1209
灰色年华
灰色年华 2020-12-16 23:44

I have this example code:

var foo = {

    self: this,

    init: function(){

        self.doStuff();
    },

    doStuff: function(){
        alert(\'doing         


        
4条回答
  •  暖寄归人
    2020-12-17 00:30

    ES6 provides getters for the object properties, so you can use them to reference the object itself and to use its other members:

    var foo = {
         prop1: 1,
    
         get prop2() { return this.prop1 + 1; }
    }
    
    // foo.prop2 = 2;
    

提交回复
热议问题