JavaScript jquery infers the way objects are empty

Source: Internet
Author: User

Java there are a lot of empty pointers to the problem, it is necessary to do the prevention and inference, if not, the console has annoying anomalies, so that the confidence has been hit, the early knock code when no experience, can not be based on abnormal information to find the source of the problem, the only thing to do is to pray, do not appear anything unusual information Ah! Now encountered with the problem, to hope that the more specific information, the better, but it backfired, we use the framework to put all the exception of all 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, it is not as convenient as the direct use of MyEclipse debugging Java code, Java inside inferred whether the string variable is empty , very easy:

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 infer whether the variable is empty, otherwise it will often appear undefined or null and so on.

The assumption is either a string type or an object type, which only needs to be used in this way:

if (str) {   //your code}

When str==undefined or Str==null or str== "" is returned false, but when str== "0" is returned true, because "0" is also a string, non-null string variable.

Suppose that when a variable is a numeric type, for example:

var i = 0;if (i) {   //your code}

This is the return false, JS is a weak type of language, for the string type and numeric type is not strict, that is, the variable defined by Var can be whatever type, just want you to initialize it, for example:

var arr = [];var str = "123"; var obj = {field: "123"};var i = 2;i = Arr;i = Str;i = obj;

Variable definitions are initialized later, what type of variable is, what type of variable it is, and then again, the type of the variable 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 not defined, 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 infers whether an object exists


The JQuery code infers whether an object exists:


Fatal error ***********************


if ($ ("#id")) {}else{} because $ ("#id") will return an object regardless of whether the objects exist.


Correct ***********************


if ($ ("#id"). length>0) {}else{} is inferred using the property length of the JQuery object, assuming that > 0 exists.


Or


if ($ ("#id") [0]) {} else {}


Or


Infer using native Javascript code directly: if (document.getElementById ("id")) {} else {}

////////////////////////////////////////////////////////////////////////////////

JavaScript jquery infers the way objects are empty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.