方法一
根据您最近的 cmets,这将让您在创建变量时输出变量,并以您当前习惯的方式继续使用变量,而无需进行复杂的数据存储创建。
代码:
// 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);
方法 2
这更复杂但也更有用。它创建了一个数据存储对象,该对象既可以为您的变量提供整洁的存储,也可以使用一些方法来设置它们的输出、获取它们以供使用、删除它们并再次输出它们。对你来说可能有点矫枉过正,但写起来很有趣! :)
Here is a working example
代码:
// 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();