Skillful use of JavaScript logical operator _javascript technique

Source: Internet
Author: User
Domobj.onkeydown = function (e) {
 if (e) {
 e ...
 } else{
 event ...
 }

The above example: if the Firefox browser executes the statement within if, that is, use the E parameter to refer to the "event object".

If the IE browser executes the statement in else, it uses event to refer to the object.

This is an if judgment method that is used for compatibility with two browsers to use event objects.

Domobj.onkeydown = function (e) { 
 var oevent = e| | event; 
 Oevent ...
}

Example: By using the attribute of the "or" logical operator, it is automatically judged that the reference to the event object should be used.

Explanation: E | | event;

If it is IE browser, then E can not be used as a reference to the event object, False. Event is true, then event will assign value to var oevent = event;

On the contrary, in Firefox, the event cannot be used as a reference to the events object, False. E is true, then E will assign value to var oevent = e;

So no matter under that browser, Oevent can refer to the event object correctly! This style of writing is more elegant than the previous if () {}else{}!

The above is the entire content of this article, I hope to help you, thank you for the support of cloud habitat community!

Related Article

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.