{"id":2125,"date":"2025-02-28T23:56:28","date_gmt":"2025-02-28T15:56:28","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2125"},"modified":"2025-02-28T23:56:28","modified_gmt":"2025-02-28T15:56:28","slug":"javascript-prototype%ef%bc%88%e5%8e%9f%e5%9e%8b%e5%af%b9%e8%b1%a1%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/28\/javascript-prototype%ef%bc%88%e5%8e%9f%e5%9e%8b%e5%af%b9%e8%b1%a1%ef%bc%89\/","title":{"rendered":"JavaScript prototype\uff08\u539f\u578b\u5bf9\u8c61\uff09"},"content":{"rendered":"\n<p>\u5b66\u4e60 <code>JavaScript prototype\uff08\u539f\u578b\u5bf9\u8c61\uff09<\/code> \u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u5e2e\u52a9\u4f60\u7406\u89e3 JavaScript \u4e2d\u5bf9\u8c61\u548c\u7ee7\u627f\u7684\u673a\u5236\u3002\u539f\u578b\u662f\u6bcf\u4e2a JavaScript \u5bf9\u8c61\u7684\u9690\u5f0f\u5c5e\u6027\uff0c\u5b83\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u8be5\u5bf9\u8c61\u7684\u5171\u4eab\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u539f\u578b\u94fe\u4e0e\u7ee7\u627f\u673a\u5236<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u578b\u94fe<\/h4>\n\n\n\n<p>\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a <code>__proto__<\/code> \u5c5e\u6027\uff0c\u6307\u5411\u5b83\u7684\u539f\u578b\u5bf9\u8c61\u3002\u539f\u578b\u5bf9\u8c61\u672c\u8eab\u4e5f\u6709\u81ea\u5df1\u7684 <code>__proto__<\/code>\uff0c\u76f4\u5230\u6700\u7ec8\u6307\u5411 <code>Object.prototype<\/code>\uff0c\u8fd9\u662f\u539f\u578b\u94fe\u7684\u6700\u9876\u5c42\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u68c0\u67e5\u5b83\u7684\u539f\u578b\u94fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = { name: \"Alice\" };\n\nconsole.log(obj.__proto__ === Object.prototype); \/\/ true\nconsole.log(Object.prototype.__proto__ === null); \/\/ true<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>obj<\/code> \u7684 <code>__proto__<\/code> \u6307\u5411 <code>Object.prototype<\/code>\u3002<\/li>\n\n\n\n<li><code>Object.prototype.__proto__<\/code> \u662f <code>null<\/code>\uff0c\u8868\u793a\u539f\u578b\u94fe\u7684\u7ec8\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u578b\u94fe\u7684\u7ee7\u627f<\/h4>\n\n\n\n<p>\u901a\u8fc7\u539f\u578b\u94fe\uff0c\u5b50\u5bf9\u8c61\u53ef\u4ee5\u7ee7\u627f\u7236\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Animal(name) {\n  this.name = name;\n}\n\nAnimal.prototype.speak = function() {\n  console.log(this.name + \" makes a sound.\");\n};\n\nfunction Dog(name) {\n  Animal.call(this, name);  \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n}\n\nDog.prototype = Object.create(Animal.prototype);  \/\/ \u7ee7\u627fAnimal\u7684\u65b9\u6cd5\nDog.prototype.constructor = Dog;  \/\/ \u4fee\u6b63\u6784\u9020\u51fd\u6570\u6307\u5411\n\nconst dog = new Dog('Buddy');\ndog.speak();  \/\/ \u8f93\u51fa\uff1aBuddy makes a sound.<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Dog.prototype = Object.create(Animal.prototype)<\/code> \u4f7f\u5f97 <code>Dog<\/code> \u7684\u5b9e\u4f8b\u80fd\u591f\u8bbf\u95ee <code>Animal<\/code> \u4e2d\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>Dog.prototype.constructor = Dog<\/code> \u7528\u6765\u4fee\u6b63 <code>Dog<\/code> \u6784\u9020\u51fd\u6570\u7684\u6307\u5411\uff0c\u56e0\u4e3a\u7ee7\u627f\u65f6\u4f1a\u4fee\u6539 <code>constructor<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Prototype \u5c5e\u6027\u4e0e\u5b9e\u4f8b\u5316<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><code>prototype<\/code> \u5c5e\u6027<\/h4>\n\n\n\n<p>\u6bcf\u4e2a\u51fd\u6570\uff08\u6784\u9020\u51fd\u6570\uff09\u90fd\u6709\u4e00\u4e2a <code>prototype<\/code> \u5c5e\u6027\uff0c\u5b83\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u8be5\u6784\u9020\u51fd\u6570\u5b9e\u4f8b\u7684\u5171\u4eab\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Person(name) {\n  this.name = name;\n}\n\nPerson.prototype.sayHello = function() {\n  console.log(\"Hello, \" + this.name);\n};\n\nconst person1 = new Person(\"Alice\");\nconst person2 = new Person(\"Bob\");\n\nperson1.sayHello();  \/\/ Hello, Alice\nperson2.sayHello();  \/\/ Hello, Bob\n\nconsole.log(person1.__proto__ === Person.prototype);  \/\/ true<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Person.prototype<\/code> \u5b58\u50a8\u7684\u662f <code>sayHello<\/code> \u65b9\u6cd5\uff0c\u5b83\u662f\u6240\u6709 <code>Person<\/code> \u5b9e\u4f8b\u5171\u4eab\u7684\u3002<\/li>\n\n\n\n<li><code>person1.__proto__<\/code> \u548c <code>person2.__proto__<\/code> \u90fd\u6307\u5411 <code>Person.prototype<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u5b9e\u4f8b\u65f6\u5982\u4f55\u8bbf\u95ee\u539f\u578b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function Car(make, model) {\n  this.make = make;\n  this.model = model;\n}\n\nCar.prototype.drive = function() {\n  console.log(this.make + \" \" + this.model + \" is driving.\");\n};\n\nconst car1 = new Car(\"Toyota\", \"Corolla\");\nconst car2 = new Car(\"Honda\", \"Civic\");\n\ncar1.drive();  \/\/ Toyota Corolla is driving.\ncar2.drive();  \/\/ Honda Civic is driving.<\/code><\/pre>\n\n\n\n<p><strong>\u91cd\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>drive<\/code> \u65b9\u6cd5\u662f\u901a\u8fc7 <code>Car.prototype<\/code> \u6765\u5171\u4eab\u7684\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u5b9e\u4f8b\u6709\u81ea\u5df1\u7684\u5c5e\u6027\uff08<code>make<\/code>, <code>model<\/code>\uff09\uff0c\u4f46\u662f\u5b83\u4eec\u5171\u4eab <code>drive<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u539f\u578b\u65b9\u6cd5\u548c\u7ee7\u627f<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u901a\u8fc7\u539f\u578b\u6dfb\u52a0\u65b9\u6cd5<\/h4>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u539f\u578b\u4e3a\u6240\u6709\u5b9e\u4f8b\u6dfb\u52a0\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4e3a <code>Person<\/code> \u7c7b\u6dfb\u52a0\u4e00\u4e2a <code>introduce<\/code> \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Person.prototype.introduce = function() {\n  console.log(\"Hi, I'm \" + this.name);\n};\n\nconst person3 = new Person(\"Charlie\");\nperson3.introduce();  \/\/ Hi, I'm Charlie<\/code><\/pre>\n\n\n\n<p><strong>\u539f\u578b\u94fe\u7684\u4f18\u52bf<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709 <code>Person<\/code> \u7c7b\u7684\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u8bbf\u95ee <code>introduce<\/code> \u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u62e5\u6709\u4e00\u4e2a\u526f\u672c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ee7\u627f\u7236\u7c7b\u539f\u578b\u65b9\u6cd5<\/h4>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>Object.create<\/code> \u7ee7\u627f\u7236\u7c7b\u7684\u539f\u578b\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Animal(name) {\n  this.name = name;\n}\n\nAnimal.prototype.speak = function() {\n  console.log(this.name + \" makes a sound.\");\n};\n\nfunction Cat(name) {\n  Animal.call(this, name);  \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n}\n\nCat.prototype = Object.create(Animal.prototype);  \/\/ \u7ee7\u627f\u7236\u7c7b\u539f\u578b\u65b9\u6cd5\nCat.prototype.constructor = Cat;  \/\/ \u4fee\u6b63\u6784\u9020\u51fd\u6570\n\nconst cat1 = new Cat(\"Whiskers\");\ncat1.speak();  \/\/ Whiskers makes a sound.<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Object.create(Animal.prototype)<\/code> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u4f5c\u4e3a <code>Cat.prototype<\/code>\uff0c\u5b83\u7ee7\u627f\u4e86 <code>Animal.prototype<\/code> \u4e0a\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>Cat.prototype.constructor = Cat<\/code> \u7528\u4e8e\u786e\u4fdd <code>Cat<\/code> \u5b9e\u4f8b\u7684\u6784\u9020\u51fd\u6570\u662f <code>Cat<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4fee\u6539\u539f\u578b\u94fe<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4fee\u6539\u5bf9\u8c61\u7684\u539f\u578b\u94fe\uff0c\u7ed9\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u65b9\u6cd5\u6216\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const person4 = new Person(\"David\");\n\nperson4.__proto__.sayGoodbye = function() {\n  console.log(this.name + \" says goodbye.\");\n};\n\nperson4.sayGoodbye();  \/\/ David says goodbye.<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u79cd\u4fee\u6539\u4f1a\u5f71\u54cd\u6240\u6709\u7ee7\u627f\u8be5\u539f\u578b\u94fe\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5b9e\u8df5\u6848\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u52a8\u7269\u7c7b\u548c\u5b50\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u52a8\u7269\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e0d\u540c\u7c7b\u578b\u7684\u52a8\u7269\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Animal(name) {\n  this.name = name;\n}\n\nAnimal.prototype.speak = function() {\n  console.log(this.name + \" makes a sound.\");\n};\n\nfunction Dog(name) {\n  Animal.call(this, name);  \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n}\n\nDog.prototype = Object.create(Animal.prototype);\nDog.prototype.constructor = Dog;\n\nDog.prototype.bark = function() {\n  console.log(this.name + \" barks.\");\n};\n\nfunction Cat(name) {\n  Animal.call(this, name);  \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n}\n\nCat.prototype = Object.create(Animal.prototype);\nCat.prototype.constructor = Cat;\n\nCat.prototype.meow = function() {\n  console.log(this.name + \" meows.\");\n};\n\n\/\/ \u521b\u5efa\u5b9e\u4f8b\nconst dog = new Dog(\"Buddy\");\nconst cat = new Cat(\"Whiskers\");\n\ndog.speak();  \/\/ Buddy makes a sound.\ndog.bark();   \/\/ Buddy barks.\n\ncat.speak();  \/\/ Whiskers makes a sound.\ncat.meow();   \/\/ Whiskers meows.<\/code><\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Dog<\/code> \u548c <code>Cat<\/code> \u7ee7\u627f\u4e86 <code>Animal<\/code> \u7684 <code>speak<\/code> \u65b9\u6cd5\uff0c\u4f46\u5404\u81ea\u6709\u81ea\u5df1\u72ec\u7279\u7684\u65b9\u6cd5\uff08<code>bark<\/code> \u548c <code>meow<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e86\u7b80\u5355\u7684\u7ee7\u627f\u5173\u7cfb\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60 JavaScript prototype\uff08\u539f\u578b\u5bf9\u8c61\uff09 \u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u5e2e\u52a9\u4f60\u7406\u89e3 JavaScri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2125","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2125"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2125\/revisions"}],"predecessor-version":[{"id":2126,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2125\/revisions\/2126"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}