METHOD 1
Based on your recent comments, this will let you output variables as you create them, and also carry on using variables in the way that you are currently used to, without getting into complicated data store creation.
The code:
// include this function once in your code, at the top of the script,
// and outside of any other functions.
function v(value) {
var outputText = (typeof value === "object" && value.constructor === Object && window.JSON) ? JSON.stringify(value) : value;
document.body.appendChild(document.createTextNode(outputText));
document.body.appendChild(document.createElement("br"));
return value;
}
// When you create variables, use this function to assign and output the value.
// So instead of: var someVariable = "someValue";
// Use: var someVariable = v("someValue");
// this will output the value to the document,
// and still assign the variables as you'd expect.
// create and output a "greeting" variable
var greeting = v("Hello There!");
// create and output an array of primary colours
var primaries = v(["red", "green", "blue"]);
// alert the greeting - alerts: "Hello there!"
alert(greeting);
METHOD 2
This is more complicated but also more useful. It creates a data store object which both provides neat storage for your variables, and a few methods to set them with output, get them for use, delete them and output them all again. May be overkill for you but was great fun to write! :)
Here is a working example
The code:
// An object in which to store variables, which also writes variables to a given element as you create them.
// Include this code at the top of your javascript.
var VariableStore = function(outputElement) {
this.store = {};
this.outputElement = outputElement;
};
VariableStore.prototype = {
create : function(key, value) {
this.store[key] = value;
this.output(key);
},
get : function(key) {
return this.store[key];
},
destroy : function(key) {
delete this.store[key];
},
output : function(key) {
var value = this.store[key];
var outputText = (typeof value === "object" && value.constructor === Object && window.JSON) ? JSON.stringify(value) : value;
this.outputElement.appendChild(document.createTextNode(outputText));
this.outputElement.appendChild(document.createElement("br"));
},
outputAll : function() {
for(var key in this.store) {
this.output(key);
}
}
};
// Here's how to use the object above.
// 1. Create a new instance of VariableStore, here called v. This only needs to be done once in your script, just underneath the VariableStore object above.
var v = new VariableStore(document.body);
// 2. This creates three new variables. They will be automatically outputted to the output element when they are created. This code can go anywhere in your script underneath the first two steps.
// The arguments are: v.create("yourVariableName", "Your Variable Contents");
v.create("myName", "Zougen Moriver"); // a simple String
v.create("primaryColours", ["red", "green", "blue"]); // an array of primary colours
v.create("someObject", {"greeting":"Hi There!"}); // A friendly object literal
// if you need to delete a variable again, this deletes the primaryColours array for example
v.destroy("primaryColours");
// 3. You can retreive any of the variables you create using v.get("variableName"). Here, we retreive the "name" variable we just created and alert it.
alert(v.get("myName"));
// 4. If you want to output all the variables again, use v.outputAll();
v.outputAll();