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

json - Assign an existing javascript array to a property in object notation - Stack Overflow

programmeradmin4浏览0评论

I'm localizing a menu and I want to assign a declared array to an object property in that way:

var menuListLocal=["Home","Play","Options","Exit"];

var menu_Controller={
 _menuList: menuListLocal,
 // .... //
}

Sorry if it is too obvious.

Thanks.

I'm localizing a menu and I want to assign a declared array to an object property in that way:

var menuListLocal=["Home","Play","Options","Exit"];

var menu_Controller={
 _menuList: menuListLocal,
 // .... //
}

Sorry if it is too obvious.

Thanks.

Share edited Aug 20, 2009 at 16:12 i_am_jorf 54.7k15 gold badges136 silver badges225 bronze badges asked Aug 20, 2009 at 16:10 VeilkrandVeilkrand 3551 gold badge3 silver badges17 bronze badges 3
  • 1 It isn't clear from your post what your problem is. – i_am_jorf Commented Aug 20, 2009 at 16:11
  • 3 Uuhm this should work? Only thing, you should remove the last , Only use a , to separate properties not at the end... – Ropstah Commented Aug 20, 2009 at 16:13
  • I second @jeffamaphone, not sure what the problem is... – localshred Commented Aug 20, 2009 at 16:15
Add a ment  | 

2 Answers 2

Reset to default 3

What you have should work, keeping in mind ropstah's ment.

var menuListLocal=["Home","Play","Options","Exit"];

var menu_Controller={
 _menuList: menuListLocal,
 _other: 'Something'
};

Usage sample:

var home = menuListLocal._menuList[0];

Strange that it's not working. If John's answer doesn't clear it up for you, try explicitly setting it in an init() method on your menu_Controller:

var menuListLocal=["Home","Play","Options","Exit"];

var menu_Controller={
   _menuList: null,

   init : function (){
       menu_Controller._menuList = menuListLocal;
   }

}
menu_Controller.init();
发布评论

评论列表(0)

  1. 暂无评论