编程小贴士

给你的编程提供小点子


JavaScript运算符的工作机制

JavaScript中的每个运算符到底是如何进行类型转换的.下面会依次讲解六个最常用的运算符的工作机制:

typeof

typeof运算符会返回操作数类型的字符串表示.主要有两个需要注意的地方:

●未定义或未声明的变量将会返回”undefined”, 比如.如果a没有被声明,那么typeof a将会返回”undefined”.

●typeof在操作数是null或函数的种情况下会”撒谎”.

除了这些, 操作数和对应的类型字符串可以从下表查出:

Undefined                            ”undefined”

Null          ”object”

Boolean       “boolean”

Number       “number”

String        ”string”

Object,不可以被调用     “object”

Object,可以被调用      ”function”

我标记“⚠”的两处地方就是上面提到的typeof误导我们的地方:type of null应该返回”Null”,而任意函数的类型应该是”object”.

减法(-)

将两边的操作数都转换为数字. “8” – true 会被转换为 8 – 1.的确非常的简单,不过下面的加法可就不是了.

加法(+)

加法是JavaScript中最麻烦的运算符了.让我们看看在执行a + b的时到底发生了什么:

1. 两边的操作数首先被转换成原始值.这里我们称之为A 和 B.

2.如果有任意一个原始值是字符串,则把另一个也转换成字符串,执行A和B的连接操作并返回连接后的字符串.

3.否则把A和B都转换为数字,返回两个数字的和.

例如:

8 + “5″ ➙ “8″ + “5″ ➙ “85″; //”5″是字符串,所以把8也转换成字符串,连接后值为”85″

8 + true ➙ 8 + 1 ➙ 9; //没有字符串,两边都转换成数字,true转换成数字为1,返回相加的和9

“8″ + true ➙ “8″ + “true” ➙ “8true”; //”8″是字符串,把true也转换成字符串”true”,连接后值为”8true”

小于(<)

和加法运算符不同,小于运算符只在两个操作数都为字符串的时候才将他们作为字符串来比较.下面是正式的操作步骤:

1.两边的操作数都转换成原始值.这里我们称之为A 和 B.

2.如果这个两个原始值都为字符串,则把A和B按照字符串来比较.

3.否则将他们都转换位数字,按照数字大小比较.

例如:

8 > “5″ ➙ 8 > 5 ➙ true; //两边不都是字符串,”5″转换为数字5

8 > true ➙ 8 > 1 ➙ true; //两边不都是字符串,true转换成数字1

“8″ > “18″ ➙ true; //两边都是字符串,8的ascii码大于1的ascii码

严格相等(===)

许多运算符中最让人省心的一个,也被称为三等号(===),它的操作很简单:检查两个操作数的类型是否相等,如果相等的话,检查他们的值是否相等.他的兄弟运算符(==)就比较复杂了.

相等(==)

JavaScript中最让人讨厌的运算符.它的工作机制是这样的:

1.首先检查两个操作数的类型,如果他们是相同的类型,那么继续执行严格相等比较.

2.如果两个操作数都是null或者是undefined,返回true.

3.如果其中一个操作数是字符串另外一个是数字,则将他们都转换数字,再执行严格相等比较.

4.如果其中一个操作数是布尔值,把它转换成数字,然后回到步骤1继续执行.

5.如果其中一个操作数是字符串或者数字,另外一个是对象值,把这个对象值转换成原始值,然后回到步骤1继续执行.

6.返回false.

这基本上意味着,如果两个操作数的类型不同,则判断的工作机制类似于小于<比较,如果类型相同,则类似于严格相等比较.总结一下就 是:当类型不同时,将两个操作数都转换为原始值,除非两个原始值都是字符串,否则再次将两个原始值转换成数字再比较,还有就是null == undefined是true.

8 == “5″ ➙ 8 == 5 ➙ false; //”5″转换成数字5

1 == true ➙ 1 == 1 ➙ true; //true转换成数字1

0 == “” ➙ 0 == 0 ➙ true; //””转换成数字0

0 == “0″ ➙ 0 == 0 ➙ true; //”0″转换成数字0

“” == “0″ ➙ false; //字符串直接判断是否相等

“1000″ == “1e3″ ➙ false;        //字符串直接判断是否相等

1000 == “1e3″ ➙ true; //”1e3″转换成1000,科学计数法

5 == {valueOf: function () { return 5; }} ➙ 5 == 5 ➙ true; //对象值转换成原始值5

157 Responses to “ JavaScript运算符的工作机制 ”

  1. see pron说道:

    xTtepH Your style is unique compared to other folks I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just bookmark this page.

  2. girl coloring说道:

    This website is really good! How can I make one like this !

  3. My Face Gifts说道:

    The Internet is like alcohol in some sense. It accentuates what you would do anyway. If you want to be a loner, you can be more alone. If you want to connect, it makes it easier to connect.

  4. visit website说道:

    Your mode of explaining the whole thing in this post is in fact good, every one be able to simply be aware of it, Thanks a lot.

  5. Oluwadamilare说道:

    You ave made some really good points there. I looked on the web for additional information about the issue and found most individuals will go along with your views on this web site.

  6. This is a good tip especially to those new to the blogosphere. Brief but very precise information Appreciate your sharing this one. A must read post!

  7. omlet arcade说道:

    It as not that I want to replicate your internet site, but I really like the design. Could you let me know which style are you using? Or was it especially designed?

  8. buy说道:

    nonetheless, you command get bought an shakiness over

  9. Thanks for the article post.Thanks Again. Keep writing.

  10. mca motor club说道:

    My brother suggested I may like this website. He used to be totally right.

  11. go nhua da nang说道:

    Im grateful for the article post. Much obliged.

  12. gia sat thep说道:

    Thanks so much for the blog.Thanks Again. Keep writing.

  13. Very good information. Lucky me I found your website by accident (stumbleupon). I have bookmarked it for later!

  14. Green Card说道:

    Way cool! Some very valid points! I appreciate you writing this article plus the rest of the site is also very good.

  15. cay thong noel说道:

    Very neat article post.Really thank you! Will read on

  16. Thanks so much for the blog post. Really Cool.

  17. MP3 Downloads说道:

    Wow, awesome blog format! How long have you been running a blog for? you make blogging glance easy. The entire glance of your website is magnificent, let alone the content material!

  18. visit website说道:

    Informative article, just what I was looking for.

  19. this website说道:

    very good put up, i definitely love this website, carry on it

  20. to my followers! Excellent blog and outstanding design.

  21. Some really select articles on this site, saved to fav.

  22. play说道:

    You are my inhalation , I own few web logs and occasionally run out from to post.

  23. Supermodel Elena说道:

    This can be a list of words, not an essay. you are incompetent

  24. girls说道:

    This very blog is no doubt interesting additionally amusing. I have picked a bunch of useful tips out of this amazing blog. I ad love to visit it over and over again. Thanks a bunch!

  25. Thanks for sharing, this is a fantastic article.Really thank you! Fantastic.

  26. You made some decent points there. I looked on line for that issue and identified a lot of people will go coupled with with all your website.

  27. my page说道:

    Looking forward to reading more. Great blog article.Really thank you! Will read on

  28. School admission说道:

    wow, awesome blog post.Much thanks again. Will read on

  29. Thanks a million and please carry on the gratifying work.

  30. Open heaven说道:

    You ave made some good points there. I checked on the internet for more information about the issue and found most people will go along with your views on this site.

  31. It is really a nice and helpful piece of information. I am satisfied that you just shared this helpful tidbit with us. Please stay us up to date like this. Thank you for sharing.

  32. tips de ventas说道:

    May I simply just say what a relief to find someone that truly understands what they

  33. I really love I really love the way you discuss this kind of topic.~; a.~

  34. Usually I do not learn post on blogs, but I would like to say that this write-up very pressured me to try and do so! Your writing taste has been amazed me. Thank you, very nice article.

  35. pool builders说道:

    Thanks for all your efforts that you have put in this. Very interesting info. A good man can be stupid and still be good. But a bad man must have brains. by Maxim Gorky.

  36. manna说道:

    Very neat blog post.Really thank you! Really Cool.

  37. well defined说道:

    please go to the web pages we comply with, like this one, as it represents our picks in the web

  38. browse说道:

    You know so much its almost tough to argue with you (not that I personally

  39. There is a bundle to find out about this. You made nice points also.

  40. go to see说道:

    It as not that I want to replicate your web page, but I really like the pattern. Could you let me know which theme are you using? Or was it custom made?

  41. for details说道:

    pretty practical stuff, overall I believe this is worth a bookmark, thanks

  42. Thanks for sharing, this is a fantastic blog post.Thanks Again. Really Great.

  43. Major thanks for the post. Really Great.

  44. Say, you got a nice blog.Really looking forward to read more. Will read on

  45. In it something is. Thanks for the help in this question, the easier, the better ?

  46. Please forgive my English.It as really a great and helpful piece of information. I am glad that you shared this useful info with us. Please stay us informed like this. Thanks for sharing.

  47. Very nice post. I just stumbled upon your blog and wanted to say

  48. go there说道:

    When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.

  49. I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Google. You have made my day! Thanks again

  50. My brother smiled and told me to see your post, and I want to thank you for putting up a very important information here. Thank you!

  51. other details说道:

    I value the article post.Really looking forward to read more. Want more.

  52. good day说道:

    Really informative article post.Thanks Again. Much obliged.

  53. some really interesting points you have written.

  54. Thank you for the auspicious writeup. It in fact was a amusement account it.

  55. I value the blog post.Really looking forward to read more.

  56. wow, awesome blog article.Thanks Again. Really Cool.

  57. Very informative blog article.Really looking forward to read more. Will read on

  58. you are stating and the best way by which you assert it.

  59. Thanks so much for the blog post. Awesome.

  60. Wow, what a video it is! Really fastidious quality video, the lesson given in this video is actually informative.

  61. Thank you for that high-quality content. I like your blog and I hope you will keep posting so often in soon future.

  62. I value the post.Really thank you! Cool.

  63. visit website说道:

    Thanks for sharing, this is a fantastic post.Much thanks again. Awesome.

  64. It as not that I want to duplicate your web site, but I really like the design. Could you tell me which style are you using? Or was it custom made?

  65. Uborka kvartir说道:

    Thanks for sharing, this is a fantastic article post.Really thank you! Fantastic.

  66. bo dam说道:

    The website loading speed is incredible.

  67. written. In my opinion, it might bring your website a little bit more interesting.

  68. Real nice post, i hope you keep posting stuffs like this, thumbs up.

  69. jimmyjohncodes说道:

    This website definitely has all of the information and facts I wanted about this subject and didn at know who to ask.

  70. Really appreciate you sharing this article post.Really thank you! Will read on

  71. chocopie说道:

    NolimH Very good blog article.Really thank you! Really Great.

  72. best pron说道:

    hgNkeQ Woman of Alien Fantastic perform you might have accomplished, this page is really amazing with amazing facts. Time is God as strategy for holding almost everything from occurring at once.

  73. Yeah bookmaking this wasn at a bad conclusion great post!

  74. Keep up the superb piece of work, I read few articles on this web site and I think that your web blog is very interesting and has got sets of great info.

  75. Very good information. Lucky me I found your site by accident (stumbleupon). I ave bookmarked it for later!

  76. Read, of course, far from my topic. But still, we can work together. How do you feel about trust management?!

  77. informative. I appreciate you spending some time and energy to put this informative article together.

  78. bistro masa说道:

    kindle fire explained by Amazon CEO Jeff Bezos Got An kindle fire specs Idea ? In This Case Study This.

  79. link vao 188bet说道:

    Thanks so much for the blog.Really thank you! Fantastic.

  80. Beingbar eyewear说道:

    Wow, what a video it is! In fact good feature video, the lesson given in this video is in fact informative.

  81. king karaoke说道:

    This awesome blog is no doubt awesome additionally informative. I have chosen helluva helpful things out of this amazing blog. I ad love to go back again soon. Cheers!

  82. collegian说道:

    we came across a cool site that you simply may appreciate. Take a appear for those who want

  83. scammer说道:

    Is there a mint app for UK people that links into your bank? Thanks

  84. Wow, that as what I was seeking for, what a material! existing here at this website, thanks admin of this website.

  85. travel说道:

    Some truly nice stuff on this website , I it.

  86. Very nice info and straight to the point. I don at know if this is truly the best place to ask but do you guys have any ideea where to hire some professional writers? Thx

  87. Some in truth exciting points you have written.Assisted me a lot, just what I was looking on behalf of.

  88. Major thanks for the article post. Really Great.

  89. It is actually a great and useful piece of information. I am glad that you simply shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

  90. Im grateful for the post.Really looking forward to read more. Really Great.

  91. Wonderful article! We are linking to this particularly great content on our site. Keep up the good writing.

  92. Chicago Escorts说道:

    Wow! This could be one particular of the most helpful blogs We ave ever arrive across on this subject. Basically Excellent. I am also an expert in this topic therefore I can understand your effort.

  93. Miami Escorts说道:

    Pretty nice post. I just stumbled upon your blog and wanted to say that I ave truly enjoyed browsing your blog posts. In any case I all be subscribing to your feed and I hope you write again soon!

  94. This is a topic that as near to my heart Take care! Exactly where are your contact details though?

  95. Right now it appears like Movable Type is the top blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  96. The Lost Ways说道:

    There is visibly a lot to identify about this. I consider you made certain good points in features also.

  97. This awesome blog is definitely interesting and also amusing. I have picked up a bunch of helpful stuff out of this blog. I ad love to come back again soon. Thanks a bunch!

  98. escorts houston说道:

    You made some good points there. I checked on the web for more info about the issue and found most people will go along with your views on this site.

  99. Well I sincerely liked reading it. This post procured by you is very useful for proper planning.

  100. Indiatimes说道:

    very few internet sites that happen to become comprehensive below, from our point of view are undoubtedly very well worth checking out

  101. Muchos Gracias for your blog post. Really Great.

  102. Latest news说道:

    Only a smiling visitor here to share the love (:, btw outstanding style and design.

  103. 먹튀검증说道:

    Your style is really unique in comparison to other people I ave read stuff from. Thanks for posting when you ave got the opportunity, Guess I will just bookmark this page.

  104. Thank you ever so for you article post.Much thanks again.

  105. Really enjoyed this post.Much thanks again. Want more.

  106. Superb post here, thought I could learn more from but we can learn more from this post.

  107. viagra 100 mg说道:

    What type of digicam was used? That is definitely a really good good quality.

  108. purchase music说道:

    You made some clear points there. I looked on the internet for the topic and found most persons will agree with your blog.

  109. Admission说道:

    Well I definitely enjoyed reading it. This article provided by you is very helpful for proper planning.

  110. purchase music说道:

    Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Excellent. I am also an expert in this topic therefore I can understand your effort.

  111. My brother suggested I might like this blog. He was totally right. This post truly made my day. You cann at imagine simply how much time I had spent for this info! Thanks!

  112. Just a smiling visitant here to share the love (:, btw outstanding style.

  113. Really appreciate you sharing this article.Thanks Again. Much obliged.

  114. 토토 사이트说道:

    You ave made some decent points there. I checked on the internet to find out more about the issue and found most individuals will go along with your views on this website.

  115. port renfrew说道:

    It as not that I want to duplicate your web site, but I really like the style and design. Could you tell me which style are you using? Or was it custom made?

  116. 다잡아说道:

    Thanks so much for the post.Really thank you! Really Great.

  117. I will immediately seize your rss feed as I can not to find your e-mail subscription link or newsletter service. Do you ave any? Kindly allow me recognize in order that I may just subscribe. Thanks.

  118. Since the admin of this website is working, no

  119. My brother suggested I might like this web site. He was totally right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!

  120. The Silent Shard This may almost certainly be really beneficial for many of one as job opportunities I plan to never only with my blog but

  121. You have made some really good points there. I checked on the net for more information about the issue and found most people will go along with your views on this website.

  122. It as really a great and useful piece of info. I am glad that you simply shared this helpful

  123. You made some first rate factors there. I seemed on the web for the problem and located most people will associate with along with your website.

  124. 다자바说道:

    Im obliged for the blog.Thanks Again. Really Cool.

  125. 바다이야기说道:

    Incredible points. Outstanding arguments. Keep up the amazing spirit.

  126. 먹튀검증단说道:

    I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You ave made my day! Thank you again

  127. It as hard to search out educated individuals on this matter, however you sound like you understand what you are speaking about! Thanks

  128. Me English no outstanding, but had to say me like what you say. Thank you from me.

  129. Carisa Devito说道:

    Oh my goodness! an incredible article dude. Thanks a ton However We are experiencing issue with ur rss . Don’t know why Cannot enroll in it. Can there be any person finding identical rss dilemma? Anyone who knows kindly respond. Thnkx

  130. I would like to get more information on this topic. Can you help me with the same.

  131. Venetta Pettus说道:

    I am now not sure the place you are getting your info, but great topic. I must spend a while learning more or figuring out more. Thanks for magnificent info I used to be searching for this info for my mission.

  132. Allyson Tegan说道:

    After study a handful of the blog posts in your site now, we truly appreciate your method of blogging. I bookmarked it to my bookmark website list and will also be checking back soon. Pls take a look at my site also and tell me how you feel.

  133. Domenic Crosser说道:

    I am curious to find out what blog platform you are working with? I’m having some small security issues with my latest website and I would like to find something more safeguarded. Do you have any suggestions?

  134. Theresa Truka说道:

    bluetooth headsets are great because they are wireless and your movement is not limited by wires”

  135. ppi claims ireland How can u add a flash slide show to your blogspot?

  136. Hazel Montandon说道:

    too much vitamin-a can also cause osteoporosis but aging is the number cause of it.

  137. I see something truly special in this website.

  138. 토토 사이트说道:

    We all talk just a little about what you should talk about when is shows correspondence to because Perhaps this has much more than one meaning.

  139. small seo tool说道:

    This web site really has all of the info I wanted about this subject and didnaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžt know who to ask.

  140. 토토 사이트说道:

    This particular blog is no doubt educating additionally amusing. I have chosen a lot of handy advices out of this blog. I ad love to go back every once in a while. Cheers!

  141. 바다 이야기说道:

    usually posts some really exciting stuff like this. If you

  142. Denny Leith说道:

    very nice post, i actually love this website, carry on it

  143. 다잡아说道:

    There is definately a great deal to learn about this issue. I like all the points you ave made.

  144. You made some clear points there. I did a search on the issue and found most guys will go along with with your blog.

  145. Ray Baars说道:

    Hello! Nice stuff, please do inform us when you post something like that!

  146. Thanks so much for the article. Awesome.

  147. Louis Fekete说道:

    I went over this web site and I think you have a lot of superb info , saved to my bookmarks (:.

  148. Merlin Rustrian说道:

    I love putting together useful information, this particular publish has got me personally a lot more info!

  149. 토토사이트说道:

    What blog hosting website should I create a blog on?

  150. I truly appreciate this post. I?аАТ’аЂа†ve been looking everywhere for this! Thank goodness I found it on Bing. You ave made my day! Thanks again

  151. Marcelino Froio说道:

    Total Control Marketing Review- Maintain up the beneficial work mate. This web site publish shows how well you realize and know this subject.

  152. xlxx porn说道:

    This awesome blog is without a doubt cool additionally informative. I have picked up a bunch of handy advices out of it. I ad love to go back again soon. Thanks a bunch!

  153. Sheri Wilczynski说道:

    I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thank you again!

  154. Tattoo Artist说道:

    pretty practical stuff, overall I imagine this is worthy of a bookmark, thanks

  155. home declutter说道:

    person supply on your guests? Is going to

  156. Muchos Gracias for your blog.Much thanks again. Much obliged.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>