{"id":3301,"date":"2024-11-10T11:32:55","date_gmt":"2024-11-10T03:32:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3301\/"},"modified":"2024-11-10T11:32:55","modified_gmt":"2024-11-10T03:32:55","slug":"js%e5%a6%82%e4%bd%95%e8%ae%b0%e4%bd%8f%e8%a1%8c%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3301\/","title":{"rendered":"js\u5982\u4f55\u8bb0\u4f4f\u884c\u6570"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u6709\u56db\u79cd\u8bb0\u4f4f\u884c\u53f7\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u884c\u53f7\u53d8\u91cf\u3001\u4f7f\u7528 error \u5bf9\u8c61\u3001\u4f7f\u7528 debugger \u5173\u952e\u5b57\u3001\u4f7f\u7528\u6e90\u6620\u5c04\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809212296422.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u8bb0\u4f4f\u884c\u6570\u63d2\u56fe\" alt=\"js\u5982\u4f55\u8bb0\u4f4f\u884c\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u8bb0\u4f4f\u884c\u6570<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bb0\u4f4f\u884c\u6570\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u884c\u53f7\u53d8\u91cf<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u884c\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>let lineNumber = 0;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u6bcf\u5f53\u4f60\u60f3\u5f15\u7528\u884c\u53f7\u65f6\uff0c\u53ea\u9700\u4f7f\u7528 lineNumber \u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>console.log(\"\u5f53\u524d\u884c\u53f7\uff1a\" + lineNumber);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Error \u5bf9\u8c61<\/strong><\/p>\n<p>Error \u5bf9\u8c61\u6709\u4e00\u4e2a lineNumber \u5c5e\u6027\uff0c\u5b83\u5305\u542b\u629b\u51fa\u9519\u8bef\u7684\u884c\u53f7\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u76ee\u7684\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>try {\n  \/\/ \u6267\u884c\u4e00\u4e9b\u4ee3\u7801\n} catch (error) {\n  console.log(\"\u9519\u8bef\u884c\u53f7\uff1a\" + error.lineNumber);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 debugger \u5173\u952e\u5b57<\/strong><\/p>\n<p>debugger \u5173\u952e\u5b57\u4f1a\u5728\u4f60\u6307\u5b9a\u7684\u884c\u53f7\u5904\u6682\u505c\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u3001\u8c03\u7528\u5806\u6808\u548c\u5176\u4ed6\u8c03\u8bd5\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>debugger; \/\/ \u5728\u6b64\u884c\u5904\u6682\u505c\u6267\u884c<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u6e90\u6620\u5c04<\/strong><\/p>\n<p>\u6e90\u6620\u5c04\u662f\u4e00\u79cd\u5c06\u7f16\u8bd1\u540e\u7684 JavaScript \u4ee3\u7801\u6620\u5c04\u5230\u539f\u59cb\u6e90\u4ee3\u7801\u7684\u65b9\u6cd5\u3002\u8fd9\u5141\u8bb8\u4f60\u5728\u8c03\u8bd5\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u65f6\u67e5\u770b\u539f\u59cb\u6e90\u4ee3\u7801\u3002\u4e3a\u6b64\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u6e90\u6620\u5c04\u5e93\uff0c\u4f8b\u5982 [source-map](https:\/\/hub.com\/mozilla\/source-map)\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e9b\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e\u5ba2\u6237\u7aef JavaScript \u4ee3\u7801\u3002<\/li>\n<li>\n<p>\u5bf9\u4e8e\u670d\u52a1\u5668\u7aef JavaScript \u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8bed\u8a00\u7279\u5b9a\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>Node.js\uff1a\u4f7f\u7528 Error.stack \u5c5e\u6027<\/li>\n<li>Java\uff1a\u4f7f\u7528 Thread.getStackTrace() \u65b9\u6cd5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u8bb0\u4f4f\u884c\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\uff0c\u6709\u56db\u79cd\u8bb0\u4f4f\u884c\u53f7\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u884c\u53f7\u53d8\u91cf\u3001\u4f7f\u7528 error \u5bf9\u8c61\u3001\u4f7f\u7528 debugger \u5173\u952e\u5b57\u3001\u4f7f\u7528\u6e90\u6620\u5c04\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u8bb0\u4f4f\u884c\u6570 \u5728 JavaScript \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bb0\u4f4f\u884c\u6570\uff1a \u4f7f\u7528\u884c\u53f7\u53d8\u91cf \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u884c\u53f7\u3002\u4f8b\u5982\uff1a let lineNumber = 0; \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u6bcf\u5f53\u4f60\u60f3\u5f15\u7528\u884c\u53f7\u65f6\uff0c\u53ea\u9700\u4f7f\u7528 lineNumber \u53d8\u91cf\u3002\u4f8b\u5982\uff1a console.log(&#8220;\u5f53\u524d\u884c\u53f7\uff1a&#8221; + lineNumber); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Error \u5bf9\u8c61 Error \u5bf9\u8c61\u6709\u4e00\u4e2a lineNumber \u5c5e\u6027\uff0c\u5b83\u5305\u542b\u629b\u51fa\u9519\u8bef\u7684\u884c\u53f7\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u76ee\u7684\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff1a try { \/\/ \u6267\u884c\u4e00\u4e9b\u4ee3\u7801 } catch (error) { console.log(&#8220;\u9519\u8bef\u884c\u53f7\uff1a&#8221; + error.lineNumber); } \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 debugger \u5173\u952e\u5b57 debugger \u5173\u952e\u5b57\u4f1a\u5728\u4f60\u6307\u5b9a\u7684\u884c\u53f7\u5904\u6682\u505c\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u3001\u8c03\u7528\u5806\u6808\u548c\u5176\u4ed6\u8c03\u8bd5\u4fe1\u606f\u3002\u4f8b\u5982\uff1a debugger; \/\/ \u5728\u6b64\u884c\u5904\u6682\u505c\u6267\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3301","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3301"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3301\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}