{"id":2108,"date":"2024-11-10T14:22:06","date_gmt":"2024-11-10T06:22:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2108\/"},"modified":"2024-11-10T14:22:06","modified_gmt":"2024-11-10T06:22:06","slug":"oop-javascript-%e6%8c%91%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2108\/","title":{"rendered":"OOP &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173064132462808.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 hub \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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u662fOOP &#8211; JavaScript \u6311\u6218\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>\u60a8\u53ef\u4ee5\u5728 hub \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; } if (typeof fn !== &#8220;function&#8221;) { 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-2108","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2108","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=2108"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2108\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}