Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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. 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. 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();

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: {}  }; 
like image 637
Michael Taufen Avatar asked Jun 21 '11 18:06

Michael Taufen


People also ask

Can you have an object inside an object JavaScript?

A JavaScript Object is a collection of Key-Value pairs, and nested objects are objects that have other objects inside them as their property. Nesting is a widely used practice in programming as it provides the code with more enhanced details.

How do you make an object inside an object?

First of all object literal follows the syntax below: var literal = { "Name": "value", "Array": [], "NestedObject": {} }; Name value separator is the colon, not comma.

What is inside an object JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

How do I access nested objects?

The sub-properties of objects can be accessed by chaining together the dot or bracket notation .


1 Answers

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.

like image 123
Jonathan Avatar answered Sep 28 '22 07:09

Jonathan