{"id":922,"date":"2022-03-10T21:32:30","date_gmt":"2022-03-10T13:32:30","guid":{"rendered":"http:\/\/www.henrycome.com\/wordpress\/?p=922"},"modified":"2022-03-10T21:32:30","modified_gmt":"2022-03-10T13:32:30","slug":"20220310213213","status":"publish","type":"post","link":"http:\/\/www.henrycome.com\/wordpress\/202203102132\/922","title":{"rendered":"P101-105-\u5f02\u5e38.js"},"content":{"rendered":"<pre><code class=\"language-javascript\">\/*P101-\u5f02\u5e38*\n\/\/\u7a0b\u5e8f\u4e2d\u5404\u79cd\u539f\u56e0\u9020\u6210\u9519\u8bef\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u7684\u60c5\u51b5\n\/\/\u51fa\u73b0\u5f02\u5e38\uff0c\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\n\n\/\/\u8bbf\u95ee\u672a\u5b9a\u4e49\u53d8\u91cf\uff0c\u5185\u7f6e\u5f02\u5e38\n\/\/ console.log(a);\/\/Uncaught ReferenceError: a is not defined\n\/\/ console.log(&#039;\u4e0a\u9762\u4e00\u884c\u4e0d\u6ce8\u91ca\u6389\uff0c\u8fd9\u884c\u4e0d\u4f1a\u6267\u884c&#039;);\/\/\u51fa\u73b0\u5f02\u5e38\u505c\u6b62\u7ee7\u7eed\u8fd0\u884c\n\n\/\/\u8bbf\u95ee\u672a\u5b9a\u4e49\u5bf9\u8c61\u7684\u5c5e\u6027\nvar emp = undefined;\nconsole.log(emp.name);\/\/Uncaught TypeError: Cannot read property &#039;name&#039; of undefined\n\/** *\/\n\n\/*P102-\u6355\u83b7\u5f02\u5e38\n\/\/\u628a\u6355\u83b7\u4e4b\u540e\uff0c\u7a0b\u5e8f\u5c31\u80fd\u7ee7\u7eed\u6267\u884c\n\/\/try\u4e2d\u653e\u8981\u76d1\u63a7\u7684\u4ee3\u7801\uff0ccatch\u7528\u6765\u63a5\u6536\u629b\u51fa\u6765\u7684\u5f02\u5e38\u518d\u5904\u7406\u5b83\n\n\/\/\u8bed\u6cd5\n\/\/3\u4e2a\u5173\u952e\u5b57{}\u8fd9\u6837\u7684\u6837\u5f0f\uff0ctry,catch(),finally\n\/\/catch()\u5e26\u53c2\u6570\uff0c\u53c2\u6570\u5c31\u662f\u63a5\u6536\u5230\u7684\u9519\u8bef\u6216\u5f02\u5e38\n\/\/finally\u4e2d\u7684\u8bed\u53e5\uff0c\u65e0\u8bba\u662f\u5426\u6709\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\n\/\/console.error\u7684\u6253\u5370\u6548\u679c\u662f\u7ea2\u5e95\u7ea2\u5b57\n\ntry{\n    var emp = undefined;\n    console.log(emp.name);\n} catch(err){\n    console.log(err);\n    console.error(err);\/\/TypeError: Cannot read property &#039;name&#039; of undefined\/\/\u6253\u5370\u7ed3\u679c\u4f1a\u663e\u793a\u6210\u7ea2\u5e95\u7ea2\u5b57\n    console.error(&#039;\u8fd9\u884c\u4f1a\u5904\u7406&#039;);\/\/\u8fd9\u884c\u4f1a\u5904\u7406\/\/\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7ea2\u5e95\u7ea2\u5b57\n} finally{\n    console.log(&#039;\u603b\u4f1a\u6267\u884c&#039;);\/\/\u603b\u4f1a\u6267\u884c\/\/\u666e\u901a\u989c\u8272\u3002finally\u4e2d\u7684\u8bed\u53e5\uff0c\u65e0\u8bba\u662f\u5426\u6709\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\n}\n\nconsole.log(&#039;\u4e0a\u9762\u6355\u83b7\u5f02\u5e38\uff0c\u8fd9\u884c\u4f1a\u6267\u884c&#039;);\/\/\u4e0a\u9762\u6355\u83b7\u5f02\u5e38\uff0c\u8fd9\u884c\u4f1a\u6267\u884c\n\/** *\/\n\n\/*P103-\u5f02\u5e38-throw\u629b\u51fa\u5f02\u5e38\n\/\/\u4e0a\u8282\u662f\u7cfb\u7edf\u629b\u51fa\u5f02\u5e38\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e3b\u52a8\u629b\u51fa\u5f02\u5e38\n\/\/\u6bd4\u5982\u8bf7\u6c42\u6570\u636e\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u51fa\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\uff0c\u8ba9\u8d1f\u8d23\u5c55\u793aHTML\u9875\u9762\u7684\u4ee3\u7801\u5904\u7406\u5f02\u5e38\n\n\/\/throw\u53ef\u4ee5\u629b\u51fa\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5efa\u8bae\u4f7f\u7528error\u5bf9\u8c61\uff0c\u6216\u8005\u57fa\u4e8eerror\u5bf9\u8c61\u5c01\u88c5\u81ea\u5df1\u7684\u5f02\u5e38\n\nfunction fetchData(){\n    console.log(&#039;\u83b7\u53d6\u6570\u636e...&#039;);\/\/\u6807\u8bb045\n    throw &#039;404&#039;;\/\/\u4ee5\u5b57\u7b26\u4e32\u7c7b\u578b\n}\n\n\/\/\/\/\u76f4\u63a5\u8c03\u7528\uff0c\u663e\u793a\u672a\u6355\u83b7 404\uff0c\u800c\u4e14\u4e0b\u9762\u4ee3\u7801\u4e0d\u4f1a\u518d\u6267\u884c\n\/\/fetchData();\/\/Uncaught 404\/\/\u9700\u8981\u6ce8\u91ca\u6389\n\ntry{\n    fetchData();\n} catch(e){\n    console.log(e);\/\/\u6807\u8bb055\n    if(e===&#039;404&#039;){\n        console.error(&#039;\u672a\u67e5\u8be2\u5230\u6570\u636e&#039;);\/\/\u6807\u8bb057\n    }\n}\n\/*\n\u6807\u8bb045\uff1a\u83b7\u53d6\u6570\u636e...\n\u6807\u8bb055\uff1a404\/\/\u666e\u901a\u663e\u793a404\uff0c\u5b57\u7b26\u4e32\u6837\u5f0f\uff0c\u4e0d\u662f\u4e0a\u8282\u90a3\u6837\u7684\u7cfb\u7edf\u63d0\u793a\n\u6807\u8bb057\uff1a\u672a\u67e5\u8be2\u5230\u6570\u636e\/\/\u7ea2\u5e95\u7ea2\u5b57\n*\/\n\/** *\/\n\n\/*P104-\u5f02\u5e38-error\u5bf9\u8c61*\/\n\/\/throw \u629b\u51faerror\u5bf9\u8c61\uff0cnew Error(&#039;404&#039;)\u5bf9\u8c61\u63a5\u6536\u6d88\u606f\u5c5e\u6027\uff0c\n\/\/\u6839\u636e\u5bf9\u8c61\u7684\u540d\u5b57\u548c\u6d88\u606f\u53ef\u4ee5\u505a\u4e00\u4e9b\u5f02\u5e38\u5904\u7406\u7684\u64cd\u4f5c\n\n\/*\u4e00\u822c\u7528\u6cd5\nfunction fetchData(){\n    console.log(&#039;\u83b7\u53d6\u6570\u636e...&#039;);\/\/\u6807\u8bb071\n    throw new Error(&#039;404&#039;);\/\/\u6807\u8bb072\n}\n\ntry{\n    fetchData();\/\/\u6807\u8bb076\n} catch(e){\n    console.log(e);\/\/\u6807\u8bb078\n    console.log(typeof e);\/\/\u6807\u8bb079 object\n    console.log(e.name);\/\/\u6807\u8bb080\n    console.log(e.message);\/\/\u6807\u8bb081\n}\n\/*\nindex.js:78 Error: 404\n    at fetchData (index.js:72)\n    at index.js:76\nindex.js:79 object\nindex.js:80 Error\nindex.js:81 404\n*\/\n\/** *\/\n\n\/*\u81ea\u5b9a\u4e49\u9519\u8bef\u5bf9\u8c61\n\/\/\u6839\u636e\u5bf9\u8c61Error\u5bf9\u8c61\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u6210\u4e3a\u7a0b\u5e8f\u4e2d\u7279\u6709\u7684\u5f02\u5e38\n\/\/\u6bd4\u5982\u4f8b\u5b50\u4e2d\uff0cfetchData()\u4f1a\u629b\u51fa\u4e00\u4e2aApiError\uff0c\u4e13\u95e8\u6807\u5fd7\u4ece\u540e\u53f0\u53d6\u6570\u636e\u51fa\u9519\u8bef\u7684\u60c5\u51b5\n\/\/\u6dfb\u52a0\u989d\u5916\u5c5e\u6027\uff0c\u6bd4\u5982url\u5c5e\u6027\uff0c\u4ee3\u8868\u54ea\u4e2a\u8def\u5f84\u51fa\u95ee\u9898\n\n\/\/\u5b9a\u4e49ApiError\uff1a\n\/\/\u5b9a\u4e49class\u5fc5\u987b\u5c0f\u5199\n\/\/\u7ee7\u627f\u81eaError\n\nclass ApiError extends Error{\n    constructor(url, ...params){\n        super(...params);\n        this.url = url;\n        this.name=&#039;ApiError&#039;;\n    }\n}\n\nfunction fetchData(){\n    console.log(&#039;\u83b7\u53d6\u6570\u636e...&#039;);\/\/\u6807\u8bb0114\n    throw new ApiError(&#039;\/post&#039;,&#039;404&#039;);\/\/\u6807\u8bb0115\n}\n\ntry{\n    fetchData();\/\/\u6807\u8bb0119\n} catch(e){\n    console.log(e);\/\/\u6807\u8bb0121\n    console.log(typeof e);\/\/\u6807\u8bb0122\n    console.log(e.name);\/\/\u6807\u8bb0123\n    console.log(e.message);\/\/\u6807\u8bb0124\n    console.log(e.url);\/\/\u6807\u8bb0125\n}\n\/*\nindex.js:121 ApiError: 404\n    at fetchData (http:\/\/127.0.0.1:5500\/index.js:115:11)\n    at http:\/\/127.0.0.1:5500\/index.js:119:5\nindex.js:122 object\nindex.js:123 ApiError\nindex.js:124 404\nindex.js:125 \/post\n*\/\n\/\/\u8fd9\u8282\u7684class\u90e8\u5206\uff0c\u56e0\u4e3a\u4e4b\u524d\u7684\u5bf9\u8c61\u90e8\u5206\u4ece\u539f\u578b\u540e\u6ca1\u6709\u542c\u8bfe\uff0c\u6240\u4ee5\uff0c\u56de\u5934\u8865\u5b8c\u8bfe\uff0c\u8981\u518d\u6765\u542c\u672c\u8282\u3002\n\/** *\/\n\n\/*P105-\u5f02\u5e38-\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\n\/\/\u4e00\u4e2a\u4ee3\u7801\u53ef\u80fd\u629b\u51fa\u591a\u4e2a\u5f02\u5e38\n\/\/\u5229\u7528\u8fd9\u4e9bError\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u5f02\u5e38\u5206\u522b\u8fdb\u884c\u5904\u7406\n\n\/\/\u5148\u51fa\u6765\u7684\u95ee\u9898\uff1a\u4f3c\u4e4e\u4e00\u6b21try\u53ea\u80fd1\u4e2a\u9519\u8bef\u88abcatch\u6293\u4f4f\uff1f\u4e0b\u9762\u4f8b\u5b50\u4e2d\n\/\/\u5224\u65ad\u4e0d\u540c\u7c7b\u578b\u7684\u95ee\u9898\uff0c\u5229\u7528instanceof\uff0c\u5224\u65ad\u662f\u5426\u662f\u67d0\u4e2a\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\nclass ApiError extends Error{\n    constructor(url, ...params){\n        super(...params);\n        this.url = url;\n        this.name=&#039;ApiError&#039;;\n    }\n}\n\nfunction fetchData(){\n    console.log(&#039;\u83b7\u53d6\u6570\u636e...&#039;);\/\/\u6807\u8bb0154\n    console.log(a);\/\/\u6807\u8bb0155 \u4ea7\u751f\u9519\u8bef\uff1a\u8bbf\u95ee\u672a\u5b9a\u4e49\u53d8\u91cf\u9020\u6210\u7684\u9519\u8bef\uff1a\u6ce8\u610f\u4e2d\u6587\u8bed\u6cd5\u8bbf\u95ee\u672a\u5b9a\u4e49\u53d8\u91cf\u9020\u6210\u7684\u9519\u8bef\uff0c\u7684\u7684\u7684\u4ec0\u4e48\u7684\uff0c\u8fd9\u6837\u5c31\u662f\u6240\u5c5e\u5173\u7cfb\u3002\u52a0\u4e0a\u9020\u6210\u7684\u5c31\u597d\u4e86\u3002\n    throw new ApiError(&#039;\/post&#039;,&#039;404&#039;);\/\/\u6807\u8bb0156\n}\n\ntry{\n    fetchData();\/\/\u6807\u8bb0160\n} catch(e){\n    console.error(e);\n    console.log(e.name);\/\/\u6807\u8bb0163\n    console.log(e.message);\/\/\u6807\u8bb0164\n\n   if(e instanceof ReferenceError){\/\/ReferenceError\u6307\u7684\u662f\u5185\u7f6e\u7684\u9519\u8bef\u5bf9\u8c61\n       console.log(&#039;\u7a0b\u5e8f\u5f02\u5e38...&#039;);\n   } else if(e instanceof ApiError){\n       console.log(&#039;API \u5f02\u5e38&#039;);\n   }\n}\n\/*\nindex.js:162 ReferenceError: a is not defined\n    at fetchData (index.js:155)\n    at index.js:160\n(\u533f\u540d) @ index.js:162\nindex.js:163 ReferenceError\nindex.js:164 a is not defined\nindex.js:167 \u7a0b\u5e8f\u5f02\u5e38...\n*\/\n\n\/\/\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u6807\u8bb0155\u4e0d\u6ce8\u91ca\u6389\uff0c\u4e0b\u9762\u5c31\u6355\u83b7\u4e0d\u5230\u6807\u8bb0156\u629b\u51fa\u7684\u5f02\u5e38\u3002\n\/\/\u672c\u8282\u4e0d\u662f\u8bf4\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u4e48\uff1f\u8fd9\u4e0d\u5e94\u8be5\u662f\u4e00\u4e2a\u4e00\u4e2a\u5f02\u5e38\u90fd\u88ab\u6355\u83b7\u7136\u540e\u5b89\u6392\u5f97\u660e\u660e\u767d\u767d\u7684\u4e48\uff1f\n\/\/\u5982\u4f55\u5b9e\u73b0\uff0c\u96be\u9053\u7528\u591a\u4e2atrycatch\u9010\u4e2a\u5bf9\u5e94\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u90fd\u8bed\u53e5\u4e48\uff1f\n\/\/\u4f8b\u5b50\u4e2d\u662f\u8981\u6c42\u53ea\u80fd\u51fa\u4e00\u4e2a\u9519\u5427\u3002\u53ef\u80fd\u9002\u5408\u90a3\u79cd\u4e5f\u662fifelse\u6765\u629b\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\u3002\n\/** *\/\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/*P101-\u5f02\u5e38* \/\/\u7a0b\u5e8f\u4e2d\u5404\u79cd\u539f\u56e0\u9020\u6210\u9519\u8bef\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u7684\u60c5\u51b5 \/\/\u51fa\u73b0\u5f02\u5e38\uff0c\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/922"}],"collection":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/comments?post=922"}],"version-history":[{"count":1,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/922\/revisions"}],"predecessor-version":[{"id":923,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/922\/revisions\/923"}],"wp:attachment":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/media?parent=922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/categories?post=922"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/tags?post=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}