-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs__myblog__js__02.md.js
More file actions
1 lines (1 loc) · 8.08 KB
/
docs__myblog__js__02.md.js
File metadata and controls
1 lines (1 loc) · 8.08 KB
1
(window["webpackJsonp"]=window["webpackJsonp"]||[]).push([[72],{mmSw:function(e,n,l){"use strict";l.r(n);var a=l("q1tI"),t=l.n(a),c=l("dEAq"),o=l("H1Ra"),r=t.a.memo((e=>{e.demos;return t.a.createElement(t.a.Fragment,null,t.a.createElement("div",{className:"markdown"},t.a.createElement("h1",{id:"js-\u95ed\u5305"},t.a.createElement(c["AnchorLink"],{to:"#js-\u95ed\u5305","aria-hidden":"true",tabIndex:-1},t.a.createElement("span",{className:"icon icon-link"})),"JS \u95ed\u5305"),t.a.createElement("p",null,"\u95ed\u5305\u7684\u5b9a\u4e49\u5176\u5b9e\u5f88\u7b80\u5355\uff1a\u51fd\u6570 A \u5185\u90e8\u6709\u4e00\u4e2a\u51fd\u6570 B\uff0c\u51fd\u6570 B \u53ef\u4ee5\u8bbf\u95ee\u5230\u51fd\u6570 A \u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u51fd\u6570 B \u5c31\u662f",t.a.createElement("code",null,"\u95ed\u5305"),"\u3002"),t.a.createElement("p",null,"\u5f88\u591a\u4eba\u5bf9\u4e8e\u95ed\u5305\u7684\u89e3\u91ca\u53ef\u80fd\u662f\u51fd\u6570\u5d4c\u5957\u4e86\u51fd\u6570\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002\u5176\u5b9e\u8fd9\u4e2a\u89e3\u91ca\u662f\u4e0d\u5b8c\u6574\u7684\uff0c\u5c31\u6bd4\u5982\u6211\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u53ef\u4ee5\u53cd\u9a73\u8fd9\u4e2a\u89c2\u70b9\u3002"),t.a.createElement("p",null,"\u5728 JS \u4e2d\uff0c\u95ed\u5305\u5b58\u5728\u7684\u610f\u4e49\u5c31\u662f\u8ba9\u6211\u4eec\u53ef\u4ee5\u95f4\u63a5\u8bbf\u95ee\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u3002"),t.a.createElement("p",null,t.a.createElement("strong",null,"\u7ecf\u5178\u9762\u8bd5\u9898\uff0c\u5faa\u73af\u4e2d\u4f7f\u7528\u95ed\u5305\u89e3\u51b3 ",t.a.createElement("code",null,"var")," \u5b9a\u4e49\u51fd\u6570\u7684\u95ee\u9898")),t.a.createElement(o["a"],{code:"for (var i = 1; i <= 5; i++) {\n setTimeout(function timer() {\n console.log(i);\n }, i * 1000);\n}\n//\u6267\u884c\u7ed3\u679c\u662f5\u4e2a6\n\n//\u8ba9\u6267\u884c\u7ed3\u679c\u53d8\u62101 2 3 4 5\u7684\u65b9\u6cd5\u6709\n//\u65b9\u6cd5\u4e00 \u4f7f\u7528let\u5757\u7ea7\u4f5c\u7528\u57df\nfor (let i = 1; i <= 5; i++) {\n setTimeout(function timer() {\n console.log(i);\n }, i * 1000);\n}\n//\u65b9\u6cd5\u4e8c \u7528\u95ed\u5305\nfor (let i = 1; i <= 5; i++) {\n (function(j) {\n setTimeout(function timer() {\n console.log(j);\n }, j * 1000);\n })(i);\n}",lang:"js"}),t.a.createElement("h2",{id:"\u95ed\u5305\u6e90\u5934"},t.a.createElement(c["AnchorLink"],{to:"#\u95ed\u5305\u6e90\u5934","aria-hidden":"true",tabIndex:-1},t.a.createElement("span",{className:"icon icon-link"})),"\u95ed\u5305\u6e90\u5934"),t.a.createElement("p",null,"\u95ed\u5305\u7684\u4ea7\u751f\u7531\u4e8e",t.a.createElement("strong",null,"\u53d8\u91cf\u4f5c\u7528\u57df\u94fe"),"\u5f15\u8d77\u7684(\u7531\u8bcd\u6cd5\u4f5c\u7528\u57df\u5bfc\u81f4)"),t.a.createElement("p",null,t.a.createElement("strong",null,"\u4ece javascript \u4f5c\u7528\u57df\u8bf4\u95ed\u5305")),t.a.createElement("p",null,"\u5728 ES5 \u53ca\u4e4b\u524d\u7684\u8bed\u8a00\u89c4\u8303\u4e2d\u4f5c\u7528\u57df\u5206 3 \u79cd:"),t.a.createElement("ul",null,t.a.createElement("li",null,"\u5168\u5c40\u4f5c\u7528\u57df"),t.a.createElement("li",null,"\u5c40\u90e8(\u51fd\u6570\u4f5c\u7528\u57df)"),t.a.createElement("li",null,"eval \u4f5c\u7528\u57df\u3002 [\u6ce8\u610f:\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df]")),t.a.createElement("p",null,"\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5c31\u5c5e\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\uff0c\u4e14\u53ea\u5bf9\u51fd\u6570\u8303\u56f4\u5185\u5176\u4ed6\u8868\u8fbe\u5f0f\u53ef\u89c1\u3002 \u800c\u51fd\u6570\u5185\u90e8\u53c8\u53ef\u4ee5\u4f7f\u7528\u7236\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u8fd9\u5c31\u662f\u7531\u4e8e\u4f5c\u7528\u57df\u94fe\uff0c\u5f53 JavaScript \u67e5\u627e\u4e0e\u53d8\u91cf\u5173\u8054\u7684\u503c\u65f6\uff0c\u4f1a\u9075\u5faa\u4e00\u4e2a\u67e5\u627e\u94fe\u3002\u8fd9\u4e2a\u94fe\u662f\u57fa\u4e8e\u4f5c\u7528\u57df\u7684\u5c42\u6b21\u7ed3\u6784\u3002 \u5982\u4e0b\u4ee3\u7801\uff1a"),t.a.createElement(o["a"],{code:"var a = 'global variable';\n(function() {\n console.log(a);\n var fn = function() {\n var a = 'local variable';\n console.log(a);\n };\n fn();\n})();\n//\u8f93\u51fa\uff1aglobal variable local variable",lang:"js"}),t.a.createElement("p",null,"\u9996\u5148\u662f\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf",t.a.createElement("code",null,"a"),",\u7136\u540e\u8c03\u7528\u4e86\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a",t.a.createElement("code",null,"console.log(a)"),"\u6839\u636e\u4f5c\u7528\u57df\u67e5\u627e\uff0c\u56e0\u4e3a\u7acb\u5373\u6267\u884c\u51fd\u6570\u91cc\u9762\u6ca1\u6709\u58f0\u660e",t.a.createElement("code",null,"a"),"\u5c31\u5f80\u4e0a\u67e5\u627e\uff0c\u5c31\u67e5\u627e\u5230",t.a.createElement("code",null,"window"),"\u4e0b\u9762\u7684",t.a.createElement("code",null,"a"),"\u6253\u5370\u51fa",t.a.createElement("code",null,"global variable"),"\uff0c\u63a5\u7740\u8fd9\u4e2a\u7acb\u5373\u6267\u884c\u51fd\u6570\u58f0\u660e\u4e86\u4e00\u4e2a\u51fd\u6570",t.a.createElement("code",null,"fn"),"\u5e76\u4e14\u8c03\u7528\u4e86\u5b83\uff0c\u6240\u4ee5\u7b2c\u4e8c\u4e2a",t.a.createElement("code",null,"console.log(a)"),"\u6839\u636e\u51fd\u6570\u5185\u90e8\u4f5c\u7528\u57df\u67e5\u627e\u5c31\u67e5\u627e\u5230\u4e86",t.a.createElement("code",null,"local variable")),t.a.createElement("ul",null,t.a.createElement("li",null,"\u8fd9\u91cc\u5176\u5b9e\u6709 2 \u4e2a\u95ed\u5305\u73af\u5883\u4e00\u4e2a\u662f",t.a.createElement("code",null,"window"),"\u5bf9\u8c61\u548c\u7acb\u5373\u6267\u884c\u51fd\u6570\u6240\u521b\u5efa\u7684\u95ed\u5305;\u53e6\u4e00\u4e2a\u662f\u7acb\u5373\u6267\u884c\u51fd\u6570\u548c\u5176\u5185\u90e8\u58f0\u660e\u7684\u51fd\u6570\u53d8\u91cf",t.a.createElement("code",null,"fn"),"\u521b\u5efa\u7684\u95ed\u5305\u3002"),t.a.createElement("li",null,"\u95ed\u5305\u5176\u5b9e\u5c31\u662f\u4e2a\u79f0\u547c\uff0c\u91cd\u8981\u7684\u662f\u5728\u8fd9\u79cd\u573a\u666f\u4e0b\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u800c\u8fd9\u6b63\u662f\u56e0\u4e3a\u8bcd\u6cd5\u4f5c\u7528\u57df\u94fe\u3002"),t.a.createElement("li",null,"\u5f53\u95ed\u5305\u4ea7\u751f\uff0c\u5728\u5c40\u90e8\u51fd\u6570\u672a\u88ab\u91ca\u653e\u4e4b\u524d\uff0c\u88ab\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u5c31\u65e0\u6cd5\u88ab\u91ca\u653e")),t.a.createElement("p",null,t.a.createElement("strong",null,"\u90a3\u4e48\u6211\u4eec\u521a\u521a\u8bf4\u7684\u201c\u8bcd\u6cd5\u201c\u4f5c\u7528\u57df\u53c8\u662f\u4ec0\u4e48"),"\u3002 \u5176\u5b9e\u8bcd\u6cd5\u5c31\u662f\u6307\u4ee3\u73af\u5883:",t.a.createElement("strong",null,"\u7531\u4e8e\u51fd\u6570\u51b3\u5b9a\u4f5c\u7528\u57df"),"\uff0c\u5e76\u4e14\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\u53ef\u4ee5\u76f4\u63a5\u7528\u6765\u53c2\u6570\u4f20\u9012\u7b49\u3002\u90a3\u4e48\u4f5c\u7528\u57df\u94fe\u662f\u600e\u6837\u6765\u786e\u5b9a\u7684\u5462: \u4e0b\u9762\u7684\u8bdd\u80cc\u719f\u4e86: ",t.a.createElement("strong",null,"\u4f5c\u7528\u57df\u94fe\u662f\u6839\u636e\u51fd\u6570\u5b9a\u4e49\u65f6\u5019\u7684\u4f4d\u7f6e\u786e\u5b9a\u7684\u800c\u4e0d\u662f\u5728\u8c03\u7528\u65f6"),"\u3002--\u8fd9\u5c31\u662f\u201c\u8bcd\u6cd5\u201d\u4f5c\u7528\u57df"),t.a.createElement("p",null,"\u8bcd\u6cd5\u4f5c\u7528\u57df\u7684\u7406\u89e3\u53ef\u4ee5\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a"),t.a.createElement(o["a"],{code:"var count = 1;\nfunction a() {\n console.log(count);\n}\n\nfunction b() {\n var count = 2;\n a();\n}\nb(); //\u6253\u5370\u51fa1\n\n//\u8981\u60f3\u6253\u5370\u51fa2 \u5c31\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\nvar count = 1;\n\nfunction b() {\n var count = 2;\n function a() {\n console.log(count);\n }\n a();\n}\nb();",lang:"js"}),t.a.createElement("p",null,"\u4e3a\u4ec0\u4e48\u4e0d\u662f 2 \u5462\uff1f\u5c31\u662f\u56e0\u4e3a\u51fd\u6570\u5728\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u91cc\u9762\u7684 a \u5df2\u7ecf\u5b58\u5728\u4e86")))}));n["default"]=e=>{var n=t.a.useContext(c["context"]),l=n.demos;return t.a.useEffect((()=>{var n;null!==e&&void 0!==e&&null!==(n=e.location)&&void 0!==n&&n.hash&&c["AnchorLink"].scrollToAnchor(decodeURIComponent(e.location.hash.slice(1)))}),[]),t.a.createElement(r,{demos:l})}}}]);