开发者

access object name within the object with this

开发者 https://www.devze.com 2023-02-23 04:46 出处:网络
in i want to alert the \'image\' which is the object name, how can i access with this ? Objectswitch={

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

精彩评论

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