Reputation: 2594
I have the function:
isset = function(obj){
alert(obj !== undefined);
}
Then when I do isset(defined_variable)
where defined_variable
has been declared, the alert box shows true but when I do isset(undefined_variable)
where undefined_variable
has not been declared, the alert box does not show at all, while I expected the alert box to show false. What am I doing wrong? I have tried using typeof
but the results are the same.
Upvotes: 3
Views: 1239
Reputation: 10849
When you dereference an undeclared variable (meaning you try to use a symbol that was never wrote before), you get a Reference error
.
There are several ways to deal with this, but you can't determine what are local variables in javascript. Thus, your problem can be solved dynamically only for global variables, or a given scope object.
You can prefix it with window
if you are in a browser context (for more context see this SO answer about global object in javascript)
This would not modify your isset
function code:
isset(window.undefined_variable)
There would be another way, which would need that isset
function changes, but uses the same principle (still in browser context):
isset('undefined_variable_name_wrawppped_in_a_string')
function isset(name) {
return name in window;
}
We can't really use typeof
in isset
, it's sad because it would be convenient since it doesn't throw a Reference Error
when the variable was never declared. We could still use a form of eval to do it, but since I don't want us to go there, I won't implement it.
But now, what if you want to check several nested properties?
function isset (memoryPath, root) {
var nodeNames = memoryPath.split('.');
var expectedDepthReached = nodeNames.length;
var depthReached = 0;
var nodeName;
var node = root;
// we are gonna read it from left to right
// using Array.prototype.pop()
// reversing it will alow us to do so
nodeNames.reverse();
do {
nodeName = nodeNames.pop();
node = node[nodeName];
if (node) {
depthReached++;
}
} while (node);
return depthReached === expectedDepthReached;
}
And an exemple:
window.myGlobals = {
path: {
to: {
a: {
variable: true
}
}
}
};
isset('myGlobals.path.to.a.variable', window), // true
isset('myGlobals.path.foo', window) // false
Upvotes: 1
Reputation: 174977
That's because there's a difference between undefined and undeclared.
var foo; // foo is undefined.
// bar is undeclared.
console.log(foo === undefined); // true
try {
console.log(bar === undefined);
} catch (e) {
console.error('I had an error!'); // gets invoked.
}
foo = 'something';
console.log(foo === undefined); // false
Upvotes: 4
Reputation: 68393
but when I do isset(undefined_variable) where udefined_variable has not been declared, the alert box does not show at all, while I expected the alert box to show false
because it throws an error in your console (check your console) that this variable that you are comparing is not defined.
Uncaught ReferenceError: 'c' is not defined
i tried with isset( c )
and c
was not declared first
Upvotes: 1