{"id":53703,"date":"2024-12-03T17:16:12","date_gmt":"2024-12-03T09:16:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53703\/"},"modified":"2024-12-03T17:16:12","modified_gmt":"2024-12-03T09:16:12","slug":"javascript-%e4%b8%ad%e9%93%be%e5%bc%8f%e8%b0%83%e7%94%a8%e6%98%af%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53703\/","title":{"rendered":"JavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aJavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241203\/1733231927674f0537b443a.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d\u94fe\u5f0f\u8c03\u7528\u7684\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u94fe\u5f0f\u8c03\u7528\u662f\u6307\u51fd\u6570\u53ef\u4ee5\u8fde\u7eed\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u8c03\u7528\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u8981\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\u63a5\u53d7\u201cthis\u201d\u5bf9\u8c61<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 this \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<pre>function sint(a, b) {\n  this.val = a + b;\n}\n\nsint.prototype.j = function(e) {\n  return this.val + e;\n}<\/pre>\n<p><strong>\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u94fe\u5f0f\u8c03\u7528<\/strong><\/p>\n<p>\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n<pre>function sint(a, b) {\n  this.val = a + b;\n\n  this.j = function(e) {\n    return this.val + e;\n  }\n}<\/pre>\n<p><strong>\u4f7f\u7528 es6 proxy \u7ed1\u5b9a symbol.toprimitive<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 es6 proxy\uff0c\u53ef\u4ee5\u7ed1\u5b9a symbol.toprimitive \u65b9\u6cd5\uff0c\u4f7f\u5176\u5728\u53c2\u4e0e\u8ba1\u7b97\u65f6\u8fd4\u56de\u6240\u9700\u7684\u503c\u3002<\/p>\n<pre>function sum(...args) {\n  this.value = args.reduce((a, b) =&gt; a + b, 0);\n\n  return new proxy(this, {\n    get: function (target, prop) {\n      if (prop === symbol.toprimitive) {\n        return () =&gt; target.value;\n      }\n\n      return target[prop];\n    }\n  });\n}\n\nsum.prototype.add = function (value) {\n  this.value += value;\n  return this;\n}<\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ Sint \u65b9\u6cd5\nconsole.log(Sint(1, 2).j(10)); \/\/ 13\n\n\/\/ ES6 Proxy \u65b9\u6cd5\nconsole.log(new Sum(1, 2, 3).add(4).add(5)); \/\/ 15\nconsole.log(new Sum(1, 2, 3).add(4).add(5) + 20); \/\/ 35<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u8c03\u6574\u7b14\u8bb0\u672c\u7535\u8111\u5c4f\u5e55\u4eae\u5ea6\uff1a\u5168\u9762\u6307\u5357\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aJavaScript \u4e2d\u94fe\u5f0f\u8c03\u7528\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 javascript \u4e2d\u94fe\u5f0f\u8c03\u7528\u7684\u5b9e\u73b0 \u5728 javascript \u4e2d\uff0c\u94fe\u5f0f\u8c03\u7528\u662f\u6307\u51fd\u6570\u53ef\u4ee5\u8fde\u7eed\u5730\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u8c03\u7528\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u7684\u65b9\u6cd5\u3002 \u8981\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\u63a5\u53d7\u201cthis\u201d\u5bf9\u8c61 \u901a\u8fc7\u4f7f\u7528 this \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u3002 function sint(a, b) { this.val = a + b; } sint.prototype.j = function(e) { return this.val + e; } \u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u94fe\u5f0f\u8c03\u7528 \u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\u3002 function sint(a, b) { this.val = a + b; this.j = function(e) { return this.val + e; } } \u4f7f\u7528 [&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-53703","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53703","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=53703"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53703\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}