How javascript jquery judges an object as null
There are many null pointers in java, which need to be prevented and judged frequently. Otherwise, the console suffers annoying exceptions, causing confidence to be compromised and there is no experience in writing programs in the early days, you cannot find the root cause of the problem based on the exception information. The only thing you do is pray. Do not see any exception information! Now we have encountered and problems, and we hope that the more detailed the exception information, the better, but it is counterproductive. The framework we adopt is actually troublesome to get all the exceptions throws even if the controller gets them again, once a result is different from the Expected One, you do not know what the problem is, and debugging is really inconvenient.
The same is true in js. Although no exception code is displayed, the console still has an error warning. During this time, we have been using chrome and IE11 to debug js Code, I think it is as convenient as directly using MyEclipse to debug java code. It is very easy to judge whether the string variable is empty in java:
String str = "";if(str!=null&&!"".equals(str)){//your code}
Of course, you can also use the API on the framework to implement:
String str = "";if(StringUtils.isNotEmpty(str)){//your code}
StringUtils. isNotEmpty is the class under org. apache. commons. lang. This package should not be confused with spring.
In js, it is also often necessary to determine whether the variable is null, or else undefined or null may often occur.
For the string type or object type, you only need to use the following method:
if(str){ //your code}
If str = undefined or str = null or str = "", false is returned, but true is returned when str = "0, because "0" is also a string, it is a non-empty string variable.
If the variable is of the numerical type, for example:
var i = 0;if(i){ //your code}
In this case, false is returned. js is a language of weak type. The String type and numeric type are not strictly differentiated, that is, variables defined by var can be of any type, as long as you initialize it, for example:
var arr = [];var str = "123";var obj = {field:"123"};var i = 2;i = arr;i = str;i = obj;
Initialize a variable after it is defined. What type of value is the variable, what type of variable is the variable, and then assign a value again. The type of the variable will change again. Of course, you can understand it, the var type is the parent type of all types, so it can be "all-encompassing. therefore, the above Code is correct and will not cause errors. To make the code readable, we recommend that you do not assign values to variables of different types to avoid confusion.
Finally, a variable of the String type can be converted to a variable of another type, for example:
var str = "0";i = 0;alert(str == i);
Returns true.
When the variable is undefined, Initialization is performed directly, which can also be identified:
xx = 0;if("0"==xx){ alert(xx);}
If it is undefined and initialized and used directly, it will not work.
//////////////////////////////////////// //////////////////////////////////////// //
Jquery checks whether an object exists
JQuery Code determines whether an object exists:
**************** *******
If ($ ("# id") {} else {} Because $ ("# id") returns an object regardless of whether the object exists.
**************** *******
If ($ ("# id"). length> 0) {} else {} is determined by the length attribute of the jQuery object. if it is greater than 0, it exists.
Or
If ($ ("# id") [0]) {} else {}
Or
Directly use the native Javascript code to determine: if (document. getElementById ("id") {} else {}
//////////////////////////////////////// ////////////////////////////////////////