{"id":154369,"date":"2024-05-08T11:31:15","date_gmt":"2024-05-08T03:31:15","guid":{"rendered":""},"modified":"2024-05-08T11:31:23","modified_gmt":"2024-05-08T03:31:23","slug":"%e5%9c%a8-javascript-%e4%b8%ad%ef%bc%8c%e6%9c%89%e5%93%aa%e4%ba%9b%e6%96%b9%e5%bc%8f%e5%8f%af%e4%bb%a5%e8%be%be%e5%88%b0%e7%bb%a7%e6%89%bf%e7%9a%84%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/154369.html","title":{"rendered":"\u5728 JavaScript \u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u8fbe\u5230\u7ee7\u627f\u7684\u6548\u679c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212619\/70ffee3f-59bc-4ace-9af9-d77d9143eaa6.webp\" alt=\"\u5728 JavaScript \u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u8fbe\u5230\u7ee7\u627f\u7684\u6548\u679c\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u8fbe\u5230\u7ee7\u627f\u7684\u6548\u679c\u4e3b\u8981\u6709\u4ee5\u4e0b\u56db\u79cd\u65b9\u5f0f\uff1a<strong>\u539f\u578b\u94fe\u7ee7\u627f\u3001\u6784\u9020\u51fd\u6570\u7ee7\u627f\u3001\u7ec4\u5408\u7ee7\u627f\u3001\u539f\u578b\u5f0f\u7ee7\u627f\u3001\u5bc4\u751f\u5f0f\u7ee7\u627f\u3001\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f<\/strong>\u3002\u539f\u578b\u94fe\u7ee7\u627f\u662f\u6700\u57fa\u672c\u7684\u7ee7\u627f\u65b9\u5f0f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u539f\u578b\u5bf9\u8c61\uff0c\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61\u6307\u5411\u4e00\u4e2a\u7236\u7c7b\u578b\u7684\u5b9e\u4f8b\u6765\u5b9e\u73b0\u7ee7\u627f\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u6613\u4e8e\u5b9e\u73b0\uff0c\u4f46\u5b58\u5728\u5305\u62ec\u65e0\u6cd5\u4f20\u9012\u53c2\u6570\u7ed9\u7236\u7c7b\u578b\u6784\u9020\u51fd\u6570\u548c\u5171\u4eab\u5f15\u7528\u7c7b\u578b\u5c5e\u6027\u7684\u7f3a\u70b9\u3002\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5176\u4e2d\u7684\u539f\u578b\u94fe\u7ee7\u627f\uff0c\u5e76\u5bf9\u5176\u5b83\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u539f\u578b\u94fe\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u539f\u578b\u94fe\u7ee7\u627f\u5229\u7528\u4e86JavaScript\u7684\u539f\u578b\u7279\u6027\u3002\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u6240\u6709\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u90fd\u4f1a\u5171\u4eab\u539f\u578b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u5b50\u7c7b\u578b\u7684\u539f\u578b\u88ab\u8bbe\u7f6e\u4e3a\u7236\u7c7b\u578b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u56e0\u6b64\uff0c\u5b50\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u539f\u578b\u94fe\u8bbf\u95ee\u7236\u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Parent() {<\/p>\n<p>    this.parentProperty = true;<\/p>\n<p>}<\/p>\n<p>Parent.prototype.getParentProperty = function() {<\/p>\n<p>    return this.parentProperty;<\/p>\n<p>};<\/p>\n<p>function Child() {<\/p>\n<p>    this.childProperty = false;<\/p>\n<p>}<\/p>\n<p>Child.prototype = new Parent(); \/\/ \u7ee7\u627f\u81eaParent<\/p>\n<p>var childInstance = new Child();<\/p>\n<p>console.log(childInstance.getParentProperty()); \/\/ true<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u3001\u6613\u4e8e\u5b9e\u73b0\uff0c\u5b50\u7c7b\u578b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u578b\u7684\u539f\u578b\u4e0a\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u5305\u62ec\u6240\u6709\u5b9e\u4f8b\u90fd\u4f1a\u5171\u4eab\u7ee7\u627f\u6765\u7684\u5f15\u7528\u7c7b\u578b\u5c5e\u6027\uff08\u5982\u6570\u7ec4\u3001\u5bf9\u8c61\uff09\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u5c5e\u6027\u7684\u4fee\u6539\u4f1a\u5f71\u54cd\u5176\u4ed6\u5b9e\u4f8b\uff1b\u65e0\u6cd5\u5728\u4e0d\u5f71\u54cd\u6240\u6709\u5bf9\u8c61\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e0b\uff0c\u7ed9\u7236\u7c7b\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6784\u9020\u51fd\u6570\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u5229\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u5e76\u4f7f\u7528\u201cthis\u201d\u53d8\u91cf\u6765\u5f15\u7528\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u5728\u5b50\u7c7b\u578b\u6784\u9020\u51fd\u6570\u7684\u5185\u90e8\u8c03\u7528\u7236\u7c7b\u578b\u6784\u9020\u51fd\u6570\uff0c\u51fd\u6570\u5185\u90e8\u4f7f\u7528&quot;this&quot;\u5173\u952e\u5b57\u4f1a\u88ab\u7ed1\u5b9a\u5230\u5b50\u7c7b\u578b\u5b9e\u4f8b\u4e0a\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Parent(name) {<\/p>\n<p>    this.name = name;<\/p>\n<p>}<\/p>\n<p>function Child(name, age) {<\/p>\n<p>    Parent.call(this, name); \/\/ \u7ee7\u627f\u81eaParent\uff0c\u5e76\u5411Parent\u4f20\u9012\u53c2\u6570<\/p>\n<p>    this.age = age;<\/p>\n<p>}<\/p>\n<p>var childInstance = new Child(&#039;ChildName&#039;, 18);<\/p>\n<p>console.log(childInstance.name); \/\/ &#039;ChildName&#039;<\/p>\n<p>console.log(childInstance.age); \/\/ 18<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ef\u4ee5\u5728\u5b50\u7c7b\u578b\u6784\u9020\u51fd\u6570\u4e2d\u5411\u7236\u7c7b\u578b\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570\uff1b\u907f\u514d\u4e86\u5f15\u7528\u7c7b\u578b\u7684\u5c5e\u6027\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u65b9\u6cd5\u90fd\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5b9a\u4e49\uff0c\u65e0\u6cd5\u590d\u7528\u3002\u5728\u7236\u7c7b\u539f\u578b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u5b50\u7c7b\u4e5f\u65e0\u6cd5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ec4\u5408\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u7ec4\u5408\u7ee7\u627f\u7ed3\u5408\u4e86\u539f\u578b\u94fe\u7ee7\u627f\u548c\u6784\u9020\u51fd\u6570\u7ee7\u627f\u7684\u4f18\u70b9\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728JavaScript\u4e2d\u5e94\u7528\u5e7f\u6cdb\uff0c\u88ab\u8ba4\u4e3a\u662f\u5b9e\u73b0\u57fa\u4e8e\u7c7b\u578b\u7ee7\u627f\u7684\u6700\u4f73\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u4f7f\u7528\u539f\u578b\u94fe\u5b9e\u73b0\u5bf9\u539f\u578b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u7ee7\u627f\uff0c\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7ee7\u627f\u5b9e\u73b0\u5bf9\u5b9e\u4f8b\u5c5e\u6027\u7684\u7ee7\u627f\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Parent(name) {<\/p>\n<p>    this.name = name;<\/p>\n<p>    this.colors = [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;];<\/p>\n<p>}<\/p>\n<p>Parent.prototype.getName = function() {<\/p>\n<p>    return this.name;<\/p>\n<p>};<\/p>\n<p>function Child(name, age) {<\/p>\n<p>    Parent.call(this, name); \/\/ \u6784\u9020\u51fd\u6570\u7ee7\u627f<\/p>\n<p>    this.age = age;<\/p>\n<p>}<\/p>\n<p>Child.prototype = new Parent(); \/\/ \u539f\u578b\u94fe\u7ee7\u627f<\/p>\n<p>Child.prototype.constructor = Child; \/\/ \u4fee\u590d\u6784\u9020\u51fd\u6570\u6307\u5411<\/p>\n<p>var childInstance1 = new Child(&#039;Kevin&#039;, 18);<\/p>\n<p>var childInstance2 = new Child(&#039;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>sy&#039;, 20);<\/p>\n<p>childInstance1.colors.push(&#039;black&#039;);<\/p>\n<p>console.log(childInstance1.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;, &#039;black&#039;]<\/p>\n<p>console.log(childInstance2.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;]<\/p>\n<p>console.log(childInstance1.getName()); \/\/ &#039;Kevin&#039;<\/p>\n<p>console.log(childInstance2.getName()); \/\/ &#039;Daisy&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u878d\u5408\u539f\u578b\u94fe\u548c\u6784\u9020\u51fd\u6570\u7684\u4f18\u70b9\uff0c<strong>\u80fd\u591f\u5b9e\u73b0\u51fd\u6570\u590d\u7528\u5e76\u4e14\u53ef\u4ee5\u5411\u7236\u7c7b\u578b\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u8c03\u7528\u4e24\u6b21\u7236\u7c7b\u6784\u9020\u51fd\u6570\uff08\u4e00\u4e2a\u5728\u521b\u5efa\u5b50\u7c7b\u539f\u578b\u65f6\uff0c\u53e6\u4e00\u4e2a\u5728\u5b50\u7c7b\u6784\u9020\u51fd\u6570\u5185\u90e8\uff09\uff0c\u53ef\u80fd\u5bfc\u81f4\u5b50\u7c7b\u7684\u539f\u578b\u4e0a\u591a\u4f59\u7684\u7236\u7c7b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u539f\u578b\u5f0f\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u539f\u578b\u5f0f\u7ee7\u627f\u662f\u4e00\u79cd\u6ca1\u6709\u4f7f\u7528\u4e25\u683c\u610f\u4e49\u4e0a\u6784\u9020\u51fd\u6570\u7684\u7ee7\u627f\u65b9\u5f0f\u3002\u5728\u5bf9\u8c61\u4e0a\u4f7f\u7528\u5df2\u5b58\u5728\u7684\u5bf9\u8c61\u4f5c\u4e3a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u539f\u578b\u3002 <\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u501f\u52a9\u539f\u578b\u53ef\u4ee5\u57fa\u4e8e\u5df2\u6709\u7684\u5bf9\u8c61\u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u540c\u65f6\u8fd8\u4e0d\u5fc5\u56e0\u6b64\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var parent = {<\/p>\n<p>    name: &#039;parent&#039;,<\/p>\n<p>    colors: [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;]<\/p>\n<p>};<\/p>\n<p>var childInstance = Object.create(parent);<\/p>\n<p>childInstance.name = &#039;child&#039;;<\/p>\n<p>childInstance.colors.push(&#039;black&#039;);<\/p>\n<p>var anotherChild = Object.create(parent);<\/p>\n<p>anotherChild.name = &#039;anotherChild&#039;; <\/p>\n<p>anotherChild.colors.push(&#039;yellow&#039;);<\/p>\n<p>console.log(childInstance.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;, &#039;black&#039;, &#039;yellow&#039;]<\/p>\n<p>console.log(anotherChild.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;, &#039;black&#039;, &#039;yellow&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u5408\u4e0d\u9700\u8981\u5355\u72ec\u521b\u5efa\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4ecd\u7136\u9700\u8981\u5728\u591a\u4e2a\u5bf9\u8c61\u95f4\u5171\u4eab\u4fe1\u606f\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u5305\u542b\u5f15\u7528\u7c7b\u578b\u503c\u7684\u5c5e\u6027\u59cb\u7ec8\u90fd\u4f1a\u5171\u4eab\u76f8\u5e94\u7684\u503c\uff0c\u4e0e\u539f\u578b\u94fe\u7ee7\u627f\u7684\u7f3a\u70b9\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5bc4\u751f\u5f0f\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u5bc4\u751f\u5f0f\u7ee7\u627f\u662f\u57fa\u4e8e\u539f\u578b\u5f0f\u7ee7\u627f\u7684\u601d\u60f3\uff0c\u5e76\u589e\u52a0\u4e86\u65b0\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u4ee5\u589e\u5f3a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u4ec5\u7528\u6765\u5c01\u88c5\u7ee7\u627f\u8fc7\u7a0b\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u5185\u90e8\u4ee5\u67d0\u79cd\u65b9\u5f0f\u6765\u589e\u5f3a\u5bf9\u8c61\uff0c\u6700\u540e\u518d\u50cf\u771f\u7684\u662f\u5b83\u505a\u4e86\u6240\u6709\u5de5\u4f5c\u4e00\u6837\u8fd4\u56de\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function createAnother(original) {<\/p>\n<p>    var clone = Object.create(original); \/\/ \u901a\u8fc7\u8c03\u7528\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61<\/p>\n<p>    clone.sayHi = function() {           \/\/ \u4ee5\u67d0\u79cd\u65b9\u5f0f\u6765\u589e\u5f3a\u8fd9\u4e2a\u5bf9\u8c61<\/p>\n<p>        console.log(&#039;hi&#039;);<\/p>\n<p>    };<\/p>\n<p>    return clone; \/\/ \u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61<\/p>\n<p>}<\/p>\n<p>var person = {<\/p>\n<p>    name: &#039;Kevin&#039;,<\/p>\n<p>    colors: [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;]<\/p>\n<p>};<\/p>\n<p>var anotherPerson = createAnother(person);<\/p>\n<p>anotherPerson.sayHi(); \/\/ &#039;hi&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u5728\u4e0d\u5fc5\u9884\u5148\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u4e5f\u80fd\u591f\u5b9e\u73b0\u5bf9\u5bf9\u8c61\u7684\u589e\u5f3a\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4e0e\u539f\u578b\u5f0f\u7ee7\u627f\u4e00\u6837\uff0c\u5f15\u7528\u7c7b\u578b\u7684\u5c5e\u6027\u4f1a\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f<\/h3>\n<\/p>\n<p><p>\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f\u662f\u5bf9\u7ec4\u5408\u7ee7\u627f\u7684\u8fdb\u4e00\u6b65\u4f18\u5316\u3002\u5728\u8fd9\u79cd\u7ee7\u627f\u65b9\u5f0f\u4e0b\uff0c\u6211\u4eec\u53ea\u8c03\u7528\u4e00\u6b21\u7236\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4e14\u56e0\u6b64\u907f\u514d\u4e86\u5728\u5b50\u7c7b\u539f\u578b\u4e0a\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u3001\u591a\u4f59\u7684\u5c5e\u6027\u3002\u4e8e\u6b64\u540c\u65f6\uff0c\u539f\u578b\u94fe\u8fd8\u80fd\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><p><strong>\u539f\u7406\uff1a<\/strong> \u901a\u8fc7\u501f\u7528\u6784\u9020\u51fd\u6570\u6765\u7ee7\u627f\u5c5e\u6027\uff0c\u901a\u8fc7\u539f\u578b\u94fe\u7684\u6df7\u6210\u5f62\u5f0f\u6765\u7ee7\u627f\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function inheritPrototype(child, parent) {<\/p>\n<p>    var prototype = Object.create(parent.prototype); \/\/ \u521b\u5efa\u5bf9\u8c61<\/p>\n<p>    prototype.constructor = child;                   \/\/ \u589e\u5f3a\u5bf9\u8c61<\/p>\n<p>    child.prototype = prototype;                     \/\/ \u6307\u5b9a\u5bf9\u8c61<\/p>\n<p>}<\/p>\n<p>function Parent(name) {<\/p>\n<p>    this.name = name;<\/p>\n<p>    this.colors = [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;];<\/p>\n<p>}<\/p>\n<p>Parent.prototype.getName = function() {<\/p>\n<p>    return this.name;<\/p>\n<p>};<\/p>\n<p>function Child(name, age) {<\/p>\n<p>    Parent.call(this, name); \/\/ \u7ee7\u627f\u5c5e\u6027<\/p>\n<p>    this.age = age;<\/p>\n<p>}<\/p>\n<p>inheritPrototype(Child, Parent);<\/p>\n<p>var childInstance = new Child(&#039;Kevin&#039;, 18);<\/p>\n<p>console.log(childInstance.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;]<\/p>\n<p>console.log(childInstance.getName()); \/\/ &#039;Kevin&#039;<\/p>\n<p>childInstance.colors.push(&#039;black&#039;);<\/p>\n<p>console.log(childInstance.colors); \/\/ [&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;, &#039;black&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ea\u8c03\u7528\u4e00\u6b21\u7236\u7c7b\u6784\u9020\u51fd\u6570\uff0c\u6548\u7387\u66f4\u9ad8\uff0c\u907f\u514d\u4e86\u5728Child.prototype\u4e0a\u9762\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u3001\u591a\u4f59\u7684\u5c5e\u6027\u3002\u540c\u65f6\u4fdd\u6301\u539f\u578b\u94fe\u4e0d\u53d8\uff0c\u80fd\u591f\u6b63\u5e38\u4f7f\u7528instanceof\u548cisPrototypeOf\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cJavaScript\u4e2d\u7684\u7ee7\u627f\u662f\u4f9d\u8d56\u4e8e\u539f\u578b\u94fe\u548c\u6784\u9020\u51fd\u6570\u7684\u673a\u5236\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ee7\u627f\u65b9\u5f0f\uff0c<strong>\u5bc4\u751f\u7ec4\u5408\u5f0f\u7ee7\u627f\u88ab\u8ba4\u4e3a\u662f\u6700\u7406\u60f3\u7684<\/strong>\u3002\u6bcf\u79cd\u7ee7\u627f\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5e94\u5f53\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f JavaScript \u4e2d\u7684\u7ee7\u627f?<\/strong> <\/p>\n<p>\u7ee7\u627f\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u4ece\u73b0\u6709\u7684\u5bf9\u8c61\u4e2d\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7ee7\u627f\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528\u539f\u578b\u94fe\u5b9e\u73b0\u7ee7\u627f\uff1f<\/strong><\/p>\n<p>JavaScript \u4e2d\u7684\u539f\u578b\u94fe\u662f\u5b9e\u73b0\u7ee7\u627f\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u7236\u5bf9\u8c61\u7684\u5b9e\u4f8b\u4f5c\u4e3a\u5b50\u5bf9\u8c61\u7684\u539f\u578b\uff0c\u6765\u5b9e\u73b0\u7ee7\u627f\u3002\u8fd9\u6837\u5b50\u5bf9\u8c61\u5c31\u53ef\u4ee5\u4f7f\u7528\u7236\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8981\u5b9e\u73b0\u539f\u578b\u94fe\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Object.create()<\/code> \u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06\u7236\u5bf9\u8c61\u7684\u5b9e\u4f8b\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u5b50\u5bf9\u8c61\u5c31\u53ef\u4ee5\u7ee7\u627f\u7236\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u7ee7\u627f\uff1f<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u5b9e\u73b0\u7ee7\u627f\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7236\u5bf9\u8c61\uff0c\u5e76\u5c06\u5c5e\u6027\u548c\u65b9\u6cd5\u7ed1\u5b9a\u5230\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u5b50\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528\u7236\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u6765\u5b9e\u73b0\u7ee7\u627f\u3002\u8fd9\u6837\u5b50\u5bf9\u8c61\u5c31\u53ef\u4ee5\u7ee7\u627f\u7236\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>call()<\/code> \u6216\u8005 <code>apply()<\/code> \u65b9\u6cd5\u6765\u8c03\u7528\u7236\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u5b50\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u6837\u5b50\u5bf9\u8c61\u5c31\u53ef\u4ee5\u7ee7\u627f\u7236\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0c\u8fbe\u5230\u7ee7\u627f\u7684\u6548\u679c\u4e3b\u8981\u6709\u4ee5\u4e0b\u56db\u79cd\u65b9\u5f0f\uff1a\u539f\u578b\u94fe\u7ee7\u627f\u3001\u6784\u9020\u51fd\u6570\u7ee7\u627f\u3001\u7ec4\u5408\u7ee7\u627f\u3001\u539f\u578b\u5f0f\u7ee7\u627f\u3001\u5bc4 [&hellip;]","protected":false},"author":3,"featured_media":154377,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/154369"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=154369"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/154369\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/154377"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=154369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=154369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=154369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}