{"id":995,"date":"2022-04-10T17:30:07","date_gmt":"2022-04-10T09:30:07","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=995"},"modified":"2023-02-16T14:39:35","modified_gmt":"2023-02-16T06:39:35","slug":"3-javascript-zhong-de-shu-ju-lei-xing","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=995","title":{"rendered":"3 &#8211; JavaScript \u4e2d\u7684\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16495845248740.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/types-in-javascript-2go1\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/types-in-javascript-2go1<\/a><\/p>\n<p>\u4efb\u4f55\u65f6\u5019\uff0c\u4f60\u7684\u503c\u53ef\u4ee5\u5b58\u50a8\u5728\u4e00\u4e2a\u53d8\u91cf\u91cc\u6216\u8005\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e9b\u503c\u5728 JavaScript \u4e2d\u6709 7 \u79cd\u7c7b\u578b\u3002\u4e0b\u9762\u6765\u4e00\u4e00\u4ecb\u7ecd\uff1a<\/p>\n<h3><a id=\"string%E9%80%9A%E5%B8%B8%E7%94%A8%E4%BA%8E%E6%96%87%E6%9C%AC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>String(\u901a\u5e38\u7528\u4e8e\u6587\u672c)<\/h3>\n<pre><code class=\"language-plain_text\">   `Mary` is a string surrounded by backticks\n   'John' is a string surrounded by single quotes\n   &quot;Parwinder&quot; is a string surrounded by double quotes\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4e09\u4e2a\u4f8b\u5b50\u90fd\u662f\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u53cd\u5f15\u53f7\u7684\u4f18\u52bf\u662f\u5141\u8bb8\u4f60\u521b\u9020\u591a\u884c\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-plain_text\">   `This is an\n   example of\n   a multi\n   line string`\n<\/code><\/pre>\n<p>\u53cd\u5f15\u53f7\u66f4\u9177\u7684\u529f\u80fd\u662f\u5141\u8bb8\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u6267\u884c\u8868\u8fbe\u5f0f\u3002\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32\u8fde\u63a5\u52a8\u6001\u503c\u65f6\u4e0d\u9700\u8981\u52a0\u53f7\u8fd0\u7b97\u7b26\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const name = &quot;Parwinder&quot;;\n   console.log(`Hello my name is ${name}`); \/\/ Hello my name is Parwinder\n<\/code><\/pre>\n<h3><a id=\"number%E5%B8%A6%E6%88%96%E8%80%85%E4%B8%8D%E5%B8%A6%E5%B0%8F%E6%95%B0%E7%BB%9F%E7%A7%B0-numbers\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Number(\u5e26\u6216\u8005\u4e0d\u5e26\u5c0f\u6570\u7edf\u79f0 numbers)<\/h3>\n<p>Numbers \u662f\u4e0d\u5e26\u5f15\u53f7\u7684\uff0cJavaScript \u63d0\u4f9b\u4e86\u8bb8\u591a\u5e2e\u52a9\u65b9\u6cd5\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b Math \u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-plain_text\">   Math.round(20.5) =&gt; 21\n   Math.round(20.1) =&gt; 20\n   Math.floor(20.7) =&gt; 20\n   Math.ceil(20.1) =&gt; 21\n   Math.random =&gt; 0.454646447863 \/\/ A random number between 0 and 1\n<\/code><\/pre>\n<p>\u4e00\u4e2a JavaScript \u4e2d\u5e38\u89c1\u7684\u5173\u4e8e\u6570\u503c\u76f8\u52a0\u7684\u9762\u8bd5\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"language-plain_text\">console.log(0.1 + 0.2) \/\/ 0.30000000000000004\n<\/code><\/pre>\n<p>\u662f\u7684\uff0c\u7ed3\u679c\u5c31\u662f <strong>0.30000000000000004<\/strong> \u800c\u4e0d\u662f <strong>0.3<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a\u6d4f\u89c8\u5668\u6267\u884c\u4e86\u6d6e\u70b9\u6570\u503c\u8fd0\u7b97\uff0c\u4ece\u8fd9\u4e2a<a href=\"https:\/\/0.30000000000000004.com\/\" target=\"_blank\" rel=\"noopener\">\u94fe\u63a5<\/a>\u6765\u66f4\u6df1\u5165\u7684\u4e86\u89e3\u3002<\/p>\n<p>\u53e6\u5916\uff0c<strong>NaN<\/strong> \u662f\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u7684\u7279\u6b8a\u503c\u4ee3\u8868\u975e\u6570\u503c\u7c7b\u578b\u3002\u5f53\u4f60\u7528\u4e00\u4e2a\u6570\u503c\u9664\u4ee5\u5b57\u7b26\u4e32\u65f6\uff1a<\/p>\n<pre><code class=\"language-plain_text\">console.log(12\/'a') \/\/ NaN\n<\/code><\/pre>\n<h3><a id=\"boolean%EF%BC%88%E7%94%A8%E6%9D%A5%E4%BB%A3%E8%A1%A8-true%E6%88%96-false%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Boolean\uff08\u7528\u6765\u4ee3\u8868 true \u6216 false\uff09<\/h3>\n<p>\u6bd4\u5982\u51fa\u73b0\u7684\u7528 true \u8868\u793a\uff0c\u672a\u51fa\u73b0\u7684\u7528 false \u8868\u793a\u3002Boolean \u503c\u4e00\u822c\u4f5c\u4e3a\u6bd4\u8f83\u8fd0\u7b97\u7684\u7ed3\u679c\uff0c\u4f8b\u5982\uff1a<strong>isAge &gt; 19<\/strong> \u6839\u636e\u5e74\u9f84\u8f93\u51fa true \u6216 false\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u8c08\u5230\u6bd4\u8f83\u6216\u8005 boolean \u65f6\uff0c\u4e00\u822c\u90fd\u4f1a\u4ecb\u7ecd <strong>==<\/strong> \u548c <strong>====<\/strong>\uff1a<\/p>\n<pre><code class=\"language-plain_text\">   console.log(10 == &quot;10&quot;); \/\/ true\n   console.log(10 === &quot;10&quot;) \/\/ false\n<\/code><\/pre>\n<p><strong>==<\/strong> \u53ea\u505a\u503c\u7684\u6bd4\u8f83\u800c <strong>===<\/strong> \u8fd8\u505a\u7c7b\u578b\u7684\u6bd4\u8f83\u3002<\/p>\n<h3><a id=\"null\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Null<\/h3>\n<p>\u5728 JavaScript \u4e2d\u6709\u4e24\u79cd\u65b9\u6cd5\u6807\u8bc6\u201c\u7a7a\u201d\uff0cnull \u548c undefined\u3002Null \u662f\u6211\u4eec\u6709\u4e00\u4e2a\u53d8\u91cf\u5e76\u4e14\u6211\u4eec\u7ed9\u5b83\u8d4b\u503c\u4e86\u4e00\u4e2a null &#8212; \u6211\u4eec\u663e\u5f0f\u7684\u7ed9\u53d8\u91cf\u8d4b\u4e86\u7a7a\uff1a<\/p>\n<pre><code class=\"language-plain_text\">   let dog = null;\n   console.log(dog); \/\/ null\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16495997435311.jpg\" alt=\"\" \/><\/p>\n<p>\u6709\u8da3\u7684\u662f\uff0cnull \u5728 JavaScript \u4e2d\u7684\u7c7b\u578b\u662f\u5bf9\u8c61\u3002<\/p>\n<h3><a id=\"undefined\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Undefined<\/h3>\n<p>\u5728 JavaScript \u4e2d\u8868\u793a\u7a7a\uff0c\u4f8b\u5982\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u4f46\u4e0d\u8d4b\u503c\uff0c\u5b83\u7684\u7ed3\u679c\u5c31\u662f undefined:<\/p>\n<pre><code class=\"language-plain_text\">   let dog;\n   console.log(dog); \/\/ undefined\n<\/code><\/pre>\n<h3><a id=\"symbol\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Symbol<\/h3>\n<p>JavaScript \u65b0\u6dfb\u52a0\u7684\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7528\u6765\u83b7\u5f97\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u3002\u6bcf\u4e00\u4e2a\u4ece Symbol() \u8fd4\u56de\u7684\u503c\u90fd\u662f\u552f\u4e00\u7684\uff0c\u5b83\u4e00\u822c\u7528\u6765\u4f5c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u7684\u552f\u4e00\u6807\u8bc6\u3002\u4f20\u9012\u7ed9 Symbol() \u7684\u53c2\u6570\u53ea\u7528\u4e8e\u6807\u8bc6\uff0c\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\u4f7f\u7528\u3002\u5c31\u7b97\u503c\u76f8\u540c Symbol() \u8fd4\u56de\u7684\u7ed3\u679c\u4e5f\u4e0d\u540c\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let id1 = Symbol(&quot;id&quot;);\nlet id2 = Symbol(&quot;id&quot;);\n\nalert(id1 == id2); \/\/ false\n<\/code><\/pre>\n<p>\u5bf9\u8c61\u5b57\u9762\u91cf\u6dfb\u52a0 Symbol() \u8fd4\u56de\u7684\u6807\u8bc6\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let id = Symbol(&quot;id&quot;);\n\nlet user = {\n  name: &quot;John&quot;,\n  [id]: 123 \/\/ not &quot;id&quot;: 123\n};\n<\/code><\/pre>\n<p>\u4e00\u5b9a\u8981\u52a0\u4e2d\u62ec\u53f7\uff0c\u662f\u4e3a\u4e86\u548c\u5b57\u7b26\u4e32 &#8216;id&#8217; \u7684\u533a\u5206\uff0c\u53e6\u5916 <strong>for..in<\/strong> \u548c <strong>Object.keys()<\/strong> \u4f1a\u5ffd\u7565\u5b83\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let id = Symbol(&quot;id&quot;);\nlet user = {\n  name: &quot;John&quot;,\n  age: 30,\n  [id]: 123\n};\n\nfor (let key in user) alert(key); \/\/ name, age (no symbols)\n\n\/\/ the direct access by the symbol works\nalert( &quot;Direct: &quot; + user[id] );\n\nObject.keys(user) \/\/ ['name','age']\n<\/code><\/pre>\n<h3><a id=\"object\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Object<\/h3>\n<p>JavaScript \u4e2d\u4e00\u5207\u7686\u5bf9\u8c61\uff0c\u5bf9\u8c61\u5b57\u9762\u91cf\u4e00\u822c\u8868\u793a\u6570\u636e\u7684\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-plain_text\">   const person = {\n       first: &quot;Parwinder&quot;,\n       last: &quot;Bhagat&quot;,\n       age: 33\n   }\n\n   console.log(person.first); \/\/ Parwinder\n   console.log(person.first); \/\/ Bhagat\n   console.log(person.age); \/\/ 33\n   console.log(person.income); \/\/ undefined\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/types-in-javas&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[6,89],"class_list":["post-995","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-javascript","tag-symbol"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":3,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions\/998"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}