Three operators that need attention in Javascript: Javascript Operators

Source: Internet
Author: User

Three operators that need attention in Javascript: Javascript Operators

I usually get used to C #, So I think what operators should be used for? But one day you get used to being overturned by other languages. I wonder if there is a strong curiosity.

In js, my habits have been overturned. Let's take a look at which operators have overturned my three views.

I: = Operator

= The reason why operators can be overturned can be seen in the following examples.

<1> "10" = 10?

If you put it in C #, the compiler will tell you bluntly that the types of the sub-accounts are different... But what will happen in JS?

From this, you can see that no matter whether you are curious or not, the answer is there. Someone may ask whether 10 is converted to 10 or 10, so this is also

It's where I keep talking. If it's C #, you can also see how to deal with it in IL, but you can't see anything in JS. You can only listen to one word in the textbook, cannot be seen as reality.

In addition to remembering, I can only say that the string "10" is converted to 10, and then the integer is compared.

<2> true = 1?

This problem can be understood in a bit. In the IL of C #, the values of true and false are converted to 1 and 0, therefore, js converts true to 1 during judgment and performs an integer comparison.

I think it's not uncommon to remember.

 

<3> {valueOf: function () {return "10" }}= = 10?

This is also a strange problem. Can the object be compared with the int type? But it can be done in JS. The principle is as follows. If an object is compared with int/string,

Js will give priority to calling the valueOf method, that is, to convert the object value. In fact, the fun thing here is that our custom valueOf overwrites the valueOf method of the parent class, so the above

The example is to judge "10" = 10 ?.

PS is required here. If the valueOf method is not defined in your class, the js internal engine will go to the toString () method again. If yes, it will be executed.

I don't know if you feel a little confused when you get started with these new features. It seems like a mess? If you are afraid that the program has potential bugs,

Convert to the same data type for comparison. Of course, there is a ===operator in js which can be said to be the closest to the logical operators in C, the extra "=" is in

"=" And then judge whether the types are equal? For example:

II: & | Operator

These two operators are amazing. In our habits of thinking, both operators should be of the bool type, but these definitions in JS will be thoroughly overturned. These new rules are exactly the same, so we can do a lot of new tricks, such as finding such traces everywhere in jquery's source code.

We have seen this sentence: bup = B & B. parentNode. Can you understand the meaning of this sentence? In fact, it means to determine whether B exists first. If B does not exist, then

Maybe B is undefined, null, 0 or NaN. if B exists, m. parentNode is returned. This is so fun. if you use C #, there will be a few if conditions,

It can also be a simplified code, and then let's look at | the operation, which can be described as a flood in the source code. However, at first glance, it is especially like a void operator in C, so close to each other

For example, ret = results | []. If results has a value, ret = results. If results is null, undefined, NaN, or 0, then result = [],

This kind of willfulness saves programmers A Lot Of if judgments. Finally, we need to add a sentence, just like in C #'s IL, that is, if judgments are made by the bottom layer of JS.

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.