Java has a lot of empty pointers to the problem, it is necessary to do the prevention and judgment, if not, the console appears annoying anomalies, let people confidence has been hit, early writing procedures when there is no experience, can not find the source of the problem according to abnormal information, the only thing to do is to pray, do not appear anything unusual information Ah! Now encountered with the problem, to hope that the details of the more detailed the better, but it backfired, we adopted the framework incredibly all of the anomalies throws even if the controller gets more trouble, once the results are not the same as expected, do not know where there is a problem, debugging is really inconvenient.
JS is the same, although there will not be a large number of exception code, but the console will still have error warning, this time has been using Chrome and IE11 debugging JS Code, feel its convenience is as good as directly using MyEclipse debugging Java code, Java inside to determine whether the string variable is empty , very simple:
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 Org.apache.commons.lang. The class below the package, don't confuse it with spring.
In JS, it is often necessary to determine whether the variable is empty, otherwise it will often appear undefined or null and so on.
If it is a string type or an object type, this is the only way to do it:
if (str) { //your code}
It returns false when str==undefined or Str==null or str== "", but returns True when str== "0" because "0" is also a string, a non-null string variable.
If the variable is a numeric type, such as:
var i = 0;if (i) { //your code}
This is the return false, JS is a weak type of language, for string type and numeric type is not strict, that is, the variable defined by Var can be any type, as long as you initialize it, such as:
var arr = [];var str = "123"; var obj = {field: "123"};var i = 2;i = Arr;i = Str;i = obj;
Variable definitions are initialized later, what types of variables, what type of variables, and then re-assignment, the type of variables will change, of course, you can understand that the Var type is the parent type of all types, and therefore can be "all-encompassing". So the above code is correct, There is no error, but for the readability of the code, it is recommended that different types of variables not be assigned to each other to avoid confusion.
To conclude, a variable of type string can generally be converted to other types of variables such as:
var str = "0"; i = 0;alert (str = = i);
Returns True
When a variable is undefined, it is initialized directly and can be identified:
xx = 0;if ("0" ==xx) { alert (xx);}
If it is not defined and is initialized, it will not be used directly.
//////////////////////////////////////////////////////////////////////////////////
jquery Determines whether an object exists
The JQuery code determines whether an object exists:
Fatal error ***********************
if ($ ("#id")) {}else{} because $ ("#id") returns an object regardless of whether or not it exists.
Correct ***********************
if ($ ("#id"). length>0) {}else{} uses the properties of the JQuery object length to determine if > 0 exists.
Or
if ($ ("#id") [0]) {} else {}
Or
Use native Javascript code directly to determine: if (document.getElementById ("id")) {} else {}
////////////////////////////////////////////////////////////////////////////////
JavaScript jquery to judge the way the object is empty