{"id":2334,"date":"2019-01-03T16:28:05","date_gmt":"2019-01-03T08:28:05","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2334"},"modified":"2020-04-16T16:02:57","modified_gmt":"2020-04-16T08:02:57","slug":"javascript-prototype%e5%8e%9f%e5%9e%8b-%e6%96%b0%e6%89%8b%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2334","title":{"rendered":"JavaScript Prototype(\u539f\u578b) \u65b0\u624b\u6307\u5357"},"content":{"rendered":"<p>\u82f1\u6587\u539f\u6587 | <a href='https:\/\/tylermcginnis.com\/beginners-guide-to-javascript-prototype\/' target='_blank' class='url'>https:\/\/tylermcginnis.com\/beginners-guide-to-javascript-prototype\/<\/a><\/p>\n<p>\u5f15\u8a00\uff1a\u5982\u679c\u4e0d\u5904\u7406\u5bf9\u8c61\uff0c\u60a8\u5c31\u65e0\u6cd5\u5728 JavaScript \u65b9\u9762\u53d6\u5f97\u5f88\u5927\u8fdb\u5c55\u3002\u5b83\u4eec\u51e0\u4e4e\u662f JavaScript \u7f16\u7a0b\u8bed\u8a00\u7684\u6240\u6709\u65b9\u9762\u7684\u57fa\u7840\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u60a8\u5c06\u4e86\u89e3\u7528\u4e8e\u5b9e\u4f8b\u5316\u65b0\u5bf9\u8c61\u7684\u5404\u79cd\u6a21\u5f0f\uff0c\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u60a8\u5c06\u9010\u6b65\u6df1\u5165\u4e86\u89e3 JavaScript \u7684\u539f\u578b\u3002<\/p>\n<h2><strong>\u57fa\u7840\u5165\u95e8<\/strong><\/h2>\n<p>\u5982\u679c\u4e0d\u5904\u7406\u5bf9\u8c61\uff0c\u60a8\u5c31\u65e0\u6cd5\u5728 JavaScript \u65b9\u9762\u53d6\u5f97\u5f88\u5927\u8fdb\u5c55\u3002\u5b83\u4eec\u51e0\u4e4e\u662f JavaScript \u7f16\u7a0b\u8bed\u8a00\u7684\u6240\u6709\u65b9\u9762\u7684\u57fa\u7840\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u5bf9\u8c61\u53ef\u80fd\u662f\u4f60\u521a\u5f00\u59cb\u5b66\u4e60\u7684\u7b2c\u4e00\u4ef6\u4e8b\u3002\u8bdd\u867d\u5982\u6b64\uff0c\u4e3a\u4e86\u6700\u6709\u6548\u5730\u5b66\u4e60 JavaScript \u4e2d\u7684\u539f\u578b\uff0c\u6211\u4eec\u5c06\u4ece\u57fa\u7840\u5f00\u59cb\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5bf9\u8c61\u662f\u952e\/\u503c\u5bf9\u3002\u521b\u5efa\u5bf9\u8c61\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528\u82b1\u62ec\u53f7{}\uff0c\u5e76\u4f7f\u7528\u70b9\u8868\u793a\u6cd5\u5411\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><strong>JavaScript \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class='language-javascript' lang='javascript'>let animal = {}\r\nanimal.name = &#39;Leo&#39;\r\nanimal.energy = 10\r\n \r\nanimal.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nanimal.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nanimal.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u5f88\u7b80\u5355\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6211\u4eec\u9700\u8981\u521b\u5efa\u591a\u4e2a\u52a8\u7269\u3002\u5f88\u81ea\u7136\u5730\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5c06\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u5728\u9700\u8981\u521b\u5efa\u65b0\u52a8\u7269\u65f6\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u5c06\u8c03\u7528\u8fd9\u4e2a\u6a21\u5f0f <code>Functional Instantiation<\/code>(\u51fd\u6570\u5b9e\u4f8b\u5316)\uff0c\u5e76\u5c06\u51fd\u6570\u672c\u8eab\u79f0\u4e3a <code>constructor function<\/code>(\u6784\u9020\u51fd\u6570) \uff0c\u56e0\u4e3a\u5b83\u8d1f\u8d23\u201c\u6784\u9020\u201d\u4e00\u4e2a\u65b0\u5bf9\u8c61\u3002<\/p>\n<h3><strong>Functional Instantiation (\u51fd\u6570\u5b9e\u4f8b\u5316)<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  let animal = {}\r\n  animal.name = name\r\n  animal.energy = energy\r\n \r\n  animal.eat = function (amount) {\r\n    console.log(`${this.name} is eating.`)\r\n    this.energy += amount\r\n  }\r\n \r\n  animal.sleep = function (length) {\r\n    console.log(`${this.name} is sleeping.`)\r\n    this.energy += length\r\n  }\r\n \r\n  animal.play = function (length) {\r\n    console.log(`${this.name} is playing.`)\r\n    this.energy -= length\r\n  }\r\n \r\n  return animal\r\n}\r\n \r\nconst leo = Animal(&#39;Leo&#39;, 7)\r\nconst snoop = Animal(&#39;Snoop&#39;, 10)\r\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6bcf\u5f53\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u79cd\u65b0\u52a8\u7269\uff08\u6216\u8005\u66f4\u5e7f\u6cdb\u5730\u8bf4\u662f\u4e00\u79cd\u65b0\u7684\u201c\u5b9e\u4f8b\u201d\uff09\u65f6\uff0c\u6211\u4eec\u6240\u8981\u505a\u7684\u5c31\u662f\u8c03\u7528\u6211\u4eec\u7684 <code>Animal<\/code> \u51fd\u6570\uff0c\u5c06\u52a8\u7269\u7684 <code>name<\/code> \u548c<code>energy<\/code> \u4f20\u9012\u7ed9\u8fd9\u4e2a\u51fd\u6570\u3002\u8fd9\u975e\u5e38\u6709\u6548\uff0c\u800c\u4e14\u975e\u5e38\u7b80\u5355\u3002\u4f46\u662f\uff0c\u4f60\u6709\u53d1\u73b0\u8fd9\u79cd\u6a21\u5f0f\u7684\u4e0d\u8db3\u4e4b\u5904\u5417\uff1f\u6211\u4eec\u8981\u5c1d\u8bd5\u89e3\u51b3\u7684\u6700\u5927\u7684\u95ee\u9898\u4e0e\u4e09\u79cd\u65b9\u6cd5\u6709\u5173 \u2013 <code>eat<\/code>\uff0c<code>sleep<\/code> \u548c <code>play<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u6bcf\u4e00\u79cd\u90fd\u4e0d\u4ec5\u662f\u52a8\u6001\u7684\uff0c\u800c\u4e14\u5b83\u4eec\u4e5f\u662f\u5b8c\u5168\u901a\u7528\u7684\u3002\u8fd9\u610f\u5473\u7740\u6ca1\u6709\u7406\u7531\u91cd\u65b0\u521b\u5efa\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6b63\u5982\u6211\u4eec\u5728\u521b\u5efa\u65b0\u52a8\u7269\u65f6\u6240\u505a\u7684\u90a3\u6837\u3002<\/p>\n<p>\u6211\u4eec\u53ea\u662f\u5728\u6d6a\u8d39\u5185\u5b58\uff0c\u8ba9\u6bcf\u4e2a\u52a8\u7269\u7269\u4f53\u90fd\u6bd4\u5b83\u9700\u8981\u7684\u66f4\u5927\u3002\u4f60\u80fd\u60f3\u5230\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u5417\uff1f<\/p>\n<p>\u5982\u679c\u6211\u4eec\u6bcf\u6b21\u521b\u5efa\u4e00\u4e2a\u65b0\u52a8\u7269\u65f6\u4e0d\u9700\u8981\u91cd\u65b0\u521b\u5efa\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u800c\u662f\u5c06\u5b83\u4eec\u79fb\u52a8\u5230\u5b83\u4eec\u81ea\u5df1\u7684\u5bf9\u8c61\u4e0a\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u6bcf\u4e2a\u52a8\u7269\u5f15\u7528\u90a3\u4e2a\u5bf9\u8c61\u4e86?\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u79cd\u6a21\u5f0f\u79f0\u4e3a <code>Functional Instantiation with Shared Methods<\/code>(\u5171\u4eab\u65b9\u6cd5\u7684\u51fd\u6570\u5b9e\u4f8b\u5316) \u3002\u63cf\u8ff0\u8d77\u6765\u6709\u70b9\u5570\u55e6 \u200d \u3002<\/p>\n<h3><strong>Functional Instantiation with Shared Methods (\u5171\u4eab\u65b9\u6cd5\u7684\u51fd\u6570\u5b9e\u4f8b\u5316)<\/strong><\/h3>\n<p>JavaScript \u4ee3\u7801\uff1a<\/p>\n<pre><code class='language-javascript' lang='javascript'>const animalMethods = {\r\n  eat(amount) {\r\n    console.log(`${this.name} is eating.`)\r\n    this.energy += amount\r\n  },\r\n  sleep(length) {\r\n    console.log(`${this.name} is sleeping.`)\r\n    this.energy += length\r\n  },\r\n  play(length) {\r\n    console.log(`${this.name} is playing.`)\r\n    this.energy -= length\r\n  }\r\n}\r\n \r\nfunction Animal (name, energy) {\r\n  let animal = {}\r\n  animal.name = name\r\n  animal.energy = energy\r\n  animal.eat = animalMethods.eat\r\n  animal.sleep = animalMethods.sleep\r\n  animal.play = animalMethods.play\r\n \r\n  return animal\r\n}\r\n \r\nconst leo = Animal(&#39;Leo&#39;, 7)\r\nconst snoop = Animal(&#39;Snoop&#39;, 10)\r\n<\/code><\/pre>\n<p>\u901a\u8fc7\u5c06\u5171\u4eab\u65b9\u6cd5\u79fb\u52a8\u5230\u5b83\u4eec\u81ea\u5df1\u7684\u5bf9\u8c61\u5e76\u5728 <code>Animal<\/code> \u51fd\u6570\u4e2d\u5f15\u7528\u8be5\u5bf9\u8c61\uff0c\u6211\u4eec\u73b0\u5728\u5df2\u7ecf\u89e3\u51b3\u4e86\u5185\u5b58\u6d6a\u8d39\u548c\u52a8\u7269\u5bf9\u8c61\u8fc7\u5927\u7684\u95ee\u9898\u3002<\/p>\n<h3><strong>Object.create<\/strong><\/h3>\n<p>\u8ba9\u6211\u4eec\u4f7f\u7528 <code>Object.create<\/code> \u518d\u6b21\u6539\u8fdb\u6211\u4eec\u7684\u4f8b\u5b50\u3002 \u7b80\u800c\u8a00\u4e4b\uff0c<code>Object.create<\/code> \u5141\u8bb8\u60a8\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2a\u5bf9\u8c61\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c<code>Object.create<\/code> \u5141\u8bb8\u60a8\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ea\u8981\u8be5\u5bf9\u8c61\u4e0a\u7684\u5c5e\u6027\u67e5\u627e\u5931\u8d25\uff0c\u5b83\u5c31\u53ef\u4ee5\u67e5\u8be2\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee5\u67e5\u770b\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u662f\u5426\u5177\u6709\u8be5\u5c5e\u6027\u3002 \u8bf4\u6e05\u695a\u9700\u8981\u5f88\u591a\u6587\u5b57\uff0c \u6211\u4eec\u6765\u770b\u4e00\u4e9b\u4ee3\u7801\u3002<\/p>\n<p>JavaScript \u4ee3\u7801\uff1a<\/p>\n<pre><code class='language-javascript' lang='javascript'>const parent = {\r\n  name: &#39;Stacey&#39;,\r\n  age: 35,\r\n  heritage: &#39;Irish&#39;\r\n}\r\n \r\nconst child = Object.create(parent)\r\nchild.name = &#39;Ryan&#39;\r\nchild.age = 7\r\n \r\nconsole.log(child.name) \/\/ Ryan\r\nconsole.log(child.age) \/\/ 7\r\nconsole.log(child.heritage) \/\/ Irish\r\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u56e0\u4e3a <code>child<\/code> \u662f\u901a\u8fc7 <code>Object.create(parent)<\/code> \u521b\u5efa\u7684\uff0c\u6240\u4ee5\u6bcf\u5f53\u5728 <code>child<\/code> \u4e2d\u67e5\u627e\u5c5e\u6027\u5931\u8d25\u65f6\uff0cJavaScript \u5c31\u4f1a\u5c06\u8be5\u67e5\u627e\u59d4\u6258\u7ed9 <code>parent<\/code> \u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f <code>child<\/code> \u6ca1\u6709 <code>heritage<\/code> \u5c5e\u6027\uff0c\u5f53\u4f60\u67e5\u627e <code>child.heritage<\/code> \u65f6\u4f60\u4f1a\u5f97\u5230 <code>parent<\/code> \u7684<code>heritage<\/code> \u5c5e\u6027\uff0c\u5373 <code>Irish<\/code>\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u901a\u8fc7\u4f7f\u7528 <code>Object.create<\/code> \uff0c\u6211\u4eec\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u7b80\u5316\u4e4b\u524d\u7684 <code>Animal<\/code> \u4ee3\u7801\u5462\uff1f\u597d\u5427\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Object.create<\/code> \u59d4\u6258\u7ed9<code>animalMethods<\/code> \u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u50cf\u6211\u4eec\u4e4b\u524d\u4e00\u6837\u9010\u4e2a\u5c06\u6240\u6709\u5171\u4eab\u65b9\u6cd5\u6dfb\u52a0\u5230 <code>Animal<\/code> \u4e2d\u3002 \u4e3a\u4e86\u542c\u8d77\u6765\u5f88\u667a\u80fd\uff0c\u8ba9\u6211\u4eec\u79f0\u4e4b\u4e3a <code>Functional Instantiation with Shared Methods and Object.create<\/code>(\u4f7f\u7528\u5171\u4eab\u65b9\u6cd5\u548cObject.create\u8fdb\u884c\u51fd\u6570\u5b9e\u4f8b\u5316)  \u3002<\/p>\n<h3><strong>Functional Instantiation with Shared Methods and Object.create (\u4f7f\u7528\u5171\u4eab\u65b9\u6cd5\u548cObject.create\u8fdb\u884c\u51fd\u6570\u5b9e\u4f8b\u5316)<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p>JavaScript \u4ee3\u7801\uff1a<\/p>\n<pre><code class='language-javascript' lang='javascript'>const parent = {\r\n  name: &#39;Stacey&#39;,\r\n  age: 35,\r\n  heritage: &#39;Irish&#39;\r\n}\r\n \r\nconst child = Object.create(parent)\r\nchild.name = &#39;Ryan&#39;\r\nchild.age = 7\r\n \r\nconsole.log(child.name) \/\/ Ryan\r\nconsole.log(child.age) \/\/ 7\r\nconsole.log(child.heritage) \/\/ Irish\r\n<\/code><\/pre>\n<p>\u6240\u4ee5\u73b0\u5728\u5f53\u6211\u4eec\u8c03\u7528 <code>leo.eat<\/code> \u65f6\uff0cJavaScript \u4f1a\u5728 <code>leo<\/code> \u5bf9\u8c61\u4e0a\u67e5\u627e <code>eat<\/code> \u65b9\u6cd5\u3002 \u8fd9\u4e2a\u67e5\u627e\u5c06\u5931\u8d25\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86 <code>Object.create<\/code>\uff0c\u5b83\u5c06\u59d4\u6258\u7ed9 <code>animalMethods<\/code> \u5bf9\u8c61\uff0c\u7136\u540e\u5728\u8fd9\u91cc\u5c06\u627e\u5230 <code>eat<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u5230\u73b0\u5728\u4e3a\u6b62\u8fd8\u633a\u597d\u7684\u3002 \u5c3d\u7ba1\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u505a\u51fa\u4e00\u4e9b\u6539\u8fdb\u3002 \u4e3a\u4e86\u8de8\u5b9e\u4f8b\u5171\u4eab\u65b9\u6cd5\uff0c\u5fc5\u987b\u7ba1\u7406\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\uff08<code>animalMethods<\/code>\uff09\u4f3c\u4e4e\u6709\u70b9\u201chacky\u201d\u3002 \u8fd9\u4f3c\u4e4e\u662f\u60a8\u5e0c\u671b\u5728\u8bed\u8a00\u672c\u8eab\u4e2d\u5b9e\u73b0\u7684\u5e38\u89c1\u529f\u80fd\u3002 \u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u5c31\u662f\u4f60\u770b\u8fd9\u7bc7\u6587\u7ae0\u7684\u539f\u56e0 \u2013 prototype(\u539f\u578b) \u3002<\/p>\n<p>\u90a3\u4e48\u7a76\u7adf\u4ec0\u4e48\u662f JavaScript \u7684 prototype\uff08\u539f\u578b\uff09\u5462\uff1f \u7b80\u5355\u5730\u8bf4\uff0cJavaScript \u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u5f15\u7528\u5bf9\u8c61\u7684 prototype \u5c5e\u6027\u3002 \u6211\u4eec\u6765\u4eb2\u81ea\u6d4b\u8bd5\u4e00\u4e0b\u3002<\/p>\n<p>JavaScript \u4ee3\u7801\uff1a<\/p>\n<pre><code class='language-javascript' lang='javascript'>function doThing () {}\r\nconsole.log(doThing.prototype) \/\/ {}\r\n<\/code><\/pre>\n<p>\u5982\u679c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\uff08\u6bd4\u5982\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684 <code>animalMethods<\/code> \uff09\u6765\u7ba1\u7406\u6211\u4eec\u7684\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u53ea\u662f\u5c06\u6bcf\u4e2a\u65b9\u6cd5\u653e\u5728 <code>Animal<\/code> \u51fd\u6570\u7684 prototype(\u539f\u578b) \u5bf9\u8c61\u4e0a\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u6211\u4eec\u6240\u8981\u505a\u7684\u5c31\u662f\u4e0d\u4f7f\u7528 <code>Object.create<\/code> \u59d4\u6258\u7ed9 <code>animalMethods<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4f7f\u7528\u6765\u59d4\u6258<code>Animal.prototype<\/code>\u3002 \u6211\u4eec\u5c06\u8fd9\u79cd\u6a21\u5f0f\u79f0\u4e3a <code>Prototypal Instantiation<\/code>(\u539f\u578b\u5b9e\u4f8b\u5316)\u3002<\/p>\n<h3><strong>Prototypal Instantiation (\u539f\u578b\u5b9e\u4f8b\u5316)<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p><strong>JavaScript \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  let animal = Object.create(Animal.prototype)\r\n  animal.name = name\r\n  animal.energy = energy\r\n \r\n  return animal\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nconst leo = Animal(&#39;Leo&#39;, 7)\r\nconst snoop = Animal(&#39;Snoop&#39;, 10)\r\n \r\nleo.eat(10)\r\nsnoop.play(5)\r\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u4f60\u53ef\u4ee5\u4e3a\u81ea\u5df1\u9f13\u638c\u9f13\u52b1\u4e00\u4e0b\u4e86\u3002 <\/p>\n<p>\u540c\u6837\uff0c\u539f\u578b\u53ea\u662f JavaScript \u4e2d\u6bcf\u4e2a\u51fd\u6570\u90fd\u5177\u6709\u7684\u5c5e\u6027\uff0c\u5e76\u4e14\u5982\u4e0a\u6240\u8ff0\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u7684\u6240\u6709\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\u65b9\u6cd5\u3002 \u6211\u4eec\u6240\u6709\u7684\u529f\u80fd\u4ecd\u7136\u76f8\u540c\uff0c\u4f46\u73b0\u5728\u6211\u4eec\u4e0d\u5fc5\u4e3a\u6240\u6709\u65b9\u6cd5\u7ba1\u7406\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u4e2a\u5185\u7f6e\u4e8e <code>Animal<\/code> \u51fd\u6570\u672c\u8eab\u7684\u5bf9\u8c61<code>Animal.prototype<\/code> \u3002<\/p>\n<h2><strong>\u66f4\u6df1\u7684\uff0c\u8d70\u8d77\uff01<\/strong><\/h2>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u4e09\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u5982\u4f55\u521b\u5efa\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li>\u5982\u4f55\u5c06\u65b9\u6cd5\u6dfb\u52a0\u5230\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u4e2d\u3002<\/li>\n<li>\u5982\u4f55\u4f7f\u7528 <code>Object.create<\/code> \u5c06\u5931\u8d25\u7684\u67e5\u627e\u59d4\u6258\u7ed9\u51fd\u6570\u7684\u539f\u578b\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e09\u4e2a\u4efb\u52a1\u4f3c\u4e4e\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u3002 JavaScript \u662f\u5426\u771f\u7684\u90a3\u4e48\u7cdf\u7cd5\uff0c\u6ca1\u6709\u66f4\u7b80\u5355\uff0c\u201c\u5185\u7f6e\u201d\u7684\u65b9\u5f0f\u6765\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff1f \u6b63\u5982\u4f60\u53ef\u80fd\u5df2\u7ecf\u731c\u6d4b\u7684\u90a3\u6837\uff0c\u5b83\u662f\u901a\u8fc7\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<p>\u6211\u4eec\u91c7\u7528\u7684\u8fd9\u79cd\u7f13\u6162\u800c\u6709\u6761\u7406\u7684\u65b9\u6cd5\u7684\u597d\u5904\u662f\uff0c\u60a8\u73b0\u5728\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3 JavaScript \u4e2d\u7684 <code>new<\/code> \u5173\u952e\u5b57\u5728\u5e55\u540e\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u56de\u987e\u4e00\u4e0b\u6211\u4eec\u7684 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\uff0c\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u90e8\u5206\u662f\u521b\u5efa\u5bf9\u8c61\u5e76\u8fd4\u56de\u5b83\u3002 \u5982\u679c\u4e0d\u4f7f\u7528 <code>Object.create<\/code>\u521b\u5efa\u5bf9\u8c61\uff0c\u6211\u4eec\u5c06\u65e0\u6cd5\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u51fd\u6570\u7684\u539f\u578b\u3002 \u5982\u679c\u6ca1\u6709 <code>return<\/code> \u8bed\u53e5\uff0c\u6211\u4eec\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u8fd4\u56de\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  let animal = Object.create(Animal.prototype)\r\n  animal.name = name\r\n  animal.energy = energy\r\n \r\n  return animal\r\n}\r\n<\/code><\/pre>\n<p><code>new<\/code> \u6709\u4e00\u4e2a\u5f88\u9177\u7684\u5730\u65b9\u2014\u2014\u5f53\u60a8\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6ce8\u91ca\u6389\u7684\u8fd9\u4e24\u884c\u4ee3\u7801\u662f\u9690\u5f0f(\u5f15\u64ce)\u5b8c\u6210\u7684\uff0c\u521b\u5efa\u7684\u5bf9\u8c61\u79f0\u4e3a <code>this<\/code>\u3002<\/p>\n<p>\u4f7f\u7528\u6ce8\u91ca\u6765\u663e\u793a\u5728\u5e55\u540e\u53d1\u751f\u7684\u4e8b\u60c5\u5e76\u5047\u8bbe\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u91cd\u5199\u4e3a\u8fd9\u6837\uff1a<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  \/\/ const this = Object.create(Animal.prototype)\r\n \r\n  this.name = name\r\n  this.energy = energy\r\n \r\n  \/\/ return this\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n<\/code><\/pre>\n<p>\u53bb\u6389\u6ce8\u91ca\u540e\uff1a<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n<\/code><\/pre>\n<p>\u540c\u6837\uff0c\u8fd9\u6837\u505a\u4ee5\u53ca\u4e3a\u6211\u4eec\u521b\u5efa <code>this<\/code> \u5bf9\u8c61\u7684\u539f\u56e0\u662f\uff0c\u6211\u4eec\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u5728\u8c03\u7528\u51fd\u6570\u65f6\u4e0d\u4f7f\u7528 <code>new<\/code> \uff0c\u5219\u8be5\u5bf9\u8c61\u6c38\u8fdc\u4e0d\u4f1a\u521b\u5efa\uff0c\u4e5f\u4e0d\u4f1a\u9690\u5f0f\u8fd4\u56de\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u770b\u5230\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nconst leo = Animal(&#39;Leo&#39;, 7)\r\nconsole.log(leo) \/\/ undefined\r\n<\/code><\/pre>\n<p>\u6b64\u6a21\u5f0f\u7684\u540d\u79f0\u662f <code>Pseudoclassical Instantiation<\/code>(\u4f2a\u7c7b\u5b9e\u4f8b\u5316) \u3002<\/p>\n<p>\u5982\u679c JavaScript \u4e0d\u662f\u60a8\u7684\u7b2c\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u60a8\u53ef\u80fd\u4f1a\u6709\u70b9\u4e0d\u5b89\u3002<\/p>\n<blockquote>\n<p>\u201cWTF\u8fd9\u4e2a\u5bb6\u4f19\u53ea\u662f\u91cd\u65b0\u521b\u9020\u4e86\u4e00\u4e2a\u66f4\u7cdf\u7cd5\u7684\u7248\u672c\u201d \u2013 \u4f60<\/p>\n<\/blockquote>\n<p>\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u719f\u6089\u7684\u4eba\uff0cClass(\u7c7b) \u5141\u8bb8\u60a8\u4e3a\u5bf9\u8c61\u521b\u5efa\u6a21\u677f\u3002\u7136\u540e\uff0c\u65e0\u8bba\u4f55\u65f6\u521b\u5efa\u8be5\u7c7b\u7684\u5b9e\u4f8b\uff0c\u90fd\u4f1a\u83b7\u5f97\u4e00\u4e2a\u5177\u6709\u6a21\u677f\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u542c\u8d77\u6765\u6709\u70b9\u719f\u6089\uff1f\u8fd9\u57fa\u672c\u4e0a\u5c31\u662f\u6211\u4eec\u5bf9\u4e0a\u9762\u7684 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\u3002\u4f46\u662f\u5bf9\u4e8e <code>Animal<\/code> \u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u5e38\u89c4\u7684\u65e7 JavaScript \u51fd\u6570\u6765\u91cd\u65b0\u521b\u5efa\u76f8\u540c\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\u3002\u5f53\u7136\uff0c\u5b83\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\u4ee5\u53ca\u4e00\u4e9b\u5173\u4e8e JavaScript \u201c\u5f15\u64ce\u201d \u6240\u5904\u7406\u7684\u4e8b\u60c5\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4f46\u7ed3\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u8fd9\u662f\u4e2a\u597d\u6d88\u606f\u3002 JavaScript \u4e0d\u662f\u4e00\u79cd \u201c\u6b7b\u201d \u8bed\u8a00\u3002\u5b83\u4e0d\u65ad\u5f97\u5230\u6539\u8fdb\uff0c\u5e76\u7531 TC-39\u59d4\u5458\u4f1a \u4e0d\u65ad\u7684\u5236\u5b9a\u6807\u51c6\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f JavaScript \u7684\u521d\u59cb\u7248\u672c\u4e0d\u652f\u6301\u7c7b\uff0c\u4e5f\u4e0d\u5f71\u54cd\u540e\u7eed\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u5b98\u65b9\u89c4\u8303\u4e2d\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u6b63\u662fTC-39\u59d4\u5458\u4f1a\u6240\u505a\u7684\u4e8b\u60c5\u3002 2015\u5e74\uff0c\u53d1\u5e03\u4e86 EcmaScript\uff08\u5b98\u65b9JavaScript\u89c4\u8303\uff096 \uff0c\u652f\u6301 Classes(\u7c7b) \u548c <code>class<\/code> \u5173\u952e\u5b57\u3002\u8ba9\u6211\u4eec\u770b\u770b\u4e0a\u9762\u7684 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u65b0\u7684 class(\u7c7b) \u8bed\u6cd5\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>class Animal {\r\n  constructor(name, energy) {\r\n    this.name = name\r\n    this.energy = energy\r\n  }\r\n  eat(amount) {\r\n    console.log(`${this.name} is eating.`)\r\n    this.energy += amount\r\n  }\r\n  sleep(length) {\r\n    console.log(`${this.name} is sleeping.`)\r\n    this.energy += length\r\n  }\r\n  play(length) {\r\n    console.log(`${this.name} is playing.`)\r\n    this.energy -= length\r\n  }\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n<\/code><\/pre>\n<p>\u5f88\u5e72\u51c0\u662f\u5427\uff1f<\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u8fd9\u662f\u521b\u5efa\u7c7b\u7684\u65b0\u65b9\u6cd5\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u524d\u9762\u82b1\u4e86\u8fd9\u4e48\u591a\u65f6\u95f4\u6765\u8ba8\u8bba\u65e7\u7684\u65b9\u5f0f\u5462\uff1f \u539f\u56e0\u662f\u65b0\u7684\u65b9\u5f0f\uff08\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\uff09\u53ea\u662f\u7ecf\u5178\u4f2a\u7c7b\u6a21\u5f0f\u7684 \u201c\u8bed\u6cd5\u7cd6\u201d\u3002 \u4e3a\u4e86\u5b8c\u5168\u7406\u89e3 ES6 \u7c7b\u7684\u4fbf\u6377\u8bed\u6cd5\uff0c\u9996\u5148\u5fc5\u987b\u7406\u89e3\u7ecf\u5178\u7684\u4f2a\u7c7b\u6a21\u5f0f\u3002<\/p>\n<p>\u5173\u4e8e ES6 Classes(\u7c7b) \u66f4\u591a\u76f8\u5173\u4fe1\u606f\u53ef\u4ee5\u67e5\u770b \u9762\u5411\u5bf9\u8c61\u7684 JavaScript \u2013 \u6df1\u5165\u4e86\u89e3 ES6 \u7c7b\u3002<\/p>\n<p>\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86 JavaScript \u539f\u578b\u7684\u57fa\u7840\u77e5\u8bc6\u3002 \u672c\u6587\u7684\u5176\u4f59\u90e8\u5206\u5c06\u81f4\u529b\u4e8e\u52a0\u6df1\u7406\u89e3\u76f8\u5173\u77e5\u8bc6\u7684\u4e3b\u9898\u3002 \u5728\u53e6\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u770b\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u5e76\u4f7f\u7528\u5b83\u4eec\u6765\u7406\u89e3 JavaScript \u4e2d\u7ee7\u627f\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<h2><strong>\u6570\u7ec4\u65b9\u6cd5<\/strong><\/h2>\n<p>\u6211\u4eec\u5728\u4e0a\u9762\u6df1\u5165\u8ba8\u8bba\u4e86\u5982\u4f55\u5728\u7c7b\u7684\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\u65b9\u6cd5\uff0c\u60a8\u5e94\u8be5\u5c06\u8fd9\u4e9b\u65b9\u6cd5\u653e\u5728\u7c7b\uff08\u6216\u51fd\u6570\uff09\u539f\u578b\u4e0a\u3002 \u5982\u679c\u6211\u4eec\u67e5\u770b <code>Array<\/code> \u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u76f8\u540c\u7684\u6a21\u5f0f\u3002 \u4ece\u5386\u53f2\u4e0a\u770b\uff0c\u60a8\u53ef\u80fd\u5df2\u7ecf\u521b\u5efa\u4e86\u8fd9\u6837\u7684\u6570\u7ec4\uff1a<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>const friends = []\r\n<\/code><\/pre>\n<p>\u4e8b\u5b9e\u8bc1\u660e\uff0c\u8fd9\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <code>Array<\/code> \u7c7b\u5b9e\u4f8b\u7684\u8bed\u6cd5\u7cd6\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>const friendsWithSugar = [] const friendsWithoutSugar = new Array()\r\n<\/code><\/pre>\n<p>\u60a8\u53ef\u80fd\u4ece\u672a\u60f3\u8fc7\uff1a\u6570\u7ec4\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u662f\u5982\u4f55\u5177\u6709\u6240\u6709\u5185\u7f6e\u65b9\u6cd5\u7684\uff08<code>splice<\/code> , <code>slice<\/code>, <code>pop<\/code> \u7b49\uff09\uff1f<\/p>\n<p>\u6b63\u5982\u60a8\u73b0\u5728\u6240\u77e5\uff0c\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e9b\u65b9\u6cd5\u5b58\u5728\u4e8e <code>Array.prototype<\/code> \u4e0a\uff0c\u5f53\u60a8\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <code>Array<\/code> \u5b9e\u4f8b\u65f6\uff0c\u60a8\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u5728\u67e5\u627e\u5931\u8d25\u65f6\u5c06\u8be5\u59d4\u6258\u8bbe\u7f6e\u4e3a <code>Array.prototype<\/code> \u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730 <code>console.log(Array.prototype)<\/code> \u6765\u67e5\u770b\u6240\u6709\u6570\u7ec4\u7684\u65b9\u6cd5\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>console.log(Array.prototype)\r\n \r\n\/*\r\n  concat: ?n concat()\r\n  constructor: ?n Array()\r\n  copyWithin: ?n copyWithin()\r\n  entries: ?n entries()\r\n  every: ?n every()\r\n  fill: ?n fill()\r\n  filter: ?n filter()\r\n  find: ?n find()\r\n  findIndex: ?n findIndex()\r\n  forEach: ?n forEach()\r\n  includes: ?n includes()\r\n  indexOf: ?n indexOf()\r\n  join: ?n join()\r\n  keys: ?n keys()\r\n  lastIndexOf: ?n lastIndexOf()\r\n  length: 0n\r\n  map: ?n map()\r\n  pop: ?n pop()\r\n  push: ?n push()\r\n  reduce: ?n reduce()\r\n  reduceRight: ?n reduceRight()\r\n  reverse: ?n reverse()\r\n  shift: ?n shift()\r\n  slice: ?n slice()\r\n  some: ?n some()\r\n  sort: ?n sort()\r\n  splice: ?n splice()\r\n  toLocaleString: ?n toLocaleString()\r\n  toString: ?n toString()\r\n  unshift: ?n unshift()\r\n  values: ?n values()\r\n*\/\r\n<\/code><\/pre>\n<p>Objects(\u5bf9\u8c61) \u4e5f\u662f\u5b8c\u5168\u76f8\u540c\u7684\u903b\u8f91\u3002 \u6240\u6709\u5bf9\u8c61\u5c06\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9 <code>Object.prototype<\/code> \uff0c\u8fd9\u5c31\u662f\u6240\u6709\u5bf9\u8c61\u90fd\u6709 <code>toString<\/code> \u548c<code>hasOwnProperty<\/code> \u7b49\u65b9\u6cd5\u7684\u539f\u56e0\u3002<\/p>\n<h3><strong>\u9759\u6001\u65b9\u6cd5<\/strong><\/h3>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u4e3a\u4ec0\u4e48\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u7c7b\u7684\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\u65b9\u6cd5\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9 Class \u5f88\u91cd\u8981\u4f46\u4e0d\u9700\u8981\u53c8\u8de8\u5b9e\u4f8b\u5171\u4eab\u7684\u65b9\u6cd5\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f \u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e00\u7cfb\u5217 <code>Animal<\/code> \u5b9e\u4f8b\u5e76\u51b3\u5b9a\u4e0b\u4e00\u4e2a\u9700\u8981\u5582\u98df\u7684\u5bf9\u8c61\uff0c\u4f1a\u600e\u6837? \u6211\u4eec\u5c06\u5176\u79f0\u4e3a <code>nextToEat<\/code>\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function nextToEat (animals) {\r\n  const sortedByLeastEnergy = animals.sort((a,b) =&gt; {\r\n    return a.energy - b.energy\r\n  })\r\n \r\n  return sortedByLeastEnergy[0].name\r\n}\r\n<\/code><\/pre>\n<p>\u6211\u4eec\u4e0d\u5e0c\u671b\u5728\u6240\u6709\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\u5b83\uff0c\u6240\u4ee5\u5728 <code>Animal.prototype<\/code> \u4e0a\u4f7f\u7528 <code>nextToEat<\/code> \u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u8f85\u52a9\u65b9\u6cd5\u3002 \u6240\u4ee5\u5982\u679c <code>nextToEat<\/code> \u4e0d\u5e94\u8be5\u5b58\u5728\u4e8e <code>Animal.prototype<\/code> \u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u628a\u5b83\u653e\u5728\u54ea\u91cc\u5462\uff1f \u90a3\u4e48\u663e\u800c\u6613\u89c1\u7684\u7b54\u6848\u662f\u6211\u4eec\u53ef\u4ee5\u5c06 <code>nextToEat<\/code> \u653e\u5728\u4e0e <code>Animal<\/code> \u7c7b\u76f8\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u7136\u540e\u50cf\u6211\u4eec\u5e73\u5e38\u90a3\u6837\uff0c\u5728\u9700\u8981\u65f6\u5f15\u7528\u5b83\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>class Animal {\r\n  constructor(name, energy) {\r\n    this.name = name\r\n    this.energy = energy\r\n  }\r\n  eat(amount) {\r\n    console.log(`${this.name} is eating.`)\r\n    this.energy += amount\r\n  }\r\n  sleep(length) {\r\n    console.log(`${this.name} is sleeping.`)\r\n    this.energy += length\r\n  }\r\n  play(length) {\r\n    console.log(`${this.name} is playing.`)\r\n    this.energy -= length\r\n  }\r\n}\r\n \r\nfunction nextToEat (animals) {\r\n  const sortedByLeastEnergy = animals.sort((a,b) =&gt; {\r\n    return a.energy - b.energy\r\n  })\r\n \r\n  return sortedByLeastEnergy[0].name\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n \r\nconsole.log(nextToEat([leo, snoop])) \/\/ Leo\r\n<\/code><\/pre>\n<p>\u73b0\u5728\u8fd9\u53ef\u884c\uff0c\u4f46\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u53ea\u8981\u6709\u4e00\u4e2a\u7279\u5b9a\u4e8e\u7c7b\u672c\u8eab\u7684\u65b9\u6cd5\uff0c\u4f46\u4e0d\u9700\u8981\u5728\u8be5\u7c7b\u7684\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\uff0c\u5c31\u53ef\u4ee5\u5c06\u5176\u6dfb\u52a0\u4e3a\u7c7b\u7684 <code>static<\/code>(\u9759\u6001) \u5c5e\u6027\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>class Animal {\r\n  constructor(name, energy) {\r\n    this.name = name\r\n    this.energy = energy\r\n  }\r\n  eat(amount) {\r\n    console.log(`${this.name} is eating.`)\r\n    this.energy += amount\r\n  }\r\n  sleep(length) {\r\n    console.log(`${this.name} is sleeping.`)\r\n    this.energy += length\r\n  }\r\n  play(length) {\r\n    console.log(`${this.name} is playing.`)\r\n    this.energy -= length\r\n  }\r\n  static nextToEat(animals) {\r\n    const sortedByLeastEnergy = animals.sort((a,b) =&gt; {\r\n      return a.energy - b.energy\r\n    })\r\n \r\n    return sortedByLeastEnergy[0].name\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u7c7b\u4e0a\u6dfb\u52a0\u4e86 <code>nextToEat<\/code> \u4f5c\u4e3a <code>static<\/code>(\u9759\u6001) \u5c5e\u6027\uff0c\u6240\u4ee5\u5b83\u5b58\u5728\u4e8e <code>Animal<\/code> \u7c7b\u672c\u8eab\uff08\u800c\u4e0d\u662f\u5b83\u7684\u539f\u578b\uff09\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528 <code>Animal.nextToEat<\/code> \u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>const leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n \r\nconsole.log(Animal.nextToEat([leo, snoop])) \/\/ Leo\r\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u6211\u4eec\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u90fd\u9075\u5faa\u4e86\u7c7b\u4f3c\u7684\u6a21\u5f0f\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u4f7f\u7528 ES5 \u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4e86\u5982\u4f55\u4f7f\u7528 <code>static<\/code> \u5173\u952e\u5b57\u5c06\u65b9\u6cd5\u76f4\u63a5\u653e\u5728\u7c7b\u672c\u8eab\u4e0a\u3002 \u4f7f\u7528ES5\uff0c\u540c\u6837\u7684\u6a21\u5f0f\u5c31\u50cf\u624b\u52a8\u5c06\u65b9\u6cd5\u6dfb\u52a0\u5230\u51fd\u6570\u5bf9\u8c61\u4e00\u6837\u7b80\u5355\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nAnimal.nextToEat = function (nextToEat) {\r\n  const sortedByLeastEnergy = animals.sort((a,b) =&gt; {\r\n    return a.energy - b.energy\r\n  })\r\n \r\n  return sortedByLeastEnergy[0].name\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst snoop = new Animal(&#39;Snoop&#39;, 10)\r\n \r\nconsole.log(Animal.nextToEat([leo, snoop])) \/\/ Leo\r\n<\/code><\/pre>\n<h3><strong>\u83b7\u53d6\u5bf9\u8c61\u7684\u539f\u578b<\/strong><\/h3>\n<p>\u65e0\u8bba\u60a8\u4f7f\u7528\u54ea\u79cd\u6a21\u5f0f\u521b\u5efa\u5bf9\u8c61\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 <code>Object.getPrototypeOf<\/code> \u65b9\u6cd5\u5b8c\u6210\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u539f\u578b\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconst prototype = Object.getPrototypeOf(leo)\r\n \r\nconsole.log(prototype)\r\n\/\/ {constructor: ?, eat: ?, sleep: ?, play: ?}\r\n \r\nprototype === Animal.prototype \/\/ true\r\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6709\u4e24\u4e2a\u8981\u70b9\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u4f1a\u6ce8\u610f\u5230 <code>proto<\/code> \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u67094\u79cd\u65b9\u6cd5\uff0c<code>constructor<\/code>\uff0c<code>eat<\/code>\uff0c<code>sleep<\/code>\uff0c\u548c<code>play<\/code>\u3002\u90a3\u8bb2\u5f97\u901a\u3002\u6211\u4eec\u5c06\u5b9e\u4f8b\u4f20\u9012\u7ed9<code>getPrototypeOf<\/code>\uff0c<code>leo<\/code> \u83b7\u53d6\u4e86\u5b9e\u4f8b\u7684\u539f\u578b\uff0c\u8fd9\u91cc\u662f\u6240\u6709\u7684\u65b9\u6cd5\u3002\u8fd9\u63d0\u793a\u6211\u4eec\uff0c\u5173\u4e8e\u539f\u578b\u7684\u53e6\u5916\u4e00\u4ef6\u4e8b\u6211\u4eec\u8fd8\u6ca1\u6709\u8ba8\u8bba\u8fc7\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u539f\u578b\u5bf9\u8c61\u5c06\u5177\u6709 <code>constructor<\/code> \u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u6307\u5411\u539f\u59cb\u51fd\u6570\u6216\u521b\u5efa\u5b9e\u4f8b\u7684\u7c7b\u3002\u8fd9\u4e5f\u610f\u5473\u7740 JavaScript \u9ed8\u8ba4\u5728\u539f\u578b\u4e0a\u653e\u7f6e <code>constructor<\/code> \u5c5e\u6027\uff0c\u6240\u4ee5\u4efb\u4f55\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u901a\u8fc7 <code>instance.constructor<\/code> \u8bbf\u95ee\u5b83\u4eec\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u7b2c\u4e8c\u4e2a\u8981\u70b9\u662f <code>Object.getPrototypeOf(leo) === Animal.prototype<\/code> \u3002\u8fd9\u4e5f\u662f\u6709\u9053\u7406\u7684\u3002 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\u6709\u4e00\u4e2a prototype(\u539f\u578b) \u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6240\u6709\u5b9e\u4f8b\u4e4b\u95f4\u5171\u4eab\u65b9\u6cd5\uff0c<code>getPrototypeOf<\/code> \u5141\u8bb8\u6211\u4eec\u67e5\u770b\u5b9e\u4f8b\u672c\u8eab\u7684\u539f\u578b\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\nconsole.log(leo.constructor) \/\/ Logs the constructor function\r\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u914d\u5408\u6211\u4eec\u4e4b\u524d\u4f7f\u7528 <code>Object.create<\/code> \u6240\u8ba8\u8bba\u7684\u5185\u5bb9\uff0c\u5176\u5de5\u4f5c\u539f\u56e0\u662f\u56e0\u4e3a\u4efb\u4f55 <code>Animal<\/code> \u5b9e\u4f8b\u90fd\u4f1a\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9 <code>Animal.prototype<\/code>\u3002 \u56e0\u6b64\uff0c\u5f53\u60a8\u5c1d\u8bd5\u8bbf\u95ee <code>leo.prototype<\/code> \u65f6\uff0c <code>leo<\/code> \u6ca1\u6709 <code>prototype<\/code> \u5c5e\u6027\uff0c\u56e0\u6b64\u5b83\u4f1a\u5c06\u8be5\u67e5\u627e\u59d4\u6258\u7ed9<code>Animal.prototype<\/code>\uff0c\u5b83\u786e\u5b9e\u5177\u6709 <code>constructor<\/code> \u5c5e\u6027\u3002 \u5982\u679c\u8fd9\u6bb5\u6ca1\u6709\u770b\u61c2\uff0c\u8bf7\u56de\u8fc7\u5934\u6765\u9605\u8bfb\u4e0a\u9762\u7684 <code>Object.create<\/code> \u3002<\/p>\n<p>\u60a8\u53ef\u80fd\u4ee5\u524d\u770b\u5230\u8fc7\u4f7f\u7528 <code>__proto__<\/code> \u83b7\u53d6\u5b9e\u4f8b\u7684\u539f\u578b\u3002 \u8fd9\u662f\u8fc7\u53bb\u7684\u9057\u7269\u3002 \u73b0\u5728\uff0c\u5982\u4e0a\u6240\u8ff0\u4f7f\u7528 <code>Object.getPrototypeOf(instance)<\/code> \u83b7\u53d6\u5b9e\u4f8b\u7684\u539f\u578b\u3002<\/p>\n<h2><strong>\u786e\u5b9a\u5c5e\u6027\u662f\u5426\u5b58\u5728\u4e8e\u539f\u578b\u4e0a<\/strong><\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u9700\u8981\u77e5\u9053\u5c5e\u6027\u662f\u5426\u5b58\u5728\u4e8e\u5b9e\u4f8b\u672c\u8eab\u4e0a\uff0c\u8fd8\u662f\u5b58\u5728\u4e8e\u5bf9\u8c61\u59d4\u6258\u7684\u539f\u578b\u4e0a\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6211\u4eec\u521b\u5efa\u7684 <code>leo<\/code> \u5bf9\u8c61\u6765\u77e5\u9053\u8fd9\u4e00\u70b9\u3002\u5047\u8bbe\u76ee\u6807\u662f\u5faa\u73af <code>leo<\/code> \u5e76\u8bb0\u5f55\u5176\u6240\u6709\u952e\u548c\u503c\u3002\u4f7f\u7528 <code>for in<\/code> \u5faa\u73af\uff0c\u53ef\u80fd\u770b\u8d77\u6765\u50cf\u8fd9\u6837\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\n \r\nfor(let key in leo) {\r\n  console.log(`Key: ${key}. Value: ${leo[key]}`)\r\n}\r\n<\/code><\/pre>\n<p>\u4f60\u671f\u671b\u770b\u5230\u4ec0\u4e48\uff1f\u6700\u6709\u53ef\u80fd\u7684\u662f\uff0c\u5b83\u662f\u8fd9\u6837\u7684 \u2013<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code>Key: name. Value: Leo\r\nKey: energy. Value: 7\r\n<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8fd0\u884c\u4ee3\u7801\uff0c\u4f60\u770b\u5230\u7684\u662f\u8fd9\u6837\u7684 \u2013<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>Key: name. Value: Leo\r\nKey: energy. Value: 7\r\nKey: eat. Value: function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\nKey: sleep. Value: function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\nKey: play. Value: function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4e3a\u4ec0\u4e48\uff1f <code>for in<\/code> \u5faa\u73af\u5c06\u5faa\u73af\u904d\u5386\u5bf9\u8c61\u672c\u8eab\u4ee5\u53ca\u5b83\u6240\u59d4\u6258\u7684\u539f\u578b\u7684\u6240\u6709 <strong>\u53ef\u679a\u4e3e\u5c5e\u6027<\/strong> \u3002 \u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u60a8\u6dfb\u52a0\u5230\u51fd\u6570\u539f\u578b\u7684\u4efb\u4f55\u5c5e\u6027\u90fd\u662f\u53ef\u679a\u4e3e\u7684\uff0c\u6211\u4eec\u4e0d\u4ec5\u4f1a\u770b\u5230<code>name<\/code> \u548c <code>energy<\/code> \uff0c\u8fd8\u4f1a\u770b\u5230\u539f\u578b\u4e0a\u7684\u6240\u6709\u65b9\u6cd5 \u2013 <code>eat<\/code>\uff0c<code>sleep<\/code> \u548c <code>play<\/code> \u3002 \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9a\u6240\u6709\u539f\u578b\u65b9\u6cd5\u90fd\u662f\u4e0d\u53ef\u679a\u4e3e\u7684\uff0c\u6216\u8005\u5982\u679c\u5c5e\u6027\u5728 <code>leo<\/code> \u5bf9\u8c61\u672c\u8eab\u4e0a\u800c\u4e0d\u662f <code>leo<\/code> \u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u7684\u539f\u578b\u4e0a\u3002 <code>hasOwnProperty<\/code> \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002<\/p>\n<p><code>hasOwnProperty<\/code> \u662f\u6bcf\u4e2a\u5bf9\u8c61\u4e0a\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u5bf9\u8c61\u662f\u5426\u5177\u6709\u6307\u5b9a\u7684\u5c5e\u6027\u4f5c\u4e3a\u5176\u81ea\u8eab\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u59d4\u6258\u7ed9\u7684\u539f\u578b\u3002 \u8fd9\u6b63\u662f\u6211\u4eec\u6240\u9700\u8981\u7684\u3002 \u73b0\u5728\u6709\u4e86\u8fd9\u4e9b\u65b0\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6211\u4eec\u7684\u4ee3\u7801\uff0c\u4ee5\u4fbf\u5229\u7528 <code>for in<\/code> \u5faa\u73af\u4e2d\u7684 <code>hasOwnProperty<\/code> \u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>...\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\n \r\nfor(let key in leo) {\r\n  if (leo.hasOwnProperty(key)) {\r\n    console.log(`Key: ${key}. Value: ${leo[key]}`)\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>\u800c\u73b0\u5728\u6211\u4eec\u770b\u5230\u7684\u53ea\u662f <code>leo<\/code> \u5bf9\u8c61\u672c\u8eab\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f <code>leo<\/code> \u539f\u578b\u4e2d\u7684\u65b9\u6cd5\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>Key: name. Value: Leo\r\nKey: energy. Value: 7\r\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u4ecd\u7136\u5bf9 <code>hasOwnProperty<\/code> \u611f\u5230\u56f0\u60d1\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5e2e\u4f60\u6d88\u9664\u56f0\u60d1\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nAnimal.prototype.eat = function (amount) {\r\n  console.log(`${this.name} is eating.`)\r\n  this.energy += amount\r\n}\r\n \r\nAnimal.prototype.sleep = function (length) {\r\n  console.log(`${this.name} is sleeping.`)\r\n  this.energy += length\r\n}\r\n \r\nAnimal.prototype.play = function (length) {\r\n  console.log(`${this.name} is playing.`)\r\n  this.energy -= length\r\n}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\n \r\nleo.hasOwnProperty(&#39;name&#39;) \/\/ true\r\nleo.hasOwnProperty(&#39;energy&#39;) \/\/ true\r\nleo.hasOwnProperty(&#39;eat&#39;) \/\/ false\r\nleo.hasOwnProperty(&#39;sleep&#39;) \/\/ false\r\nleo.hasOwnProperty(&#39;play&#39;) \/\/ false\r\n<\/code><\/pre>\n<h3><strong>\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h3>\n<p>\u6709\u65f6\u60a8\u60f3\u77e5\u9053\u5bf9\u8c61\u662f\u5426\u662f\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002 \u4e3a\u6b64\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>instanceof<\/code> \u8fd0\u7b97\u7b26\u3002 \u7528\u4f8b\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u5982\u679c\u60a8\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7\u5b83\uff0c\u5b9e\u9645\u7684\u8bed\u6cd5\u6709\u70b9\u5947\u602a\u3002 \u5b83\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code>object instanceof Class\r\n<\/code><\/pre>\n<p>\u5982\u679c <code>object<\/code> \u662f <code>Class<\/code> \u7684\u5b9e\u4f8b\uff0c\u5219\u4e0a\u9762\u7684\u8bed\u53e5\u5c06\u8fd4\u56de <code>true<\/code> \uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code> \u3002\u56de\u5230\u6211\u4eec\u7684 <code>Animal<\/code> \u793a\u4f8b\uff0c\u6211\u4eec\u4f1a\u6709\u7c7b\u4f3c\u7684\u4e1c\u897f\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code>function Animal (name, energy) {  this.name = name  this.energy = energy} function User () {} const leo = new Animal(&#39;Leo&#39;, 7) leo instanceof Animal \/\/ trueleo instanceof User \/\/ false\r\n<\/code><\/pre>\n<p><code>instanceof<\/code> \u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u68c0\u67e5\u5bf9\u8c61\u539f\u578b\u94fe\u4e2d\u662f\u5426\u5b58\u5728 <code>constructor.prototype<\/code> \u3002 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>leo instanceof Animal<\/code> \u4e3a <code>true<\/code> \uff0c\u56e0\u4e3a<code>Object.getPrototypeOf(leo) === Animal.prototype<\/code>\u3002 \u53e6\u5916\uff0c<code>leo instanceof User<\/code> \u4e3a <code>false<\/code> \uff0c\u56e0\u4e3a <code>Object.getPrototypeOf(leo) !== User.prototype<\/code>\u3002<\/p>\n<h3><strong>\u521b\u5efa\u65b0\u7684\u4e0d\u53ef\u77e5\u6784\u9020\u51fd\u6570<\/strong><\/h3>\n<p>\u4f60\u80fd\u53d1\u73b0\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u5417\uff1f<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nfunction User () {}\r\n \r\nconst leo = new Animal(&#39;Leo&#39;, 7)\r\n \r\nleo instanceof Animal \/\/ true\r\nleo instanceof User \/\/ false\r\n<\/code><\/pre>\n<p>\u5373\u4f7f\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684 JavaScript \u5f00\u53d1\u4eba\u5458\u6709\u65f6\u4e5f\u4f1a\u56e0\u4e3a\u4e0a\u9762\u7684\u4f8b\u5b50\u800c\u88ab\u7eca\u5012\u3002 \u56e0\u4e3a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u4e4b\u524d\u5b66\u8fc7\u7684 <code>pseudoclassical pattern<\/code>(\u7ecf\u5178\u4f2a\u7c7b\u6a21\u5f0f)\uff0c\u6240\u4ee5\u5f53\u8c03\u7528 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u5b83\u3002 \u5982\u679c\u6211\u4eec\u4e0d\u8fd9\u6837\u505a\uff0c\u5219\u4e0d\u4f1a\u521b\u5efa <code>this<\/code> \u5173\u952e\u5b57\uff0c\u4e5f\u4e0d\u4f1a\u9690\u5f0f\u8fd4\u56de\u3002<\/p>\n<p>\u4f5c\u4e3a\u590d\u4e60\uff0c\u6ce8\u91ca\u6389\u7684\u884c\u662f\u5728\u51fd\u6570\u4e0a\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u65f6\u5e55\u540e\u6240\u505a\u7684\u4e8b\u60c5\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n \r\nconst leo = Animal(&#39;Leo&#39;, 7)\r\n<\/code><\/pre>\n<p>\u8fd9\u4f3c\u4e4e\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ec6\u8282\uff0c\u8ba9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u8bb0\u4f4f\u3002 \u5047\u8bbe\u6211\u4eec\u6b63\u5728\u4e0e\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5408\u4f5c\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u786e\u4fdd\u6211\u4eec\u7684 <code>Animal<\/code> \u6784\u9020\u51fd\u6570\u59cb\u7ec8\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u5462\uff1f \u4e8b\u5b9e\u8bc1\u660e\uff0c\u5b83\u662f\u901a\u8fc7\u4f7f\u7528\u6211\u4eec\u4e4b\u524d\u5b66\u5230\u7684 <code>instanceof<\/code> \u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u90a3\u4e48\u6784\u9020\u51fd\u6570\u4f53\u7684\u5185\u90e8\u5c06\u662f\u6784\u9020\u51fd\u6570\u672c\u8eab\u7684\u5b9e\u4f8b\u3002 \u90a3\u662f\u5f88\u591a\u6587\u5b57\u624d\u80fd\u8bf4\u6e05\u695a\u7684\u3002 \u8fd9\u662f\u4e00\u4e9b\u4ee3\u7801\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  \/\/ const this = Object.create(Animal.prototype)\r\n \r\n  this.name = name\r\n  this.energy = energy\r\n \r\n  \/\/ return this\r\n}\r\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u91cd\u65b0\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u53ea\u5411\u51fd\u6570\u7684\u4f7f\u7528\u8005\u6253\u5370\u8b66\u544a\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>function Animal (name, energy) {\r\n  if (this instanceof Animal === false) {\r\n    console.warn(&#39;Forgot to call Animal with the new keyword&#39;)\r\n  }\r\n \r\n  this.name = name\r\n  this.energy = energy\r\n}\r\n<\/code><\/pre>\n<p>\u73b0\u5728\u65e0\u8bba\u662f\u5426\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528 <code>Animal<\/code>\uff0c\u5b83\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<h3><strong>\u91cd\u65b0\u521b\u5efa Object.create<\/strong><\/h3>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u975e\u5e38\u4f9d\u8d56\u4e8e <code>Object.create<\/code> \u6765\u521b\u5efa\u59d4\u6258\u7ed9\u6784\u9020\u51fd\u6570\u539f\u578b\u7684\u5bf9\u8c61\u3002 \u6b64\u65f6\uff0c\u60a8\u5e94\u8be5\u77e5\u9053\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 <code>Object.create<\/code> \uff0c\u4f46\u60a8\u53ef\u80fd\u6ca1\u6709\u60f3\u5230\u7684\u4e00\u4ef6\u4e8b\u662f<code>Object.create<\/code> \u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002 \u4e3a\u4e86\u8ba9\u60a8\u771f\u6b63\u4e86\u89e3 <code>Object.create<\/code> \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5c06\u91cd\u65b0\u521b\u5efa\u5b83\u3002 \u9996\u5148\uff0c\u6211\u4eec\u5bf9 <code>Object.create<\/code> \u7684\u5de5\u4f5c\u539f\u7406\u4e86\u89e3\u591a\u5c11\uff1f<\/p>\n<ol>\n<li>\u5b83\u63a5\u53d7\u4e00\u4e2a\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li>\u5b83\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u53c2\u6570\u5bf9\u8c61\u3002<\/li>\n<li>\u5b83\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<p>\u8ba9\u6211\u4eec\u4ece\u7b2c1\u70b9\u5f00\u59cb\u5427\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>Object.create = function (objToDelegateTo) {\r\n \r\n}\r\n<\/code><\/pre>\n<p>\u5f88\u7b80\u5355\u3002<\/p>\n<p>\u73b0\u5728\u7b2c2\u70b9 \u2013 \u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u53c2\u6570\u5bf9\u8c61\u3002 \u8fd9\u4e2a\u6709\u70b9\u68d8\u624b\u3002 \u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u6211\u4eec\u5bf9 <code>new<\/code> \u5173\u952e\u5b57\u548c\u539f\u578b\u5982\u4f55\u5728 JavaScript \u4e2d\u5de5\u4f5c\u7684\u77e5\u8bc6\u3002\u9996\u5148\uff0c\u5728 <code>Object.create<\/code> \u5b9e\u73b0\u7684\u4e3b\u4f53\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7a7a\u51fd\u6570\u3002 \u7136\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u7a7a\u51fd\u6570\u7684\u539f\u578b\u8bbe\u7f6e\u4e3a\u53c2\u6570\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u7a7a\u51fd\u6570\u3002\u5982\u679c\u6211\u4eec\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u4e5f\u4f1a\u5b8c\u6210\u7b2c3\u70b9\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>Object.create = function (objToDelegateTo) {\r\n  function Fn(){}\r\n  Fn.prototype = objToDelegateTo\r\n  return new Fn()\r\n}\r\n<\/code><\/pre>\n<p>\u6709\u70b9\u91ce\u86ee\u662f\u5427\uff1f\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5427\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u51fd\u6570 <code>Fn<\/code> \u65f6\uff0c\u5b83\u5e26\u6709\u4e00\u4e2a <code>prototype<\/code> \u5c5e\u6027\u3002 \u5f53\u6211\u4eec\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u5b83\u65f6\uff0c\u6211\u4eec\u77e5\u9053\u6211\u4eec\u5c06\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u51fd\u6570\u7684\u539f\u578b\u3002 \u5982\u679c\u6211\u4eec\u8986\u76d6\u51fd\u6570\u7684\u539f\u578b\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u51b3\u5b9a\u5728\u67e5\u627e\u5931\u8d25\u65f6\u59d4\u6258\u7ed9\u54ea\u4e2a\u5bf9\u8c61\u3002 \u6240\u4ee5\u5728\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u8c03\u7528 <code>Object.create<\/code> \u65f6\u4f20\u5165\u7684\u5bf9\u8c61\u8986\u76d6 <code>Fn<\/code>\u7684\u539f\u578b\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a <code>objToDelegateTo<\/code>\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u53ea\u652f\u6301 <code>Object.create<\/code> \u7684\u5355\u4e2a\u53c2\u6570\u3002\u5b98\u65b9\u5b9e\u73b0\u8fd8\u652f\u6301\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u5141\u8bb8\u60a8\u5411\u521b\u5efa\u7684\u5bf9\u8c61\u6dfb\u52a0\u66f4\u591a\u5c5e\u6027\u3002<\/p>\n<h3><strong>\u7bad\u5934\u51fd\u6570<\/strong><\/h3>\n<p>\u7bad\u5934\u51fd\u6570\u6ca1\u6709\u81ea\u5df1\u7684 <code>this<\/code> \u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u7528\u4e8e\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u60a8\u5c1d\u8bd5\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u7bad\u5934\u51fd\u6570\uff0c\u5b83\u5c06\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>const Animal = () =&gt; {}\r\n \r\nconst leo = new Animal() \/\/ Error: Animal is not a constructor\r\n<\/code><\/pre>\n<p>\u53e6\u5916\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u4e0a\u9762\u8bc1\u660e\u4e86 <code>pseudoclassical pattern<\/code>(\u7ecf\u5178\u4f2a\u7c7b\u6a21\u5f0f) \u4e0d\u80fd\u4e0e\u7bad\u5934\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u6240\u4ee5\u7bad\u5934\u51fd\u6570\u4e5f\u6ca1\u6709 <code>prototype<\/code>(\u539f\u578b) \u5c5e\u6027\u3002<\/p>\n<p>JavaScript \u4ee3\u7801:<\/p>\n<pre><code class='language-javascript' lang='javascript'>const Animal = () =&gt; {}console.log(Animal.prototype) \/\/ undefined\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u82f1\u6587\u539f\u6587 | https:\/\/tylermcginnis.com\/beginners-guide-to-jav [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,105],"tags":[132],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2334"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2334"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2334\/revisions\/2854"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}