3 回答
TA贡献1887条经验 获得超5个赞
in
// global scopevar theFu; // theFu has been declared, but its value is undefinedtypeof theFu; // "undefined"
in
"theFu" in window; // true"theFoo" in window; // false
undefined
typeof
if (typeof myVar !== 'undefined')
typeof
undefined
undefined
window.undefined = "omg";"omg" == undefined // true
undefined
if (window.myVar)
false 0 "" NaN null undefined
if (myVariable)
ReferenceError
.
// abc was never declared.if (abc) { // ReferenceError: abc is not defined}
// or it's a property that can throw an errorObject.defineProperty(window, "myVariable", { get: function() { throw new Error("W00t?"); }, set: undefined });if (myVariable) { // Error: W00t?}
TA贡献1818条经验 获得超8个赞
myVar === undefined
===
==
myVar
typeof myVar === "undefined"
undefined
setTimeout
window.setTimeout = function () { alert("Got you now!");};
=== undefined
undefined
typeof
if (window.someVar === undefined) { doSomething();}
if (typeof myVar !== "undefined") { doSomething();}
var iAmUndefined;
in
if ("myVar" in window) { doSomething();}
in
=== undefined
typeof
TA贡献1862条经验 获得超6个赞
typeof
==
===
if
. (undefined
null
if (typeof someUndeclaredVariable == "undefined") { // Works}if (someUndeclaredVariable === undefined) { // Throws an error}
添加回答
举报