编程小贴士

给你的编程提供小点子


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

78 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.

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>