I need to call a parent property from child object in an object literal

后端 未结 5 767
时光取名叫无心
时光取名叫无心 2020-12-02 23:16

I tried to call from child object a parent attribute

var parentObj = {  
   attr1:1,  
   attr2:2,   
   childObj:{  
      method1:function(){  
         r         


        
5条回答
  •  青春惊慌失措
    2020-12-03 00:05

    This can be done with the power of closures!

    var Construct = function() {
        var self = this;    
    
        this.attr1 = 1;
        this.attr2 = 2;
        this.childObj = {
            method1: function () {
                return self.attr1 * self.attr2
            }
        }
    }
    
    
    var obj = new Construct();
    

提交回复
热议问题