本文介绍了检测未定义的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查JavaScript中的对象属性是否未定义的最佳方法是什么?
What's the best way of checking if an object property in JavaScript is undefined?
推荐答案
使用:
if (typeof something === "undefined") {
alert("something is undefined");
}
如果具有某些属性的对象变量可以使用如下相同的内容:
If an object variable which have some properties you can use same thing like this:
if (typeof my_obj.someproperties === "undefined"){
console.log('the property is not available...'); // print into console
}
这篇关于检测未定义的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!