Things You'll Need:
- Computer
- Text editor
- Website or test on your personal computer
-
Step 1
In your javascript area (probably in your .js file that you will link to from your HTML(ASP, PHP, JSP, or any page displaying HTML content), type the following:
var displayMessages = {
sayHello: "hello",
sayBye: "bye"
}; -
Step 2
Now in any function also in your javascript if you want to refer to "hello" or "bye" you can put:
displayMessages.sayHello
example: alert(displayMessages.sayHello); // will alert "hello" to the user -
Step 3
If you want to make globally (within your javascript file) accessible object references, use the following JSON:
var myObjects = {
objectOne: function() {
return document.getElementById('myObjectsIdHere');
},
objectTwo: function() {
return document.getElementById('myObjectsIdHere2');
}
}; -
Step 4
Then, if you want to refer to that object later on, instead of always calling document.getElementById('myObjectsIdHere'), you can just put:
myObjects.objectOne();
example: var objectOne = myObjects.objectOne();
objectOne.value = "a new value!"; -
Step 5
Of course this isn't necessary to do, but I like to organize my javascript in this manner in case the object I'm referring to changes, I do not have to change many document.getElementById()'s, just the one in the myObjects var.










