typeof x; // some string literal "string", "object", "undefined"if (typeof x === "string") { // === is redundant because we already know typeof returns a string literalif (typeof x == "string") { // sufficient
当涉及到对象属性时,需要考虑的另一件事是您是否真的想要检查undefined。给定的属性名称可以在对象上不存在(读取时产生值undefined),以值undefined存在于对象本身,以值undefined存在于对象的原型上,或者以非undefined值存在于其中任何一个上。'key' in obj会告诉您键是否位于对象原型链上的任何位置,Object.prototype.hasOwnProperty.call(obj, 'key')会告诉您它是否直接位于对象上。不过,我不会在这个答案中详细介绍原型和使用对象作为字符串键控映射,因为它主要是为了反驳其他答案中的所有糟糕建议,无论对原始问题的可能解释如何。
示例变量名的不寻常选择?这是来自Firefox的NoScript扩展的真正死代码。 ²不要假设不知道范围内的内容在一般情况下是可以的,不过。滥用动态范围引起的额外漏洞:Project Zero 1225 再一次假设ES5+环境,undefined指的是全局对象的undefined属性。
var hasUndefinedProperty = function hasUndefinedProperty(obj, prop){return ((prop in obj) && (typeof obj[prop] == 'undefined'));};
示例:
var a = { b : 1, e : null };a.c = a.d;
hasUndefinedProperty(a, 'b'); // false: b is defined as 1hasUndefinedProperty(a, 'c'); // true: c is defined as undefinedhasUndefinedProperty(a, 'd'); // false: d is undefinedhasUndefinedProperty(a, 'e'); // false: e is defined as null
// And now...delete a.c ;hasUndefinedProperty(a, 'c'); // false: c is undefined
var s; // UndefinedjQuery.isEmptyObject(s); // Will return true;
s = null; // Defined as nulljQuery.isEmptyObject(s); // Will return true;
//Usageif (jQuery.isEmptyObject(s)) {alert('Either variable:s is undefined or its value is null');} else {alert('variable:s has value ' + s);}
s = 'something'; // Defined with some valuejQuery.isEmptyObject(s); // Will return false;
// to test someObject.somePropertyvar descriptor = Object.getOwnPropertyDescriptor(someObject, 'someProperty');
if (typeof descriptor === 'undefined') {// was never initialized} else if (typeof descriptor.value === 'undefined') {if (descriptor.get || descriptor.set) {// is an accessor property, defined via getter and setter} else {// is initialized with `undefined`}} else {// is initialized with some other value}
containerObject = { propertyName: void "anything" }>> Object { propertyName: undefined }
// Now the testing
"propertyName" in containerObject && ""+containerObject["propertyName"] == "undefined";>> true
/* Which makes sure that nonexistent property will not return a false positive* unless it is previously defined */
"foo" in containerObject && ""+containerObject["foo"] == "undefined";>> false
var x;if (typeof x === 'undefined') {// these statements execute}
使用typeof的一个原因是,如果变量尚未声明。
// x has not been declared beforeif (typeof x === 'undefined') { // evaluates to true without errors// these statements execute}
if (x === undefined) { // throws a ReferenceError
}
var x;if (x === void 0) {// these statements execute}
// y has not been declared beforeif (y === void 0) {// throws a ReferenceError (in contrast to `typeof`)}
var handler = {get: function(target, name) {return name in target ? target[name] : 'N/A';}};
var p = new Proxy({}, handler);p.name = 'Kevin';console.log('Name: ' +p.name, ', Age: '+p.age, ', Gender: '+p.gender)
var boo ='lala';
function check(){if(this['foo']){console.log('foo is here');}else{console.log('have no foo');}
if(this['boo']){console.log('boo is here');}else{console.log('have no boo');}}
check();
function isUndefined(variable,defaultvalue=''){
if (variable == undefined ) return defaultvalue;
return variable;
}
var obj={und:undefined,notundefined:'hi i am not undefined'}
function isUndefined(variable,defaultvalue=''){
if (variable == undefined ){return defaultvalue;}return variable
}
console.log(isUndefined(obj.und,'i am print'))console.log(isUndefined(obj.notundefined,'i am print'))