最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript access parent properties - Stack Overflow

programmeradmin1浏览0评论

How can I get parent options?

I would like to access a global myObject options

var myObject = {    
  method: function(){
    this.options = {};
  },      
  property: {    
    propertyMethod: function(){
       alert(this.options);
    }     
  }    
}    
myObject.method();
myObject.property.propertyMethod();

How can I get parent options?

I would like to access a global myObject options

var myObject = {    
  method: function(){
    this.options = {};
  },      
  property: {    
    propertyMethod: function(){
       alert(this.options);
    }     
  }    
}    
myObject.method();
myObject.property.propertyMethod();
Share Improve this question asked Feb 13, 2013 at 16:13 xavipxavip 5536 silver badges14 bronze badges 3
  • 3 Generally you can't do that. But if myObject is global, then you can always do myObject.options in child object. – freakish Commented Feb 13, 2013 at 16:16
  • @freakish: That (slightly expanded) is an answer. – T.J. Crowder Commented Feb 13, 2013 at 16:16
  • @T.J.Crowder Yeah, actually I wasn't 100% sure so I've decided to make it a ment. :) – freakish Commented Feb 13, 2013 at 16:19
Add a ment  | 

2 Answers 2

Reset to default 5

If you mean, you want to access myObject's options property from within property.propertyMethod, there's no way to do that except by taking advantage of the fact that propertyMethod is a closure over the myObject variable, e.g.:

var myObject = {    
  method: function(){
    this.options = {};
  },      
  property: {    
    propertyMethod: function(){
       alert(myObject.options); // <=== Change is here
    }     
  }    
}    
myObject.method();
myObject.property.propertyMethod();

...which isn't usually a good idea. :-) Instead, you may want to design your object differently. For instance:

var myObject = (function() {
  var obj = {
    method: function(){
      obj.options = {};
    },      
    property: {    
      propertyMethod: function(){
         alert(obj.options);
      }     
    }    
  };
  return obj;
})();
myObject.method();
myObject.property.propertyMethod();

That way, you're using the execution context of the call to the function. But if you're going to do that, maybe take it to the next step:

function makeObject() {
  var obj = {
    method: function(){
      obj.options = {};
    },      
    property: {    
      propertyMethod: function(){
         alert(obj.options);
      }     
    }    
  };
  return obj;
}
var myObject = makeObject();
myObject.method();
myObject.property.propertyMethod();

...so you can make more than one. Or even make a true constructor:

function MyObject() {
  var obj = this;

  obj.method = function(){
    obj.options = {};
  };      

  obj.property = {    
    propertyMethod: function(){
       alert(obj.options);
    }     
  };
}
var myObject = new MyObject();
myObject.method();
myObject.property.propertyMethod();

...although since you're not leveraging the prototype, there's no much reason to make it a constructor function.

You can use call to bind this to myObject

myObject.property.propertyMethod.call(myObject);

发布评论

评论列表(0)

  1. 暂无评论