Normally we can declare variable via:
var myVar1 = "Hello";
var myVar2 = true;
When put it into object way, it will look like this:
var globalVar = {
myVar1: "Hello",
myVar2: true
}
How can I declare a variable without assigning anything, e.g. var myVar3; in the object way?
Thank you.
2 Answers 2
How can I declare a variable without assigning anything, e.g. var myVar3; in the object way?
You can't. But you can get the same result:
var globalVar = {
myVar1: undefined,
myVar2: undefined
};
When you declare a variable with var, the variable's initial value is undefined. So the above does the same for object properties.
Comments
Using undefined is technically correct, but has little to no point, you'd be better off declaring the variable with a NULL consider this code:
var obj = {
oUn : undefined,
oNull : null
};
document.body.appendChild(document.createTextNode(
typeof obj.oUn
+ ' ' + typeof obj.oNull
+ ' ' + typeof obj.doesNotExist
));
Although yes, in FireBug, and maybe other debuggers (did not check) obj.oUn does come up, but if you try to check it within JS you get the same result as if its not even defined. (Note that the "typeof obj.doesNotExist" returned the same value as "typeof obj.oUn" although, it was not defined). Though that said if you check it within a for loop you do get it i.e. this will display the obj.Un
var obj = {
oUn : undefined,
oNull : null
};
document.body.appendChild(document.createTextNode(
typeof obj.oUn
+ ' ' + typeof obj.oNull
+ ' ' + typeof obj.doesNotExist
));
document.body.appendChild(document.createElement('br'));
var name;
for(name in obj) {
document.body.appendChild(document.createTextNode(name));
document.body.appendChild(document.createElement('br'));
}
So I guess it depends on why you need it. By in large I would say if you do not need the name per say (i.e. your code already knows the name) then do not even declare it and simply use
var globalVar = {};
You can always extend on it latter once you need it or if you need it, then use NULL, because you can actually check in JS if its in use by checking if the value is NULL or not. With undefined you'd have no way of knowing, because it does not differ at all from a value that is not in use and you'd need to make a cumbersome helper function in order to find out.
5 Comments
undefined. typeof makes no distinction between them, but nearly everything else does. (There are lots of things typeof doesn't distinguish, like Date vs RegExp.) But there is a fundamental difference between an object not having a problem and having the property with the value undefined, and that difference shows up nearly everywhere (for-in loops, the in operator, Object.keys, hasOwnProperty, ...).dynamicVariavbleName in obj to find out if obj has a property called dynamicVariavbleName. That will be true if the property exists (even if it has the value undefined), false if it doesn't. in will also check the object's prototype (like your code does). If you don't want to check the object's prototype, obj.hasOwnProperty("dynamicVariavbleName"). But in is closer to what you're doing now. Again, just to add tools! :-)dynamicVariavbleName in obj to find out if obj has a property called dynamicVariavbleName" is slightly misleading. What I meant was "...to find out if obj has a property with the name in the dynamicVariavbleName variable." So 'foo' in obj tells you whether obj has a property called foo. If you have a variable, f, with the contents "bar", then f in obj will check if obj has a property called bar. Best,
globalVar.myVar3 = null;