{"id":52027,"date":"2024-12-03T14:43:16","date_gmt":"2024-12-03T06:43:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52027\/"},"modified":"2024-12-03T14:43:16","modified_gmt":"2024-12-03T06:43:16","slug":"oop-javascript-%e6%8c%91%e6%88%98-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52027\/","title":{"rendered":"OOP &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>OOP &#8211; JavaScript \u6311\u6218<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aOOP &#8211; JavaScript \u6311\u6218\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731924206673b10eebbd04.jpg\" class=\"aligncenter\" title=\"OOP &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" alt=\"OOP &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 github \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2> oop \u76f8\u5173\u6311\u6218 <\/h2>\n<hr>\n<h3> \u5b9e\u4f8b\u5316 <\/h3>\n<pre>\/**\n * @param {any} obj\n * @param {target} target\n * @return {boolean}\n *\/\n\n\/\/ one-line solution\nfunction myinstanceof(obj, fn) {\n  return fn.prototype.isprototypeof(obj);\n}\n\nfunction myinstanceof(obj, fn) {\n  if (typeof obj !== \"object\" || obj === null) {\n    return false;\n  }\n\n  if (typeof fn !== \"function\") {\n    return false;\n  }\n\n  let proto = object.getprototypeof(obj);\n\n  while (proto) {\n    if (proto === fn.prototype) {\n      return true;\n    }\n\n    proto = object.getprototypeof(proto);\n  }\n\n  return false;\n}\n\n\/\/ usage example\nclass a {}\nclass b extends a {}\nconst b = new b();\nconsole.log(myinstanceof(b, b)); \/\/ =&gt; true\nconsole.log(myinstanceof(b, a)); \/\/ =&gt; true\nconsole.log(myinstanceof(b, object)); \/\/ =&gt; true\nfunction c() {}\nconsole.log(myinstanceof(b, c)); \/\/ =&gt; false\nc.prototype = b.prototype;\nconsole.log(myinstanceof(b, c)); \/\/ =&gt; true\nc.prototype = {};\nconsole.log(myinstanceof(b, c)); \/\/ =&gt; false\n<\/pre>\n<hr>\n<h3> \u65b0\u7684 <\/h3>\n<pre>\/**\n * @param {Function} constructor\n * @param {any[]} args\n * `myNew(constructor, ...args)` should return the same as `new constructor(...args)`\n *\/\n\nfunction myNew(constructor, ...args) {\n  const obj = {};\n  Object.setPrototypeOf(obj, constructor.prototype);\n\n  const result = constructor.call(obj, ...args);\n\n  if (typeof result !== \"object\" || result == null) {\n    return obj;\n  } else {\n    return result;\n  }\n}\n\n\/\/ Usage example\nfunction Person(name) {\n  this.name = name;\n}\nconst person = myNew(Person, \"Mike\");\nconsole.log(person); \/\/ =&gt; Person { name: 'Mike' }\n<\/pre>\n<hr>\n<h2> \u53c2\u8003 <\/h2>\n<ul>\n<li>60\u3002\u521b\u5efa\u60a8\u81ea\u5df1\u7684\u65b0\u8fd0\u7b97\u7b26 &#8211; bfe.dev<\/li>\n<li>90\u3002\u7f16\u5199\u4f60\u81ea\u5df1\u7684\u5b9e\u4f8b &#8211; bfe.dev<\/li>\n<li>\u5b9e\u4f8b &#8211; mdn<\/li>\n<li>\u65b0 &#8211; mdn<\/li>\n<li>\u65b9\u6cd5\u94fe &#8211; wikipedia.org<\/li>\n<li>2726\u3002\u5e26\u65b9\u6cd5\u94fe\u7684\u8ba1\u7b97\u5668 &#8211; leetcode<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aOOP &#8211; JavaScript \u6311\u6218\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win10\u952e\u76d8\u65e0\u6cd5\u8f93\u5165\u600e\u4e48\u529e Win10\u952e\u76d8\u65e0\u6cd5\u8f93\u5165\u89e3\u51b3\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>OOP &#8211; JavaScript \u6311\u6218 \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\u300aOOP &#8211; JavaScript \u6311\u6218\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 \u60a8\u53ef\u4ee5\u5728 github \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002 oop \u76f8\u5173\u6311\u6218 \u5b9e\u4f8b\u5316 \/** * @param {any} obj * @param {target} target * @return {boolean} *\/ \/\/ one-line solution function myinstanceof(obj, fn) { return fn.prototype.isprototypeof(obj); } function myinstanceof(obj, fn) { if (typeof obj !== &#8220;object&#8221; || obj === null) { return false; } [&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-52027","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52027","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=52027"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52027\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}