What is a potential pitfall with using typeof bar === "object"Ask a question
b are defined within the enclosing scope of the function, and since the line they are on begins with the
typeof a and
typeof b to both be undefined in the above example.
However, that is not the case. The issue here is that most developers incorrectly understand the statement
var a = b = 3; to be shorthand for:
var b = 3; var a = b;
But in fact,
var a = b = 3; is actually shorthand for:
b = 3; var a = b;
As a result (if you are not using strict mode), the output of the code snippet would be:
a defined? false b defined? true
But how can b be defined outside of the scope of the enclosing function? Well, since the statement
var a = b = 3; is shorthand for the statements
b = 3; and
var a = b;, b ends up being a global variable (since it is not preceded by the var keyword) and is therefore still in scope even outside of the enclosing function.
Note that, in strict mode (i.e., with use strict), the statement
var a = b = 3; will generate a runtime error of ReferenceError: b is not defined, thereby avoiding any headfakes/bugs that might othewise result. (Yet another prime example of why you should use use strict as a matter of course in your code!)
- 0c++: object declaration in header file
- 0using namespace std is bad
- 0C++: delete single object using delete
- 0Iterate over tokens using std::regex
- 0How to provide command line arguments using gdb within a bash script
- 0C++: avoid 'using namespace'
- 0C++11: What is possible to do with moved-from object
- 0typedef vs using in C++11
- 3Get String in YYYYMMDD format from JS date object?
- 1Get String in YYYYMMDD format from JS date object?
- 1Converting JS object to JSON string