How to distinguish between a variable that is not declared and a declared variable, but not assign any value?

In both scenarios, the variable type will be "undefined". But an undefined variable will throw an exception.

Is there an easy way to handle this?

+6
source share
4 answers

You can find the question (and my answer) in How to check if a variable or object is undefined? . In general, I consider any access to the "undeclared variable" as a programming error.

However, this particular case can be * only ** detected using detection to throw a ReferenceError . But yuck, yuck, yuck! Remember that variable declarations are a static lexical construct, ignoring the quirks with the variable properties of a global object.

ReferenceError , and now "strict", exist for some reason, and I suspect this is an XY problem. I do not even recommend using typeof for this purpose: fix the code typeof

Happy coding.


* It was pointed out that "variable" in window will [and only] work for global "variables" (they are really just properties that do not have to be qualified in all contexts).

+4
source

If the property we want to check in the object, whether it exists or not, even if it is undefined.

we will use one of them: 'prop' in obj (for checking properties from the prototype chain) or obj.hasOwnProperty ('prop')

we need to use the methods described above to check if a property exists as an access property that was not declared in the object, also returns undefined.

 var o={}; oc=undefined; oc===undefined; //is true oa===undefined; //is true as well even though c exists while a doesn't 

usually not a problem, since no one really declares undefined properties, but when this is done so.

 oc=''; //when it can be string or oc=null; //to clearly indicate that its nothing. then oc === undefined will return false! note!!! null == undefined //true while null === undefined //false that why use three equals to test 

For variables not declared and not inside the object. On access, the Compiler returns (link) an error. If this does not mean that it is regarded as a global property, the window object property has not been declared, at least in the entire parent scope, so it will be undefined just like oa was at the top. it will become window.prop .

 so x; //error but x=3; //no error assumed to be global object. just like o.abcd = 3; would... make(declare) a property abcd in object o valued(assigned) 3 all at once. 

To prevent properties from becoming a global variable, we use the var keyword inside a function , for example var k;

One thing you can do about this is to catch a reference error when you throw for a variable that does not exist and is considered the variable itself.

 try { x } catch(e){//code to run when x is not declared let alone defined.} 
+1
source

You can try :

 var a; try { a; alert('a'); } catch(e) { /* a not defined */ } try { b; alert('b'); } catch(e) { /* b not defined */ } alert('done'); 

Demo

0
source

Never try to access undeclared vars if you are writing pure JS. To avoid such errors (among many others), start LINTing your JS from http://www.jslint.com/ or http://jshint.com/ .

A good read to help you understand the LINT tools and the arguments behind their results is Crockford's book, JavaScript: The Good Parts ( http://www.amazon.com/gp/aw/d/0596517742 ).

0
source

Source: https://habr.com/ru/post/904720/


All Articles