简体   繁体   English

javascript中对象内的对象

[英]Objects inside objects in javascript

I'm somewhat new to Javascript, so maybe this is just a noob mistake, but I haven't found anything that specifically helps me while looking around. 我对Javascript有些新意,所以也许这只是一个noob错误,但我没有找到任何特别有助于我环顾四周的东西。 I'm writing a game, and I'm trying to build an object for the pause menu. 我正在写一个游戏,我正在尝试为暂停菜单构建一个对象。

One of the things I would like to do is have the buttons on the menu be objects inside of the pause_menu object for the sake of organization. 我想做的其中一件事是,为了组织起见,菜单上的按钮是pause_menu对象内的对象。 I'm eventually going to add event handlers to these objects, and I'd like to do that inside the pause_menu object as well. 我最终要为这些对象添加事件处理程序,我也想在pause_menu对象中执行此操作。 Some of the button's aren't fully coded yet, but I'd like to get at least something working before I keep going. 有些按钮尚未完全编码,但我希望在继续操作之前至少可以使用某些按钮。

I'm using Raphael.js v1.5.2 to render the shapes. 我正在使用Raphael.js v1.5.2渲染形状。 The Raphael stuff works for the rest of the interface, but the code for that is not as pleasant to look at as this, so something similar to this would be preferable to me. 拉斐尔的东西适用于界面的其余部分,但是代码并不像这样看起来那么令人愉快,所以类似的东西对我来说会更好。

My current problem is that nothing actually renders when I do var pause_menu = new pause_menu(); 我目前的问题是,当我执行var pause_menu = new pause_menu()时,实际上没有任何渲染。

This is the code I have so far for the pause menu: 这是我到目前为止暂停菜单的代码:

//Pause Menu Object:
function pause_menu() {

    function pause_button() {
        this.button = game.rect(0, 350, 150, 50, 5);
        this.text =  game.text(75, 375, 'PAUSE');
    }
    function resume_button() {
        this.button;
        this.text;
    }
    function quit_button() {
        this.button;
        this.text;
    }
    this.pause_button = new pause_button(); //the button that the user presses to pause the game (I want an event handler on this to trigger .show() method for presently hidden menu items)
    this.resume_button = new resume_button();
    this.quit_button = new quit_button();
    this.box = game.rect(150, 50, 400, 300, 5).hide(); //the box that surrounds the menu when it appears
}
var pause_menu = new pause_menu();

OK, so here's the solution (with an event handler): 好的,所以这是解决方案(带有事件处理程序):

var pause_menu = {

    pause_button: { button : game.rect(0, 350, 150, 50, 5).click(function (event){
                       pause_menu.menu_box.show();
                  }), text : game.text(75, 375, 'PAUSE') },
    menu_box: game.rect(150, 50, 400, 300, 5).hide(),
    resume_button: {},
    quit_button: {}

};
var pause_menu = {
    pause_button : { someProperty : "prop1", someOther : "prop2" },
    resume_button : { resumeProp : "prop", resumeProp2 : false },
    quit_button : false
};

then: 然后:

pause_menu.pause_button.someProperty //evaluates to "prop1"

etc etc. 等等

You may have as many levels of Object hierarchy as you want, as long you declare an Object as being a property of another parent Object. 只要将Object声明为另一个父Object的属性,就可以拥有任意级别的Object层次结构。 Pay attention to the commas on each level, that's the tricky part. 注意每个级别的逗号,这是棘手的部分。 Don't use commas after the last element on each level: 不要在每个级别的最后一个元素后使用逗号:

{el1, el2, {el31, el32, el33}, {el41, el42}}

 var MainObj = { prop1: "prop1MainObj", Obj1: { prop1: "prop1Obj1", prop2: "prop2Obj1", Obj2: { prop1: "hey you", prop2: "prop2Obj2" } }, Obj3: { prop1: "prop1Obj3", prop2: "prop2Obj3" }, Obj4: { prop1: true, prop2: 3 } }; console.log(MainObj.Obj1.Obj2.prop1); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM