{"id":102,"date":"2018-11-21T23:15:39","date_gmt":"2018-11-21T15:15:39","guid":{"rendered":"http:\/\/h-k.pw\/?p=102"},"modified":"2018-11-21T23:15:39","modified_gmt":"2018-11-21T15:15:39","slug":"javascript","status":"publish","type":"post","link":"https:\/\/harson.co\/index.php\/2018\/11\/21\/javascript\/","title":{"rendered":"JavaScript"},"content":{"rendered":"<p>\u5728HTML&lt;head&gt;\u7684\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u8bed\u53e5\u6765\u5f15\u5165.js \u6587\u4ef6<\/p>\n<p>&lt;script src=&#8221;&#8230;&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728JS\u4e2d if \u8bed\u53e5\uff0c\u5faa\u73af\u8bed\u53e5\u548cC \u51e0\u4e4e\u4e00\u6837<\/p>\n<p>\u8fd9\u662fJS \u4e2d\u7684\u8d4b\u503c,\u5355\u4e2a\u8bed\u53e5\u7528\u5206\u53f7\u7ed3\u675f<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">var x = 1; var y = 2;\r\ni\u00a0=\u00a00;\u00a0\u00a0\/\/\u8fd9\u4e9b\u521d\u59cb\u5316\u53d8\u91cf\u4f1a\u4f7f\u5f97\u00a0i\u53d8\u91cf\u6210\u4e3a\u5168\u5c40\u53d8\u91cf<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>JS\u4e5f\u53ef\u50cfPython\u4e00\u6837\u7528 \u52a0\u53f7\uff08+\uff09\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6216\u8005\u7528<strong>\u6a21\u677f\u5b57\u7b26\u4e32<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\r\nvar name = '\u5c0f\u660e';\r\nvar age = 20;\r\nvar message = `\u4f60\u597d, ${name}, \u4f60\u4eca\u5e74${age}\u5c81\u4e86!`;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>JS\u4e2d\u4e5f\u542b\u6709\u7c7b\u4f3cPython\u5b57\u5178\u7684<strong>\u5bf9\u8c61<\/strong>\u548c<strong>\u6570\u7ec4<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\u5bf9\u8c61\r\nvar person = {\r\n    name: 'Bob',\r\n    age: 20,\r\n    tags: ['js', 'web', 'mobile'],\r\n    city: 'Beijing',\r\n    hasCar: true,\r\n    zipcode: null\r\n};\r\nperson[name]\r\nperson.name   \/\/\u53e6\u4e00\u79cd\u8bbf\u95ee\u952e\u503c\u5bf9\u7684\u65b9\u6cd5\r\n\/\/\u5b83\u7684\u952e\u53ea\u80fd\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u7528 map , set \u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u952e\r\n\r\n<\/code><\/pre>\n<h2>Map, Set<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);\r\nm.get('Michael'); \/\/ 95(\u83b7\u53d6\u503c)\r\n\/\/map\u4f7f\u7528,\u5b83\u7684\u672c\u8d28\u662f\u4e8c\u4f4d\u6570\u7ec4\r\n\r\nset\u00a0\u7c7b\u4f3c\u00a0\u5217\u8868\r\nvar s2 = new Set([1, 2, 3]); \/\/ \u542b1, 2, 3\r\n<\/code><code class=\"\"><\/code><\/pre>\n<h2>\u6570\u7ec4\u7684\u64cd\u4f5c<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">[1, 2, 3.14, 'Hello', null, true];\r\n\u53e6\u4e00\u79cd\u521b\u5efa\u65b9\u5f0f\r\nnew Array(1, 2, 3); \/\/ \u521b\u5efa\u4e86\u6570\u7ec4[1, 2, 3]\r\n\r\nvar arr = [1, 2, 3.14, 'Hello', null, true];\r\narr.length;  \/\/\u8fd4\u56dearr\u6570\u7ec4\u7684\u957f\u5ea6,\u53ef\u76f4\u63a5\u5bf9\u5176\u8d4b\u503c\u6539\u53d8\u6570\u7ec4\u5927\u5c0f\r\narr.indexOf('Hello')   \/\/\u8fd4\u56de'Hello'\u5728 arr\u4e2d\u7684\u4f4d\u7f6e\uff0c\u82e5\u627e\u4e0d\u5230\u5219\u8fd4\u56de0\r\narr.slice(0 ,3)    \/\/\u6570\u7ec4\u7684\u622a\u53d6\uff0c\u7c7b\u4f3c\u4e8epython\u7684 [0:3]\r\narr.shift()  \/\/\u5220\u9664\u7b2c\u4e00\u4e2a\u5143\u7d20\r\narr.pop()   \/\/\u628a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5220\u9664\r\narr.push('123')  \/\/\u7c7b\u4f3c\u4e8e append('123')\r\narr.unshift('321') \/\/\u6dfb\u52a0\u81f3\u6570\u7ec4\u9996\r\narr.sort()   \/\/\u6392\u5e8f\r\narr.join()  \/\/\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7528\u62ec\u53f7\u4e2d\u7684\u5b57\u7b26\u8fde\u63a5\u8d77\u6765\r\narr.concat([1,2,3])  \/\/\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\r\n<\/code><\/pre>\n<h2>for\u5faa\u73af\u62d3\u5c55<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">var a = ['A', 'B', 'C'];\r\nfor (var x of a)   \/\/ \u904d\u5386 a \u7684\u5143\u7d20\r\n{\r\n    console.log(x);\r\n}\r\n\r\nfor (var x in a)   \/\/ \u904d\u5386 a \u7684\u7d22\u5f15\r\n{\r\n    console.log(x);\r\n}<\/code><\/pre>\n<h2>\u5b9a\u4e49\u51fd\u6570<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">function abs(x) {\r\n    if (x &gt;= 0) {\r\n        return x;\r\n    } else {\r\n        return -x;\r\n    }\r\n}<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709return\u8bed\u53e5\uff0c\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4e5f\u4f1a\u8fd4\u56de\u7ed3\u679c\uff0c\u53ea\u662f\u7ed3\u679c\u4e3aundefined\uff08\u7c7b\u4f3c\u4e8ePython\u7684None\uff09\u3002<\/p>\n<p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6JS\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a <strong>arguments\u00a0<\/strong>\u7684\u6570\u7ec4\uff0c\u5b83\u5305\u542b\u6240\u6709\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">function foo(a, b, c) {\r\n    if (arguments.length === 2) {\r\n        \/\/ \u5b9e\u9645\u62ff\u5230\u7684\u53c2\u6570\u662fa\u548cb\uff0cc\u4e3aundefined\r\n        c = b; \/\/ \u628ab\u8d4b\u7ed9c\r\n        b = null; \/\/ b\u53d8\u4e3a\u9ed8\u8ba4\u503c\r\n    }\r\n}<\/code><\/pre>\n<p>\u4f46\u662f\uff0carguments \u4e0d\u5229\u4e8e\u83b7\u53d6\u591a\u8f93\u5165\u7684\u53c2\u6570\uff0c\u4e8e\u662f\u6709\u4e86 <strong>&#8230;rest\u00a0 \u53c2\u6570<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">function foo(a, b, ...rest) {\r\n    console.log('a = ' + a);\r\n    console.log('b = ' + b);\r\n    console.log(rest);\r\n}\r\n\r\nfoo(1, 2, 3, 4, 5);\r\n\/\/ \u7ed3\u679c:\r\n\/\/ a = 1\r\n\/\/ b = 2\r\n\/\/ Array [ 3, 4, 5 ]\r\n\r\nfoo(1);\r\n\/\/ \u7ed3\u679c:\r\n\/\/ a = 1\r\n\/\/ b = undefined\r\n\/\/ Array []<\/code><\/pre>\n<h2>\u65b9\u6cd5<\/h2>\n<p>\u5728\u5bf9\u8c61\uff08\u5b57\u5178\uff09\u4e2d\u5d4c\u5165\u51fd\u6570\u5728JS\u4e2d\u53eb\u65b9\u6cd5<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">var xiaoming = {\r\n    name: '\u5c0f\u660e',\r\n    birth: 1990,\r\n    age: function () {\r\n        var y = new Date().getFullYear();\r\n        return y - this.birth;\r\n    }\r\n};\/\/\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u52a8\u6001\u5730\u663e\u793a\u5e74\u9f84<\/code><\/pre>\n<p>this \u7c7b\u4f3c\u4e8e self \u5b83\u8868\u793a\u5b83\u7684\u7236\u7ea7\u7c7b\uff0c\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d this\u8868\u793a xiaoming \u7c7b \u5bf9\u8c61<\/p>\n<p>\u53ef\u4ee5\u7528\u00a0 <strong>apply<\/strong> \u6765\u6539\u53d8this\u7684\u6307\u5411<\/p>\n<p>apply\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u9700\u8981\u7ed1\u5b9a\u7684<code>this<\/code>\u53d8\u91cf\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f<code>Array<\/code>\uff0c\u8868\u793a\u51fd\u6570\u672c\u8eab\u7684\u53c2\u6570\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">function getAge() {\r\n    var y = new Date().getFullYear();\r\n    return y - this.birth;\r\n}\r\n\r\nvar xiaoming = {\r\n    name: '\u5c0f\u660e',\r\n    birth: 1990,\r\n    age: getAge\r\n};\r\n\r\nxiaoming.age(); \/\/ 25\r\ngetAge.apply(xiaoming, []); \/\/ 25, this\u6307\u5411xiaoming, \u53c2\u6570\u4e3a\u7a7a<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728HTML&lt;head&gt;\u7684\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u8bed\u53e5\u6765\u5f15\u5165.js \u6587\u4ef6 &lt;script src=&#038;#822 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":6,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}