awesome-interview/assets/js/8584d295.087c1e69.js

1 line
12 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[7259],{3905:function(e,t,n){n.d(t,{Zo:function(){return c},kt:function(){return k}});var r=n(7294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var p=r.createContext({}),u=function(e){var t=r.useContext(p),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},c=function(e){var t=u(e.components);return r.createElement(p.Provider,{value:t},e.children)},s={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,o=e.originalType,p=e.parentName,c=i(e,["components","mdxType","originalType","parentName"]),m=u(n),k=a,f=m["".concat(p,".").concat(k)]||m[k]||s[k]||o;return n?r.createElement(f,l(l({ref:t},c),{},{components:n})):r.createElement(f,l({ref:t},c))}));function k(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=n.length,l=new Array(o);l[0]=m;var i={};for(var p in t)hasOwnProperty.call(t,p)&&(i[p]=t[p]);i.originalType=e,i.mdxType="string"==typeof e?e:a,l[1]=i;for(var u=2;u<o;u++)l[u]=n[u];return r.createElement.apply(null,l)}return r.createElement.apply(null,n)}m.displayName="MDXCreateElement"},1251:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return i},contentTitle:function(){return p},metadata:function(){return u},toc:function(){return c},default:function(){return m}});var r=n(7462),a=n(3366),o=(n(7294),n(3905)),l=["components"],i={sidebar_label:"\u57fa\u7840\uff1a\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406",sidebar_position:6},p="\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406",u={unversionedId:"book1/js-closures",id:"book1/js-closures",isDocsHomePage:!1,title:"\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406",description:"\u76f8\u5173\u95ee\u9898",source:"@site/docs/book1/js-closures.md",sourceDirName:"book1",slug:"/book1/js-closures",permalink:"/awesome-interview/book1/js-closures",tags:[],version:"current",sidebarPosition:6,frontMatter:{sidebar_label:"\u57fa\u7840\uff1a\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406",sidebar_position:6},sidebar:"tutorialSidebar",previous:{title:"\u6846\u67b6\uff1aVue \u7684 computed \u548c watch \u7684\u533a\u522b",permalink:"/awesome-interview/book1/frame-vue-computed-watch"},next:{title:"\u57fa\u7840\uff1a\u524d\u7aef\u6a21\u5757\u5316\u89c4\u8303",permalink:"/awesome-interview/book1/js-module-specs"}},c=[{value:"\u76f8\u5173\u95ee\u9898",id:"\u76f8\u5173\u95ee\u9898",children:[]},{value:"\u56de\u7b54\u5173\u952e\u70b9",id:"\u56de\u7b54\u5173\u952e\u70b9",children:[]},{value:"\u77e5\u8bc6\u70b9\u6df1\u5165",id:"\u77e5\u8bc6\u70b9\u6df1\u5165",children:[{value:"1. \u4f5c\u7528\u57df",id:"1-\u4f5c\u7528\u57df",children:[]},{value:"2. \u4ec0\u4e48\u662f\u95ed\u5305",id:"2-\u4ec0\u4e48\u662f\u95ed\u5305",children:[]},{value:"3. \u95ed\u5305\u7684\u5e94\u7528",id:"3-\u95ed\u5305\u7684\u5e94\u7528",children:[]}]},{value:"\u53c2\u8003\u8d44\u6599",id:"\u53c2\u8003\u8d44\u6599",children:[]}],s={toc:c};function m(e){var t=e.components,n=(0,a.Z)(e,l);return(0,o.kt)("wrapper",(0,r.Z)({},s,n,{components:t,mdxType:"MDXLayout"}),(0,o.kt)("h1",{id:"\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406"},"\u95ed\u5305\u7684\u4f5c\u7528\u548c\u539f\u7406"),(0,o.kt)("h2",{id:"\u76f8\u5173\u95ee\u9898"},"\u76f8\u5173\u95ee\u9898"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"\u4ec0\u4e48\u662f\u95ed\u5305"),(0,o.kt)("li",{parentName:"ul"},"\u95ed\u5305\u7684\u5e94\u7528")),(0,o.kt)("h2",{id:"\u56de\u7b54\u5173\u952e\u70b9"},"\u56de\u7b54\u5173\u952e\u70b9"),(0,o.kt)("p",null,(0,o.kt)("inlineCode",{parentName:"p"},"\u4f5c\u7528\u57df")," ",(0,o.kt)("inlineCode",{parentName:"p"},"\u5f15\u7528")," ",(0,o.kt)("inlineCode",{parentName:"p"},"\u51fd\u6570")),(0,o.kt)("p",null,"\u4f5c\u7528\uff1a\u80fd\u591f\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u4f5c\u7528\u57df\u5916\uff0c\u4f7f\u7528\u51fd\u6570\u5b9a\u4e49\u4f5c\u7528\u57df\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u3002"),(0,o.kt)("p",null,"\u539f\u7406\uff1a\u57fa\u4e8e\u8bcd\u6cd5\u4f5c\u7528\u57df\u94fe\u548c\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u901a\u8fc7\u7ef4\u6301\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\uff0c\u8ba9\u51fd\u6570\u4f5c\u7528\u57df\u53ef\u4ee5\u5728\u5f53\u524d\u4f5c\u7528\u57df\u5916\u88ab\u8bbf\u95ee\u5230\u3002"),(0,o.kt)("h2",{id:"\u77e5\u8bc6\u70b9\u6df1\u5165"},"\u77e5\u8bc6\u70b9\u6df1\u5165"),(0,o.kt)("h3",{id:"1-\u4f5c\u7528\u57df"},"1. \u4f5c\u7528\u57df"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("strong",{parentName:"li"},"\u4f5c\u7528\u57df"),"\uff1a\u7528\u4e8e\u786e\u5b9a\u5728\u4f55\u5904\u4ee5\u53ca\u5982\u4f55\u67e5\u627e\u53d8\u91cf\uff08\u6807\u8bc6\u7b26\uff09\u7684\u4e00\u5957\u89c4\u5219\u3002"),(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("strong",{parentName:"li"},"\u8bcd\u6cd5\u4f5c\u7528\u57df"),"\uff1a\u8bcd\u6cd5\u4f5c\u7528\u57df\u662f",(0,o.kt)("strong",{parentName:"li"},"\u5b9a\u4e49\u5728\u8bcd\u6cd5\u9636\u6bb5\u7684\u4f5c\u7528\u57df"),"\u3002\u8bcd\u6cd5\u4f5c\u7528\u57df\u662f\u7531\u5199\u4ee3\u7801\u65f6\u5c06\u4ee3\u7801\u548c\u5757\u4f5c\u7528\u57df\u5199\u5728\u54ea\u91cc\u6765\u51b3\u5b9a\u7684\uff0c\u56e0\u6b64\u5f53\u8bcd\u6cd5\u4f5c\u7528\u57df\u5904\u7406\u4ee3\u7801\u662f\u4f1a\u4fdd\u6301\u4f5c\u7528\u57df\u4e0d\u53d8(\u5927\u90e8\u5206\u60c5\u51b5)\u3002"),(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("strong",{parentName:"li"},"\u5757\u4f5c\u7528\u57df"),"\uff1a\u6307\u7684\u662f\u53d8\u91cf\u548c\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5c5e\u4e8e\u6240\u5904\u7684\u4f5c\u7528\u57df\uff0c\u4e5f\u53ef\u4ee5\u5c5e\u4e8e\u67d0\u4e2a\u4ee3\u7801\u5757\uff08\u901a\u5e38\u7528{}\u5305\u88f9\uff09\u3002\u5e38\u89c1\u7684\u5757\u7ea7\u4f5c\u7528\u57df\u6709 with\uff0ctry/catch\uff0clet\uff0cconst \u7b49\u3002"),(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("strong",{parentName:"li"},"\u51fd\u6570\u4f5c\u7528\u57df"),"\uff1a\u5c5e\u4e8e\u8fd9\u4e2a\u51fd\u6570\u7684\u5168\u90e8\u53d8\u91cf\u90fd\u53ef\u4ee5\u5728\u6574\u4e2a\u51fd\u6570\u8303\u56f4\u5185\u4f7f\u7528\u53ca\u590d\u7528\uff08\u5305\u62ec\u5d4c\u5957\u4f5c\u7528\u57df\uff09\u3002"),(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("strong",{parentName:"li"},"\u4f5c\u7528\u57df\u94fe"),"\uff1a\u67e5\u627e\u53d8\u91cf\u65f6\uff0c\u5148\u4ece\u5f53\u524d\u4f5c\u7528\u57df\u5f00\u59cb\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u5230\u7236\u7ea7(\u8bcd\u6cd5\u5c42\u9762\u4e0a\u7684\u7236\u7ea7)\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\uff0c\u4e00\u76f4\u627e\u5230\u5168\u5c40\u4f5c\u7528\u57df\u3002\u4f5c\u7528\u57df\u94fe\u6b63\u662f\u5305\u542b\u8fd9\u4e9b\u4f5c\u7528\u57df\u7684\u5217\u8868\u3002")),(0,o.kt)("h3",{id:"2-\u4ec0\u4e48\u662f\u95ed\u5305"},"2. \u4ec0\u4e48\u662f\u95ed\u5305"),(0,o.kt)("blockquote",null,(0,o.kt)("p",{parentName:"blockquote"},"\u5f53\u51fd\u6570\u53ef\u4ee5\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u6240\u5728\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u65f6\uff0c\u5c31\u4ea7\u751f\u4e86\u95ed\u5305\uff0c\u5373\u4f7f\u662f\u51fd\u6570\u5728\u5f53\u524d\u8bcd\u6cd5\u4f5c\u7528\u57df\u5916\u6267\u884c\u3002 \u2014\u2014\u300a\u4f60\u4e0d\u77e5\u9053\u7684 JavaScript\u300b")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-javascript"},'function foo() {\n var a = "hzfe";\n function bar() {\n console.log(a);\n }\n return bar;\n}\n\nvar baz = foo();\nbaz(); // hzfe\n')),(0,o.kt)("p",null,"\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 bar \u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u540e\uff0c\u5728\u81ea\u5df1\u5b9a\u4e49\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u4ee5\u5916\u7684\u5730\u65b9\u6267\u884c\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5728\u51fd\u6570 foo \u6267\u884c\u540e\uff0c\u901a\u5e38\u4f1a\u671f\u5f85\u51fd\u6570 foo \u7684\u6574\u4e2a\u5185\u90e8\u4f5c\u7528\u57df\u88ab\u5f15\u64ce\u56de\u6536\u673a\u5236\u9500\u6bc1\u3002\u800c\u95ed\u5305\u53ef\u4ee5\u963b\u6b62\u8fd9\u4ef6\u4e8b\u60c5\u7684\u53d1\u751f\u3002\u4e8b\u5b9e\u4e0a\u5185\u90e8\u4f5c\u7528\u57df\u4f9d\u7136\u5b58\u5728\uff0c\u56e0\u4e3a\u51fd\u6570 bar \u672c\u8eab\u5728\u4f7f\u7528\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u88ab\u56de\u6536\u3002"),(0,o.kt)("p",null,"\u5728 JavaScript \u4e2d\uff0c",(0,o.kt)("strong",{parentName:"p"},"\u6bcf\u5f53\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u95ed\u5305\u5c31\u4f1a\u5728\u51fd\u6570\u521b\u5efa\u7684\u540c\u65f6\u88ab\u521b\u5efa\u51fa\u6765"),"\u3002"),(0,o.kt)("h3",{id:"3-\u95ed\u5305\u7684\u5e94\u7528"},"3. \u95ed\u5305\u7684\u5e94\u7528"),(0,o.kt)("p",null,"\u65e0\u8bba\u4f55\u65f6\u4f55\u5730\uff0c\u5982\u679c",(0,o.kt)("strong",{parentName:"p"},"\u5c06\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u5c31\u4f1a\u770b\u5230\u95ed\u5305\u5728\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u5e94\u7528"),"\u3002\u5728\u5b9a\u65f6\u5668\uff0c\u4e8b\u4ef6\u76d1\u542c\u5668\uff0cajax \u8bf7\u6c42\uff0c\u8de8\u7a97\u53e3\u901a\u4fe1\uff0cweb workers \u6216\u8005\u4efb\u4f55\u5176\u4ed6\u7684\u5f02\u6b65/\u540c\u6b65\u4efb\u52a1\u4e2d\uff0c\u53ea\u8981",(0,o.kt)("strong",{parentName:"p"},"\u4f7f\u7528\u4e86\u56de\u8c03\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u4f7f\u7528\u95ed\u5305"),"\u3002\u4f7f\u7528\u95ed\u5305\u7684\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003",(0,o.kt)("a",{parentName:"p",href:"/awesome-interview/book2/coding-throttle-debounce"},"\u5b9e\u73b0\u8282\u6d41\u9632\u6296\u51fd\u6570"),"\u3002"),(0,o.kt)("p",null,(0,o.kt)("strong",{parentName:"p"},"TIPS: \u95ed\u5305\u4e0e\u6267\u884c\u51fd\u6570\u5173\u7cfb")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-javascript"},'var a = "hzfe";\n(function IIFE() {\n console.log(a);\n})();\n')),(0,o.kt)("p",null,"\u901a\u5e38\u8ba4\u4e3a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff08IIFE\uff09\u662f\u5178\u578b\u7684\u89c2\u5bdf\u95ed\u5305\u7684\u5178\u578b\u4f8b\u5b50\uff0c\u4f46\u4e25\u683c\u6765\u8bf4\u5e76\u4e0d\u662f\u3002",(0,o.kt)("strong",{parentName:"p"},"\u867d\u7136\u521b\u5efa\u4e86\u95ed\u5305\uff0c\u4f46\u6ca1\u6709\u4f53\u73b0\u51fa\u95ed\u5305\u7684\u4f5c\u7528"),"\u3002\u56e0\u4e3a\u51fd\u6570\u5e76\u4e0d\u662f\u5728\u5b83\u672c\u8eab\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u4ee5\u5916\u6267\u884c\u7684\u3002\n\u5b83\u5728\u5b9a\u4e49\u65f6\u6240\u5728\u7684\u4f5c\u7528\u57df\u4e2d\u6267\u884c\uff0c\u800c\u975e\u5916\u90e8\u4f5c\u7528\u57df\u3002"),(0,o.kt)("h2",{id:"\u53c2\u8003\u8d44\u6599"},"\u53c2\u8003\u8d44\u6599"),(0,o.kt)("ol",null,(0,o.kt)("li",{parentName:"ol"},(0,o.kt)("a",{parentName:"li",href:"https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures"},"\u95ed\u5305 MDN")),(0,o.kt)("li",{parentName:"ol"},(0,o.kt)("a",{parentName:"li",href:"/awesome-interview/book2/browser-garbage"},"\u5783\u573e\u56de\u6536\u673a\u5236")),(0,o.kt)("li",{parentName:"ol"},(0,o.kt)("a",{parentName:"li",href:"https://github.com/getify/You-Dont-Know-JS"},"\u4f60\u4e0d\u77e5\u9053\u7684 JavaScript\uff08\u4e0a\u5377\uff09"))))}m.isMDXComponent=!0}}]);