I had a code problem when testing if some vars are empty or not, and decide to test it in a fiddle:
var result = "";
var Teste = new Object();
Teste.ObjectNew = new Object();
Teste.StringNew = new String();
Teste.NumberNew = new Number();
Teste.ArrayNew = new Array();
Teste.ObjectLiteral = {};
Teste.StringLiteral = "";
Teste.NumberLiteral = 0;
Teste.ArrayLiteral = [];
Teste.ObjectNull = Object(null);
Teste.StringNull = String(null);
Teste.NumberNull = Number(null);
Teste.ArrayNull = [null];
for (var i in Teste) {
if (Teste[i] == null) {
result += "<p>Type " + i + " is null: " + Teste[i] + "</p>";
} else {
result += "<p>Type " + i + " is not null: " + Teste[i] + "</p>";
}
}
document.getElementById("result").innerHTML = result;
<div id="result"></div>
The result is:
Type ObjectNew is not null: [object Object]
Type StringNew is not null:
Type NumberNew is not null: 0
Type ArrayNew is not null:
Type ObjectLiteral is not null: [object Object]
Type StringLiteral is not null:
Type NumberLiteral is not null: 0
Type ArrayLiteral is not null:
Type ObjectNull is not null: [object Object]
Type StringNull is not null: null
Type NumberNull is not null: 0
Type ArrayNull is not null:
I tested in Safari, same result.
I was coding in php altogether with JS and had problems in adjust my mind. In php, $var = array() returns NULL, but in JavaScript it seams there is never null value at any type. In EcmaScript definition, null is "primitive value that represents the intentional absence of any object value", but it seams impossible in JavaScript at list by my tests, excluding the case of v = null that i think is a Null type of var.
In addition, I believe AS3 follow the ecmascript concept by split type of for from it's values, the var statement "build" a var as an object apart from values.
So how do we correctly refer to a null value, if there is a way to?
EDIT
I did this test when I had this situation: I created a variable that has the relative directory of a graphic library. If this variable is null, it means I don't wish to change it from the default value (I have a table with default values) during initializing phase of my software, so the system just add the proper http base for the directory. If the variable is not null, it will assume the value was just assigned to it. But if it is an empty space, it means the directory is the root, but will be taken as null, generating an error.
Dinamyc:
var dir = new String(); // should be null
// initializing
dir = ""; // the directory will be the root
// finish ini
if(dir==null) … // assume the default value, but this doesn't work, so how can I know?