Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

javascript - 检测未定义的对象属性(Detecting an undefined object property)

检查JavaScript中的对象属性是否未定义的最佳方法是什么?

  ask by Matt Sheppard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

(采用:)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...