This time I will show you how to detect attributes in web development, and what are theprecautions for detecting attributes in web development. The following is a practical case, let's take a look.
The scenario where null (and undefined) is used is when checking whether a property exists in the
object, such as:
// 不好的写法:检测假值if (object[propertyName]) {}// 不好的写法:和null相比较if (object[propertyName] != null) {}// 不好的写法:和undefined比较if (object[propertyName] != undefined) {}
Copy after login
Each in the above code Judgment actually checks the value of the attribute through the given name, rather than judging whether the attribute pointed to by the given name exists, because when the attribute value is a falsy value, the result will be wrong, such as 0, " "(empty
String), false, null and undefined. After all, these are legal values for the property. For example, if the attribute records a number, the value can be zero. In this case, the first judgment in the above code will cause an error. By analogy, if the attribute value is null or undefined, all three judgments will cause errors.
The best way to determine whether an attribute exists is to use the in
operator. The in operator will simply determine whether the attribute exists, rather than reading the value of the attribute. This can avoid the ambiguous statements mentioned earlier in this section. The in operator will return true if the instance object's properties exist orinheritfrom the object's prototype. For example:
var object = { count: 0, related: null};// 好的写法if ("count" in object) { // 这里的代码会执行}// 不好的写法:检测假值if (object["count"]) { // 这里的代码不会执行}// 好的写法if ("related" in object) { // 这里的代码会执行}// 好的写法if (object["related"] != null) { // 这里的代码不会执行}
Copy after login
If you only want to check whether a certain property of the instance object exists, use the hasOwnProperty() method. All JS objects that inherit from Object have this method, which returns true if this property exists in the instance (if this property only exists in the prototype, it returns false). It should be noted that in IE8 and earlier versions of IE,
DOM objectdoes not inherit from Object, so it does not include this method. In other words, you should check whether the DOM object exists before calling its hasOwnProperty() method (if you already know that the object is not DOM, you can omit this step).
// 对于所有非DOM对象来说,这是好的写法if (object.hasOwnProperty("related")) { // 执行这里的代码}// 如果你不确定是否为DOM对象,则这样来写if ("hasOwnProperty" in object && object.hasOwnProperty("related")) { // 执行这里的代码}
Copy after login
Because of the situation of IE8 and earlier versions of IE, when judging whether the properties of the instance object exist, I prefer to use the in operator. HasOwnProperty(() is only used when judging the instance properties. ). Whenever you need to check for the existence of a property, use the in operator or hasOwnProperty(). Doing this can avoid many bugs.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to avoid null comparison in web development
How to detect arrays in web development
The above is the detailed content of How to detect properties in web development. For more information, please follow other related articles on the PHP Chinese website!