{"id":20647,"date":"2026-04-16T13:44:39","date_gmt":"2026-04-16T05:44:39","guid":{"rendered":"https:\/\/92it.top\/?p=20647"},"modified":"2026-04-16T13:44:39","modified_gmt":"2026-04-16T05:44:39","slug":"javascript-%e5%8e%9f%e5%9e%8b%ef%bc%88prototype%ef%bc%89%e9%80%9a%e4%bf%97%e8%ae%b2%e8%a7%a3-%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=20647","title":{"rendered":"JavaScript \u539f\u578b\uff08prototype\uff09\u901a\u4fd7\u8bb2\u89e3 \u63a8\u8350"},"content":{"rendered":"\n<p><strong>\u524d\u8a00\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u539f\u578b\u5c31\u662f JS \u7ed9\u5bf9\u8c61 \u201c\u5171\u4eab\u5c5e\u6027\u548c\u65b9\u6cd5\u201d \u7684\u673a\u5236\uff0c\u4e0d\u7528\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u91cd\u590d\u5199\u4e00\u904d\u4ee3\u7801\uff0c\u7701\u7a7a\u95f4\u3001\u7701\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u628a\u539f\u578b\u7406\u89e3\u6210\uff1a<strong>\u4e00\u4e2a\u516c\u5171\u7684 \u201c\u5de5\u5177\u7bb1\u201d<\/strong>\uff0c\u6240\u6709\u7528\u540c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u90fd\u80fd\u5171\u7528\u8fd9\u4e2a\u5de5\u5177\u7bb1\u91cc\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u5148\u641e\u61c2 3 \u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u2460 prototype \u2014\u2014 \u6784\u9020\u51fd\u6570\u7684 \u201c\u516c\u5171\u5de5\u5177\u7bb1\u201d\n<ul>\n<li>\u53ea\u6709\u51fd\u6570\u624d\u6709 prototype\uff0c\u5b83\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7528\u6765\u5b58\u653e\u5171\u4eab\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2461 <strong>proto<\/strong> \u2014\u2014 \u5bf9\u8c61\u7684 \u201c\u5de5\u5177\u7bb1\u94a5\u5319\u201d\n<ul>\n<li>\u6240\u6709\u5bf9\u8c61\u90fd\u6709 <strong>proto<\/strong>\uff0c\u5b83\u6307\u5411\u81ea\u5df1\u7684\u6784\u9020\u51fd\u6570\u7684 prototype\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2462 \u539f\u578b\u94fe\n<ul>\n<li>\u5bf9\u8c61\u627e\u4e0d\u5230\u5c5e\u6027 \/ \u65b9\u6cd5 \u2192 \u53bb <strong>proto<\/strong> \u627e \u2192 \u8fd8\u627e\u4e0d\u5230 \u2192 \u7ee7\u7eed\u5f80\u4e0a\u627e \u2192 \u76f4\u5230 null<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e00\u6761\u94fe\u5c31\u662f\u539f\u578b\u94fe\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u7528\u751f\u6d3b\u4f8b\u5b50\u79d2\u61c2\u539f\u578b<\/strong><\/p>\n\n\n\n<p>\u573a\u666f\uff1a\u9020 100 \u4e2a\u5b66\u751f<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\uff1a\u540d\u5b57\u3001\u5b66\u53f7\uff08<strong>\u81ea\u5df1\u72ec\u6709<\/strong>\uff09<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u5b66\u751f\u90fd\u4f1a\uff1a\u5b66\u4e60\u3001\u5403\u996d\u3001\u7761\u89c9\uff08<strong>\u5927\u5bb6\u5171\u7528<\/strong>\uff09<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u4f7f\u7528\u539f\u578b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Student(name, id) {\n  this.name = name\n  this.id = id\n  \/\/ \u6bcf\u4e2a\u5b66\u751f\u90fd\u521b\u5efa\u4e00\u4e2a study \u65b9\u6cd5\uff01100\u4e2a\u5b66\u751f\u5c31\u6709100\u4e2a\u65b9\u6cd5\uff0c\u6d6a\u8d39\u5185\u5b58\n  this.study = function() {\n    console.log('\u5b66\u4e60')\n  }\n}<\/pre>\n\n\n\n<p>\u7528\u539f\u578b\u4f18\u5316\uff08\u5171\u4eab\u65b9\u6cd5\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Student(name, id) {\n  this.name = name  \/\/ \u81ea\u5df1\u7684\n  this.id = id      \/\/ \u81ea\u5df1\u7684\n}\n\n\/\/ \u653e\u5728 prototype \u91cc = \u516c\u5171\u5de5\u5177\u7bb1\nStudent.prototype.study = function() {\n  console.log(this.name + '\u5728\u5b66\u4e60')\n}\nStudent.prototype.eat = function() {\n  console.log('\u5403\u996d')\n}<\/pre>\n\n\n\n<p>\u73b0\u5728\uff1a<\/p>\n\n\n\n<ul>\n<li>100 \u4e2a\u5b66\u751f\u5bf9\u8c61\uff0c<strong>\u53ea\u5b58 1 \u4efd study\u3001eat \u65b9\u6cd5<\/strong><\/li>\n\n\n\n<li>\u6bcf\u4e2a\u5b66\u751f\u90fd\u80fd\u8c03\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684 <code>__proto__<\/code> \u6307\u5411\u8fd9\u4e2a\u516c\u5171\u5de5\u5177\u7bb1<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u5bf9\u8c61\u600e\u4e48\u7528\u539f\u578b\uff1f<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ 1. \u6784\u9020\u51fd\u6570\nfunction Student(name) {\n  this.name = name\n}\n\n\/\/ 2. \u7ed9\u539f\u578b\u52a0\u5171\u4eab\u65b9\u6cd5\nStudent.prototype.sayHi = function() {\n  console.log('\u6211\u662f ' + this.name)\n}\n\n\/\/ 3. \u521b\u5efa\u5bf9\u8c61\nlet s1 = new Student('\u5c0f\u660e')\nlet s2 = new Student('\u5c0f\u7ea2')\n\n\/\/ 4. \u8c03\u7528\u65b9\u6cd5\uff08\u65b9\u6cd5\u5728\u539f\u578b\u4e0a\uff09\ns1.sayHi()  \/\/ \u6211\u662f \u5c0f\u660e\ns2.sayHi()  \/\/ \u6211\u662f \u5c0f\u7ea2<\/pre>\n\n\n\n<p>\u8bbe\u8ba1\u521d\u8877\uff1a<strong>\u5206\u79bb\u300c\u5b58\u50a8\u300d\u548c\u300c\u67e5\u627e\u300d<\/strong><\/p>\n\n\n\n<ul>\n<li>prototype = \u516c\u5171\u4ed3\u5e93\uff08\u53ea\u8bfb \/ \u6302\u8f7d\u533a\uff09\n<ul>\n<li>\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u53ea\u9700\u8981\u4e00\u4efd\u539f\u578b\u4ed3\u5e93<\/li>\n\n\n\n<li>\u6240\u6709\u5b9e\u4f8b\u5171\u7528\u8fd9\u4e00\u4e2a\u4ed3\u5e93\uff0c\u6781\u81f4\u8282\u7701\u5185\u5b58<\/li>\n\n\n\n<li>\u5f00\u53d1\u8005\u53ea\u9700\u8981\u7ed9\u300c\u51fd\u6570\u7684 prototype\u300d\u52a0\u65b9\u6cd5\uff0c\u6240\u6709\u5b9e\u4f8b\u81ea\u52a8\u80fd\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709<code>prototype<\/code>\uff1a\u6bcf <code>new<\/code> \u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c31\u8981\u590d\u5236\u4e00\u904d\u6240\u6709\u65b9\u6cd5\uff0c\u521b\u5efa 1000 \u4e2a\u5b9e\u4f8b\u5c31\u5197\u4f59 1000 \u4efd\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u539f\u578b\u94fe\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<p>\u4e00\u5c42\u4e00\u5c42\u5f80\u4e0a\u627e\u5c5e\u6027 \/ \u65b9\u6cd5\uff0c\u5c31\u662f<strong>\u539f\u578b\u94fe<\/strong>\u3002<\/p>\n\n\n\n<p>\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">s1.toString()\n<\/pre>\n\n\n\n<p>\u67e5\u627e\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">s1 \u81ea\u5df1\u627e toString \u2192 \u6ca1\u6709\n\u53bb s1.__proto__\uff08Student.prototype\uff09\u2192 \u6ca1\u6709\n\u53bb Student.prototype.__proto__\uff08Object.prototype\uff09\u2192 \u627e\u5230\u4e86\uff01\n\u6267\u884c<\/pre>\n\n\n\n<p>\u8fd9\u5c31\u662f<strong>\u539f\u578b\u94fe<\/strong>\u3002<\/p>\n\n\n\n<p>\u6700\u7ec8\u539f\u578b\u94fe\u9876\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Object.prototype.__proto__ \u2192 null<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u6700\u91cd\u8981\u7684 3 \u6761\u89c4\u5219<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u51fd\u6570\u624d\u6709 prototype<\/strong>\uff0c\u5bf9\u8c61\u6ca1\u6709 <\/li>\n\n\n\n<li><strong>\u5bf9\u8c61\u90fd\u6709 proto<\/strong>\uff0c\u6307\u5411\u521b\u5efa\u5b83\u7684\u6784\u9020\u51fd\u6570\u7684\u539f\u578b <\/li>\n\n\n\n<li>\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5 \/ \u5c5e\u6027\u65f6\uff1a<strong>\u5148\u627e\u81ea\u5df1 \u2192 \u627e\u4e0d\u5230\u53bb\u539f\u578b\u627e \u2192 \u518d\u627e\u4e0d\u5230\u6cbf\u539f\u578b\u94fe\u5f80\u4e0a\u627e<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u6784\u9020\u51fd\u6570 Student\n       \u2193 (prototype)\nStudent.prototype  \u3010\u516c\u5171\u5de5\u5177\u7bb1\u3011\n       \u2191 (__proto__)\n    \u5bf9\u8c61 s1\u3001s2<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u9ed8\u8ba4\u539f\u578b\u5bf9\u8c61\u91cc\u9762\u957f\u4ec0\u4e48\u6837<\/strong>\uff1f<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>prototype<\/code> \u5c31\u662f\u4e00\u4e2a\u666e\u901a JS \u5bf9\u8c61\uff08\u952e\u503c\u5bf9\u5b57\u5178\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Person(){}\n<\/pre>\n\n\n\n<p>JS \u4f1a\u81ea\u52a8\u7ed9 Person \u7ed1\u5b9a\u4e00\u4e2a\u300c\u5929\u751f\u81ea\u5e26\u7684\u7a7a\u539f\u578b\u5bf9\u8c61\u300d<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Person.prototype = \u3010\u6d4f\u89c8\u5668\u81ea\u52a8\u521b\u5efa\u7684\u9ed8\u8ba4\u539f\u578b\u5bf9\u8c61\u3011\n<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u9ed8\u8ba4\u539f\u578b\u5bf9\u8c61\u91cc\u9762\u957f\u4ec0\u4e48\u6837\uff1f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Person() {}\n\nconsole.log(Person.prototype);<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  constructor: Person,   \/\/ \u5929\u751f\u81ea\u5e26\uff0c\u5173\u952e\u6807\u8bb0\n  __proto__: Object.prototype\n}<\/pre>\n\n\n\n<p>\u5e72\u5e72\u51c0\u51c0\uff0c\u6ca1\u6709\u4efb\u4f55\u591a\u4f59\u65b9\u6cd5\u3002JS \u5f15\u64ce\u81ea\u52a8\u521b\u5efa\u7684\u3001\u4ee5\u5f53\u524d\u51fd\u6570\u4e3a constructor \u7684\u7eaf\u7a7a\u539f\u578b\u5bf9\u8c61<\/p>\n\n\n\n<p><code>Person.prototype<\/code><strong>\u672c\u8d28\u5c31\u662f Object \u5bf9\u8c61<\/strong>\uff0c\u53ea\u662f\u5f15\u64ce\u63d0\u524d\u5e2e\u4f60\u5efa\u597d\u3001\u9884\u88c5\u4e86 <code>constructor<\/code>\u3002<\/p>\n\n\n\n<p>\u5728 JS \u91cc\uff1a<strong>\u6240\u6709\u666e\u901a\u5bf9\u8c61\uff0c\u672c\u8d28\u5c31\u662f\u4e00\u4e2a \u952e\u503c\u5bf9\u5b57\u5178<\/strong><\/p>\n\n\n\n<p>\u5c31\u8ddf Java \u7684 <code>Map&lt;String, Object><\/code> \u4e00\u6a21\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ JS \u666e\u901a\u5bf9\u8c61 = \u7eaf\u5b57\u5178\nlet obj = {\n  name: \"\u5f20\u4e09\",\n  age: 18\n};<\/pre>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u968f\u65f6\uff1a<\/p>\n\n\n\n<ul>\n<li>\u52a0 key<\/li>\n\n\n\n<li>\u5220 key<\/li>\n\n\n\n<li>\u4fee\u6539 key<\/li>\n\n\n\n<li>key \u7684\u503c\u53ef\u4ee5\u662f\uff1a\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001<strong>\u51fd\u6570\u3001\u6570\u7ec4\u3001\u5bf9\u8c61<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Student(){}\n<\/pre>\n\n\n\n<p><code>Student.prototype<\/code> \u4e0d\u662f\u7279\u6b8a\u4e1c\u897f<\/p>\n\n\n\n<p>\u5b83 = \u5f15\u64ce\u81ea\u52a8\u5e2e\u4f60\u521b\u5efa\u7684\u4e00\u4e2a\u300c\u666e\u901a\u5b57\u5178\u5bf9\u8c61\u300d<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5f15\u64ce\u81ea\u52a8\u65b0\u5efa\u4e00\u4e2a\u666e\u901a\u5b57\u5178\u5bf9\u8c61\nStudent.prototype = {\n  constructor: Student   \/\/ \u5c31\u8fd9\u4e00\u4e2akey\n}<\/pre>\n\n\n\n<p><strong>\u5b83\u5c31\u662f\u4e00\u4e2a\u7eaf\u7eaf\u7684\u666e\u901a\u5bf9\u8c61\u3001\u7eaf\u5b57\u5178\uff0c\u6ca1\u6709\u4efb\u4f55\u7279\u6743\u3002<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n\/\/ \u7ed9\u5b57\u5178\u52a0\u4e00\u4e2a key: sayHi\uff0c\u503c\u662f\u4e00\u4e2a\u51fd\u6570\nStudent.prototype.sayHi = function(){\n  console.log(\"\u4f60\u597d\")\n}\n\n\u7b49\u4ef7\u4e8e\uff1a\n\/\/ 1. \u5148\u62ff\u5230\u8fd9\u4e2a\u5b57\u5178\nlet \u5b57\u5178 = Student.prototype;\n\n\/\/ 2. \u7ed9\u5b57\u5178\u65b0\u589e\u4e00\u4e2a\u952e\u503c\u5bf9\n\u5b57\u5178.sayHi = \u51fd\u6570;<\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u7ed3\u8bba\uff08\u5148\u80cc\u4e0b\u6765\uff09<\/strong><\/p>\n\n\n\n<ol>\n<li>JS \u6240\u6709\u666e\u901a\u51fd\u6570\uff0c\u5929\u751f\u81ea\u5e26\u4e00\u4e2a\u5c5e\u6027\uff1a<code>prototype<\/code><\/li>\n\n\n\n<li><code>prototype<\/code> \u5c31\u662f\u4e00\u4e2a\u666e\u901a JS \u5bf9\u8c61\uff08\u952e\u503c\u5bf9\u5b57\u5178\uff09<\/li>\n\n\n\n<li><code>new \u51fa\u6765\u7684\u5b9e\u4f8b\uff0c\u5929\u751f\u4f1a\u6302\u9760\u5230\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\uff0c\u7528\u6765\u5171\u4eab\u5c5e\u6027\/\u65b9\u6cd5<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u6240\u4ee5\uff1a<code>Person.prototype<\/code> = \u4e00\u4e2a\u63d0\u524d\u51c6\u5907\u597d\u7684\u300c\u516c\u5171\u4ed3\u5e93\u5bf9\u8c61\u300d<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>Java\u53ef\u4ee5\u52a8\u6001\u7ed9\u4efb\u4f55\u5bf9\u8c61\u52a0\u65b9\u6cd5\u5417\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7b54\u6848\uff1a\u5b8c\u5168\u4e0d\u53ef\u4ee5 \u274c<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f <strong>Java \u9759\u6001\u8bed\u8a00<\/strong> \u548c <strong>JS \u52a8\u6001\u8bed\u8a00<\/strong> \u6700\u6838\u5fc3\u7684\u5206\u6c34\u5cad\u3002<\/p>\n\n\n\n<p><strong>\ud83d\udd39JavaScript \u73a9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ 1. \u666e\u901a\u5bf9\u8c61\nlet obj = {}\n\n\/\/ 2. \u8fd0\u884c\u65f6\u968f\u4fbf\u52a0\u65b9\u6cd5\uff0c\u968f\u65f6\u968f\u5730\nobj.sayHi = function() {\n  console.log(\"\u4f60\u597d\")\n}\nobj.sayHi()<\/pre>\n\n\n\n<p>\u2705 JS \u4efb\u610f\u5bf9\u8c61\uff0c\u8fd0\u884c\u65f6\u968f\u610f\u589e\u5220\u5c5e\u6027\u3001\u65b9\u6cd5\uff0c\u5929\u751f\u652f\u6301\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39Java \u7edd\u5bf9\u4e0d\u884c<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class User {\n  \/\/ \u53ea\u80fd\u5728\u8fd9\u91cc\u5199\u6b7b\u65b9\u6cd5\n  void test(){}\n}\n\npublic class Demo {\n    public static void main(String[] args) {\n        User u = new User();\n        \n        \/\/ \u274c \u8bed\u6cd5\u62a5\u9519\uff01\u6839\u672c\u5199\u4e0d\u51fa\u6765\n        u.sayHi = \u65b9\u6cd5;\n    }\n}\n<\/pre>\n\n\n\n<p>Java \u5bf9\u8c61\u4e00\u65e6 new \u51fa\u6765\uff0c\u7ed3\u6784\u5b8c\u5168\u56fa\u5b9a\uff1a<\/p>\n\n\n\n<ul>\n<li>\u4e0d\u80fd\u65b0\u589e\u53d8\u91cf<\/li>\n\n\n\n<li>\u4e0d\u80fd\u65b0\u589e\u65b9\u6cd5<\/li>\n\n\n\n<li>\u4e0d\u80fd\u5220\u9664\u539f\u6709\u5c5e\u6027<\/li>\n\n\n\n<li>\u7c7b\u957f\u4ec0\u4e48\u6837\uff0c\u5bf9\u8c61\u4e00\u8f88\u5b50\u5c31\u957f\u4ec0\u4e48\u6837<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u5e95\u5c42\u672c\u8d28\u533a\u522b<\/strong><\/p>\n\n\n\n<ul>\n<li>Java\uff1a\u57fa\u4e8e\u7c7b \/ \u9759\u6001\u7ed1\u5b9a<\/li>\n\n\n\n<li>\u4e00\u5207\u8bbe\u8ba1\u5728 \u7f16\u8bd1\u671f \u5c31\u5b9a\u6b7b<\/li>\n\n\n\n<li>\u65b9\u6cd5\u5c5e\u4e8e\u300c\u7c7b\u300d\uff0c\u4e0d\u5c5e\u4e8e\u5355\u4e2a\u5bf9\u8c61<\/li>\n\n\n\n<li>\u5bf9\u8c61\u53ea\u662f\u300c\u6570\u636e\u8f7d\u4f53\u300d\uff0c\u53ea\u5b58\u6210\u5458\u53d8\u91cf<\/li>\n\n\n\n<li>\u65b9\u6cd5\u5168\u90e8\u5728 \u7c7b\u6a21\u677f \u91cc\uff0c\u5168\u5c40\u5171\u4eab\uff0c\u4e0d\u80fd\u4e34\u65f6\u52a0<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Java \u4e16\u754c\u89c2\uff1a\n\u5148\u6709 \u7c7b\uff08\u6a21\u677f\uff09 \u2192 \u518d new \u5bf9\u8c61\n\u6a21\u677f\u4e0d\u6539\uff0c\u5bf9\u8c61\u6c38\u8fdc\u4e0d\u80fd\u52a0\u65b0\u529f\u80fd<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39<\/strong>JS\uff1a<strong>\u57fa\u4e8e\u5bf9\u8c61 \/ \u52a8\u6001\u7ed1\u5b9a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u4e00\u5207\u90fd\u662f\u5bf9\u8c61<\/li>\n\n\n\n<li>\u65b9\u6cd5\u5c31\u662f\u4e00\u4e2a\u53d8\u91cf\u503c\uff0c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11<\/li>\n\n\n\n<li>obj.xxx = \u51fd\u6570 \u672c\u8d28\u5c31\u662f \u7ed9\u5bf9\u8c61\u52a0\u4e00\u4e2a\u952e\u503c\u5bf9<\/li>\n\n\n\n<li>prototype \u53ea\u662f\u4e00\u4e2a\u7528\u6765\u5171\u4eab\u65b9\u6cd5\u7684\u666e\u901a\u5bf9\u8c61<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">JS \u4e16\u754c\u89c2\uff1a\n\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u300c\u5b57\u5178\u300d\uff0ckey \u968f\u4fbf\u52a0\u3001\u968f\u4fbf\u6539\u3001\u968f\u4fbf\u5220<\/pre>\n\n\n\n<p>\u4e3a\u4ec0\u4e48 JS \u53ef\u4ee5\u8fd9\u6837\u5199\uff1f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Student.prototype.sayHi = function(){}\n<\/pre>\n\n\n\n<p>\u56e0\u4e3a\uff1a<\/p>\n\n\n\n<ol>\n<li><code>Student.prototype<\/code> \u5c31\u662f\u4e00\u4e2a<strong>\u666e\u901a\u5b57\u5178\u5bf9\u8c61<\/strong><\/li>\n\n\n\n<li><code>sayHi<\/code> \u53ea\u662f\u4e00\u4e2a key<\/li>\n\n\n\n<li>\u53f3\u8fb9\u51fd\u6570\u53ea\u662f\u4e00\u4e2a<strong>\u503c<\/strong><\/li>\n<\/ol>\n\n\n\n<p>\u6362\u6210 Java \u903b\u8f91\u7406\u89e3\uff1a<\/p>\n\n\n\n<p>Java \u6ca1\u6709\u300c\u7ed9\u5bf9\u8c61\u4e34\u65f6\u52a0\u65b9\u6cd5\u300d\u7684\u8bed\u6cd5\uff0c<\/p>\n\n\n\n<p><strong>\u6240\u6709\u65b9\u6cd5\u5fc5\u987b\u63d0\u524d\u5199\u5728\u7c7b\u91cc\u9762<\/strong>\uff0c\u4e0d\u80fd\u8fd0\u884c\u65f6\u4e34\u65f6\u6302\u8f7d\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>JS\u4e2d\uff0c\u4e3a\u4ec0\u4e48\u5bf9\u8c61\u4f1a\u53bb\u627e\u8fd9\u4e2aprototype\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\ud83d\udd39\u5148\u5b9a 3 \u4e2a\u6838\u5fc3\u7ed3\u8bba<\/strong><\/p>\n\n\n\n<ul>\n<li>JS \u6240\u6709\u666e\u901a\u51fd\u6570\uff0c\u5929\u751f\u81ea\u5e26\u4e00\u4e2a\u5c5e\u6027\uff1aprototype<\/li>\n\n\n\n<li>prototype \u5c31\u662f\u4e00\u4e2a\u666e\u901a JS \u5bf9\u8c61\uff08\u952e\u503c\u5bf9\u5b57\u5178\uff09<\/li>\n\n\n\n<li>new \u51fa\u6765\u7684\u5b9e\u4f8b\uff0c\u5929\u751f\u4f1a\u6302\u9760\u5230\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\uff0c\u7528\u6765\u5171\u4eab\u5c5e\u6027\/\u65b9\u6cd5<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u51fd\u6570\u7684 prototype \u662f\u4ec0\u4e48<\/strong><\/p>\n\n\n\n<p>\u4f60\u5199\u4e00\u4e2a\u666e\u901a\u51fd\u6570<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Person() { }\n<\/pre>\n\n\n\n<p>JS \u5f15\u64ce\u81ea\u52a8\u5077\u5077\u505a\u4e86\u4e00\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5f15\u64ce\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a \u7eaf\u666e\u901a\u5bf9\u8c61\nPerson.prototype = {\n  constructor: Person   \/\/ \u53ea\u5b58\u8fd9\u4e00\u4e2a\u5c5e\u6027\n}<\/pre>\n\n\n\n<p>\u6240\u4ee5\uff1a<code>Person.prototype<\/code> = \u4e00\u4e2a\u63d0\u524d\u51c6\u5907\u597d\u7684\u300c\u516c\u5171\u4ed3\u5e93\u5bf9\u8c61\u300d<\/p>\n\n\n\n<p>\u5b83\u5c31\u662f\u4e2a\u666e\u901a\u5bf9\u8c61\uff0c\u548c <code>{}<\/code> \u6ca1\u6709\u672c\u8d28\u533a\u522b\uff0c\u4f60\u53ef\u4ee5\u968f\u610f\u52a0\u5c5e\u6027\u3001\u52a0\u51fd\u6570\u3001\u6539\u3001\u5220\u3001\u8986\u76d6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5f80\u516c\u5171\u4ed3\u5e93\u91cc\u653e\u65b9\u6cd5\nPerson.prototype.say = function(){}\nPerson.prototype.age = 20<\/pre>\n\n\n\n<p>\u5bf9\u6bd4 Java\uff1a<br>Java \u662f\u300c\u7c7b\u91cc\u9762\u5199\u6b7b\u65b9\u6cd5\u300d<br>JS \u662f\u300c\u51fd\u6570\u81ea\u5e26\u4e00\u4e2a\u516c\u5171\u4ed3\u5e93\uff0c\u65b9\u6cd5\u5168\u90e8\u6302\u5728\u4ed3\u5e93\u91cc\u300d<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u4e3a\u4ec0\u4e48 new \u51fa\u6765\u7684\u5bf9\u8c61\uff0c\u4f1a\u53bb\u627e\u8fd9\u4e2a prototype\uff1f<\/strong><\/p>\n\n\n\n<p>\u5f53\u4f60\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const p = new Person()\n<\/pre>\n\n\n\n<p><code>new<\/code> \u505a\u4e86 4 \u4ef6\u4e8b\uff0c\u5176\u4e2d\u6700\u5173\u952e\u7684\u4e00\u6b65\uff1a<\/p>\n\n\n\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u65b0\u5bf9\u8c61<\/li>\n\n\n\n<li>\u628a\u65b0\u5bf9\u8c61\u7684 <code>__proto__<\/code>\uff0c\u5f3a\u884c\u6307\u5411 <code>Person.prototype<\/code><\/li>\n\n\n\n<li>\u6267\u884c\u6784\u9020\u51fd\u6570\u4ee3\u7801<\/li>\n\n\n\n<li>\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61<\/li>\n<\/ol>\n\n\n\n<p>\u5bf9\u8c61\u4e3a\u4ec0\u4e48\u8981\u4e3b\u52a8\u53bb\u627e prototype\uff1f<br>\u6838\u5fc3\uff1aJS \u7684\u300c\u5c5e\u6027\u67e5\u627e\u89c4\u5219\u300d<strong>\uff08\u8bed\u8a00\u5e95\u5c42\u6b7b\u89c4\u5219\uff09<\/strong><\/p>\n\n\n\n<p>\u5f53\u4f60\u5199\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">p.say()<\/pre>\n\n\n\n<p>JS \u4f1a\u6309\u56fa\u5b9a\u987a\u5e8f\u627e\uff0c\u4e00\u6b65\u90fd\u4e0d\u4f1a\u4e71\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5148\u5728\u81ea\u5df1\u8eab\u4e0a\u627e\uff1ap \u81ea\u8eab\u6709\u6ca1\u6709 say\uff1f<\/li>\n\n\n\n<li>\u627e\u4e0d\u5230 \u2192 \u8d70\u9690\u85cf\u5f15\u7ebf <strong>proto<\/strong><\/li>\n\n\n\n<li>\u627e\u5230 Person.prototype \u2192 \u5728\u8fd9\u4e2a\u4ed3\u5e93\u91cc\u627e say<\/li>\n\n\n\n<li>\u627e\u5230\u5c31\u6267\u884c\uff1b\u627e\u4e0d\u5230\u7ee7\u7eed\u5f80\u4e0a\u627e\uff08\u539f\u578b\u94fe\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39JS \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u8bbe\u8ba1<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u628a\u65b9\u6cd5\u5199\u5728\u5b9e\u4f8b\u4e0a\uff1f<br>\u975e\u8981\u641e\u4e00\u4e2a prototype \u8ba9\u5bf9\u8c61\u7ed5\u4e00\u5708\u53bb\u627e\uff1f<\/p>\n\n\n\n<ul>\n<li>\u4e3a\u4e86<strong>\u7701\u5185\u5b58<\/strong>\uff08\u6700\u6838\u5fc3\u539f\u56e0\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4e0d\u7528 prototype\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Person(){\n  \/\/ \u6bcf new \u4e00\u4e2a\u4eba\uff0c\u5c31\u65b0\u5efa\u4e00\u4efd\u51fd\u6570\n  this.say = function(){}\n}\nconst p1 = new Person()\nconst p2 = new Person()\n\np1 \u6709\u4e00\u4efd say\np2 \u6709\u4e00\u4efd say\n1000 \u4e2a\u5b9e\u4f8b = 1000 \u4efd\u4e00\u6a21\u4e00\u6837\u7684\u51fd\u6570\uff0c\u5185\u5b58\u7206\u70b8<\/pre>\n\n\n\n<p>\u7528 prototype \u4e4b\u540e\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5168\u5c40\u5c31\u8fd9 1 \u4efd\nPerson.prototype.say = function(){}<\/pre>\n\n\n\n<p>\u6240\u6709\u5b9e\u4f8b\u5171\u7528<strong>\u540c\u4e00\u4e2a\u51fd\u6570<\/strong>\uff0c\u5b9e\u4f8b\u53ea\u5b58\u81ea\u5df1\u72ec\u6709\u7684\u6570\u636e\uff08name\/age\uff09\uff0c\u65b9\u6cd5\u5168\u5171\u4eab\u3002<\/p>\n\n\n\n<ul>\n<li>\u4e3a\u4e86<strong>\u52a8\u6001\u6269\u5c55<\/strong>\uff08Java \u505a\u4e0d\u5230\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u540e\u671f\u60f3\u7ed9\u6240\u6709\u5b9e\u4f8b\u7edf\u4e00\u52a0\u529f\u80fd\uff1f\u76f4\u63a5\u6539\u4ed3\u5e93\u5c31\u884c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4e00\u884c\u4ee3\u7801\uff0c\u6240\u6709 \u5df2\u521b\u5efa \/ \u672a\u521b\u5efa \u7684\u5b9e\u4f8b\u5168\u90e8\u751f\u6548\nPerson.prototype.run = function(){}<\/pre>\n\n\n\n<p>Java \u7c7b\u4e00\u65e6\u5b9a\u4e49\u5b8c\uff0c\u4e0d\u80fd\u968f\u4fbf\u52a0\u65b9\u6cd5\uff0cJS \u9760 prototype \u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>__proto__ \u4e3a\u4ec0\u4e48\u8981\u52a0\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5462\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u53cc\u4e0b\u5212\u7ebf <code>__xx__<\/code>\uff0c\u5728\u7f16\u7a0b\u8bed\u8a00\u91cc\uff0c\u6709\u4e00\u4e2a\u901a\u7528\u89c4\u77e9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u524d\u540e\u53cc\u4e0b\u5212\u7ebf = \u5185\u90e8\u9690\u85cf\u5c5e\u6027\u3001\u5f15\u64ce\u79c1\u6709\u3001\u5b98\u65b9\u5185\u7f6e\u3001\u4e0d\u5efa\u8bae\u5f00\u53d1\u8005\u968f\u4fbf\u78b0<br><\/pre>\n\n\n\n<p><code>__proto__<\/code> \u610f\u601d\u5c31\u662f\uff1a<\/p>\n\n\n\n<p><strong>\u8fd9\u662f JS \u5f15\u64ce\u5185\u90e8\u7528\u7684\u5e95\u5c42\u5c5e\u6027\uff0c\u4e0d\u5c5e\u4e8e\u5f00\u53d1\u8005\u6b63\u5e38\u4f7f\u7528\u7684 API\uff0c\u53ea\u662f\u88ab\u8feb\u66b4\u9732\u51fa\u6765\u7ed9\u4f60\u770b\u7684\u3002<\/strong><\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48JS\u51fd\u6570\u4e0a\u80fd\u5b9a\u4e49\u5bf9\u8c61\u5462\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6838\u5fc3\u7b54\u6848\u4e00\u53e5\u8bdd\uff1a<strong>\u56e0\u4e3a\u5728 JavaScript \u91cc\uff0c\u51fd\u6570\u672c\u8eab\u5c31\u662f\u5bf9\u8c61\u3002<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8bf4 \u201c\u51fd\u6570\u5c31\u662f\u5bf9\u8c61\u201d\uff1f<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u6210\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5728 Java \u91cc\uff0c\u51fd\u6570\u548c\u5bf9\u8c61\u662f\u5b8c\u5168\u5206\u5f00\u7684\uff0c\u51fd\u6570\u4e0d\u80fd\u5f53\u5bf9\u8c61\u7528\uff1b<\/li>\n\n\n\n<li>\u4f46\u5728 JS \u91cc\uff0c<strong>\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5bf9\u8c61<\/strong>\uff0c\u5929\u751f\u5c31\u53ef\u4ee5\u62e5\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u5199\u7684\u8fd9\u53e5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Person.prototype = { \/* ... *\/ }\n<\/pre>\n\n\n\n<p>\u672c\u8d28\u5c31\u662f\uff1a\u7ed9\u51fd\u6570\u8fd9\u4e2a \u201c\u5bf9\u8c61\u201d\uff0c\u6dfb\u52a0 \/ \u4fee\u6539\u4e00\u4e2a\u53eb <code>prototype<\/code> \u7684\u5c5e\u6027\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39\u600e\u4e48\u8bc1\u660e \u201c\u51fd\u6570\u662f\u5bf9\u8c61\u201d\uff1f<\/strong><\/p>\n\n\n\n<p>\u770b\u8fd9\u6bb5\u4ee3\u7801\u5c31\u61c2\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Person() {}\n\n\/\/ \u7ed9\u51fd\u6570\u672c\u8eab\u52a0\u4e00\u4e2a\u5c5e\u6027\uff0c\u5b8c\u5168\u5408\u6cd5\uff01\nPerson.name = \"\u4eba\u7c7b\u6784\u9020\u5668\";\nPerson.age = 100;\nPerson.sayHi = function() {\n  console.log(\"\u6211\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4f46\u6211\u4e5f\u80fd\u6709\u81ea\u5df1\u7684\u65b9\u6cd5\uff01\");\n};\n\nconsole.log(Person.name);   \/\/ \"\u4eba\u7c7b\u6784\u9020\u5668\"\nconsole.log(Person.age);    \/\/ 100\nPerson.sayHi();             \/\/ \u6b63\u5e38\u6267\u884c<\/pre>\n\n\n\n<p>\u4f60\u4f1a\u53d1\u73b0\uff0c\u51fd\u6570\u53ef\u4ee5\u50cf\u666e\u901a\u5bf9\u8c61\u4e00\u6837\uff0c\u88ab\u52a0\u5c5e\u6027\u3001\u52a0\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u672c\u6765\u5c31\u662f\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p><code>prototype<\/code> \u662f\u4ec0\u4e48\uff1a\u5b83\u5c31\u662f<strong>\u6240\u6709\u666e\u901a\u51fd\u6570\u5929\u751f\u81ea\u5e26\u7684\u4e00\u4e2a\u5c5e\u6027<\/strong>\uff0c\u548c\u4f60\u4e0a\u9762\u7ed9\u51fd\u6570\u52a0\u7684 <code>name<\/code>\u3001<code>age<\/code> \u6ca1\u533a\u522b\uff0c\u53ea\u662f JS \u5f15\u64ce\u9ed8\u8ba4\u5e2e\u4f60\u52a0\u4e0a\u4e86\u800c\u5df2\u3002<\/p>\n\n\n\n<ul>\n<li>\u8fd9\u4e2a\u5c5e\u6027\u7684\u503c\uff0c\u9ed8\u8ba4\u662f\u4e00\u4e2a\u7a7a\u5bf9\u8c61<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u4fee\u6539\u3001\u8986\u76d6\u5b83\uff0c\u5c31\u50cf\u4fee\u6539\u4efb\u4f55\u5bf9\u8c61\u5c5e\u6027\u4e00\u6837<\/li>\n\n\n\n<li>\u5f53\u4f60\u7528 <code>new Person()<\/code> \u521b\u5efa\u5b9e\u4f8b\u65f6\uff0cJS \u4f1a\u81ea\u52a8\u628a\u5b9e\u4f8b\u7684 <code>__proto__<\/code> \u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u662f JS \u7684\u6838\u5fc3\u8bbe\u8ba1\u4e4b\u4e00\uff1a<strong>\u4e00\u5207\u7686\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p>\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\uff0c\u624d\u80fd\u88ab\u8d4b\u503c\u3001\u88ab\u4f20\u9012\u3001\u88ab\u4fee\u6539\u5c5e\u6027 <code>prototype<\/code> \u5c5e\u6027\uff0c\u5c31\u662f\u4e3a\u4e86\u5b9e\u73b0\u539f\u578b\u7ee7\u627f\u800c\u5b58\u5728\u7684\u7279\u6b8a\u5c5e\u6027<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u5199\u4e2ahtml\u7684\u4ee3\u7801\uff0c\u770b\u5230\u8fd9\u4e2a\u539f\u578b\u5417\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u7684 HTML<\/strong>\uff0c\u6253\u5f00\u9875\u9762 \u2192 F12 \u63a7\u5236\u53f0\uff0c\u5c31\u80fd\u8089\u773c\u770b\u6e05\uff1a<code>prototype<\/code>\u3001<code>__proto__<\/code>\u3001<code>constructor<\/code>\u3001\u539f\u578b\u94fe \u5168\u90e8\u4e00\u76ee\u4e86\u7136\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"zh-CN\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>JS \u539f\u578b\u53ef\u89c6\u5316&lt;\/title>\n&lt;\/head>\n&lt;body>\n    &lt;script>\n        \/\/ 1. \u5b9a\u4e49\u666e\u901a\u6784\u9020\u51fd\u6570\n        function Person(name) {\n            this.name = name;\n        }\n\n        \/\/ 2. \u5f80\u3010\u51fd\u6570\u7684 prototype\uff08\u539f\u578b\u5bf9\u8c61\uff09\u3011\u6302\u8f7d\u5171\u4eab\u65b9\u6cd5\n        Person.prototype.sayHello = function(){\n            console.log(\"\u6211\u662f\u539f\u578b\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u7684\u540d\u5b57\uff1a\", this.name);\n        };\n\n        \/\/ 3. new \u751f\u6210\u5b9e\u4f8b\n        const p1 = new Person(\"\u5f20\u4e09\");\n        const p2 = new Person(\"\u674e\u56db\");\n\n\n        \/\/ ========== \u63a7\u5236\u53f0\u6253\u5370\uff0c\u6e05\u6670\u67e5\u770b ==========\n        console.log(\"---------- 1. \u67e5\u770b\u51fd\u6570\u7684 prototype ----------\");\n        console.log(\"Person.prototype\uff1a\", Person.prototype);\n\n\n        console.log(\"\\n---------- 2. \u67e5\u770b\u5b9e\u4f8b\u7684 __proto__ ----------\");\n        console.log(\"p1.__proto__\uff1a\", p1.__proto__);\n\n\n        console.log(\"\\n---------- 3. \u6838\u5fc3\u7b49\u5f0f\uff08\u5173\u952e\uff09 ----------\");\n        console.log(p1.__proto__ === Person.prototype); \/\/ true\n\n\n        console.log(\"\\n---------- 4. \u67e5\u770b constructor ----------\");\n        console.log(Person.prototype.constructor);\n\n\n        console.log(\"\\n---------- 5. \u8c03\u7528\u539f\u578b\u65b9\u6cd5 ----------\");\n        p1.sayHello();\n        p2.sayHello();\n\n\n        console.log(\"\\n---------- 6. \u5b8c\u6574\u539f\u578b\u94fe ----------\");\n        console.log(\"p1 -->\", p1.__proto__);\n        console.log(\"p1.__proto__ -->\", p1.__proto__.__proto__); \/\/ Object.prototype\n        console.log(\"Object.prototype -->\", p1.__proto__.__proto__.__proto__); \/\/ null\n    &lt;\/script>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"595\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-1024x595.png\" alt=\"\" class=\"wp-image-20652\" style=\"width:624px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-1024x595.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-300x174.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-768x446.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-1536x892.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-830x482.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-230x134.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-350x203.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158-480x279.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2026\/04\/\u56fe\u7247-158.png 1694w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\ud83d\udd16 \u539f\u578b\u5c31\u662f JS \u7ed9\u5bf9\u8c61 \u201c\u5171\u4eab\u5c5e\u6027\u548c\u65b9\u6cd5\u201d \u7684\u673a\u5236\uff0c\u4e0d\u7528\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u91cd\u590d\u5199\u4e00\u904d\u4ee3\u7801\uff0c\u7701\u7a7a\u95f4\u3001\u7701\u4ee3\u7801\u3002 \u4f60 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20647"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20647"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20647\/revisions"}],"predecessor-version":[{"id":20653,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20647\/revisions\/20653"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}