Home > Web Front-end > JS Tutorial > body text

Summary of tips for using Javascript conditional judgment_javascript tips

WBOY
Release: 2016-05-16 19:01:13
Original
1109 people have browsed it

Consider the following code

if (node.nextSibling.className == ...) {
...
}

in node or node.nextSibling is empty (null ), an error will be returned. So, the code for the usual solution is

if ((node) && (next = node.nextSibling) && ... ) {
...
}

Then, when the condition is judged to be one or more, the code will form the following situation

if (
(node) &&
(node.nextSibling) &&
(node.nextSibling) .className == ...)
... ) {
...
}

As the judgment conditions continue to increase, the code will become very "ugly" .

There is a small "trick" that can simplify the conditional judgment expression. We can add an empty object ({}) or zero (0) as an alternative

if ( next = (node ​​|| 0).nextSibling) ) {
...
}

Then, the above code can be written like this

if (((node ​​|| 0).nextSibling || 0).className == ...) {
...
}

--Split--

Personally, the above code will be very streamlined from a certain perspective. However, in the actual daily coding process, especially when multiple people cooperate, these codes may cause certain troubles to other developers.

As Xiao Ma said, if you are already using certain frameworks, specific issues need to be analyzed in detail. For example, the above conditional judgment code can be used using YUI coding.

YAHOO.util.Dom.hasClass(el, className)

appears to be more streamlined and easier than the above code. understand.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template