Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript determine whether an object exists?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how javascript determines whether an object exists or not. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Judgment methods: 1, use "if (! Obj)" sentence; 2, use "if (! window.Obj)"; 3, use "if (! this.Obj)"; 4, use "if (typeof Obj==" undefined ")"; 5, use "if (Obj==undefined)", and so on.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The method of judging the existence of an object by javascript

Now, we need to determine whether a global object myObj exists, and if not, declare it. The algorithms described in natural language are as follows:

If (myObj does not exist) {declare myObj;}

Here are 10 ways to implement it:

Method 1:

If (! myObj) {var myObj = {};}

Method 2:

In addition to the var command, there is another rewrite that can also get the correct result:

If (! window.myObj) {myObj = {};}

Window is the top-level object of javascript, and all global variables are its properties. Therefore, judging whether the myobj is empty is equivalent to judging whether the window object has a myobj attribute, so that you can avoid ReferenceError errors because the myObj is not defined. However, from the standpoint of the code, it is best to add var to the second line:

If (! window.myObj) {var myObj = {};}

Or write something like this:

If (! window.myObj) {window.myObj = {};}

Method 3:

The drawback of the above approach is that in some runtime environments (such as V8, Rhino), window is not necessarily the top-level object. Therefore, consider rewriting it as:

If (! this.myObj) {this.myObj = {};}

At the global variable level, the this keyword always points to the top-level variable, so it can be independent of different runtime environments.

Method 4:

However, the readability of the above is poor, and the direction of the this is variable and error-prone, so it is further rewritten:

Var global = this; if (! global.myObj) {global.myObj = {};}

It is much clearer to use the custom variable global to represent the top-level object.

Method 5:

You can also use the typeof operator to determine whether myObj is defined.

If (typeof myObj = = "undefined") {var myObj = {};}

This is the most widely used method to determine whether a javascript object exists or not.

Method 6:

Because the value of myObj is directly equal to undefined when it is defined but not assigned, the above writing can be simplified:

If (myObj = = undefined) {var myObj = {};}

There are two points to note here. First, the var keyword on the second line cannot be reduced, otherwise there will be a ReferenceError error. Secondly, undefined cannot add single or double quotation marks, because the data type undefined is compared here, not the string "undefined".

Method 7:

The above method still holds in the case of "exact comparison" (=):

If (myObj = undefined) {var myObj = {};}

Method 8:

According to the language design of javascript, undefined = = null, so comparing whether myObj is equal to null can also get the correct result:

If (myObj = = null) {var myObj = {};}

However, although the results are correct, semantically, this method of judgment is wrong and should be avoided. Because null refers to an empty object that has been assigned to null, that is, the object actually has a value, while undefined refers to an object that does not exist or is not assigned. Therefore, you can only use the "comparison operator" (= =) here, and if you use the "exact comparison operator" (= =) here, you will make an error.

Method 9:

You can also use the in operator to determine whether myObj is an attribute of the top-level object:

If (! ('myObj' in window)) {window.myObj = {};}

Method 10:

Finally, use the hasOwnProperty method to determine whether myObj is an attribute of the top-level object:

If (! this.hasOwnProperty ('myObj')) {this.myObj = {};} Thank you for reading! This is the end of the article on "how to judge whether an object exists in javascript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report