开发者

access object name within the object with this

in i want to alert the 'image' which is the object name, how can i access with this ?

Objectswitch={
    'image':
    {
        addCount:function(){
            alert('count');
        },
        addCountandCreate:function(){
            this.addCount();
            alert(this);
        }
  开发者_开发技巧  }
}


Sorry, you can't with that structure. this refers to the object: {addCount:, addCountandCreate:} and JS has no way to say this.parentObject (which doesn't make sense anyway since an object can have multiple references)


You'll need to add the name as a property of the object:

var imageObj = {
  name: "image",
  addCount:function(){
    alert('count');
  },
  addCountandCreate:function(){
     this.addCount();
     alert(this.name);
  }
}

you can then reference that object from within another, of course:

ObjectSwitch = {
  "image": imageObj
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜