{"id":2669,"date":"2017-06-14T17:13:11","date_gmt":"2017-06-14T09:13:11","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2669"},"modified":"2017-06-14T17:13:11","modified_gmt":"2017-06-14T09:13:11","slug":"js%e5%af%b9%e8%b1%a1%e7%bb%a7%e6%89%bf%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2669","title":{"rendered":"JS\u5bf9\u8c61\u7ee7\u627f\u7684\u51e0\u79cd\u65b9\u5f0f\u603b\u7ed3"},"content":{"rendered":"<p>\u4eca\u5929\u5b66\u4e60\u4e86\u4e00\u4e0bjs\u7684\u7ee7\u627f\uff0cjs\u4e2d\u7684\u7ee7\u627f\u4e3b\u8981\u5206\u56db\u79cd\uff0c\u539f\u578b\u7ee7\u627f\uff0c\u6784\u9020\u51fd\u6570\u7ee7\u627f\uff0ccall\/apply\u7ee7\u627f\u4ee5\u53caes6\u7684extend\u7ee7\u627f\u3002<br \/>\n<span style=\"font-size: medium;\"><b>1.\u539f\u578b\u7ee7\u627f\uff1a<\/b><\/span><br \/>\n\u539f\u578b\u7ee7\u627f\u4e3b\u8981\u5229\u7528js\u5bf9\u8c61\u7684prototype\u5f15\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u590d\u5236\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>\/\/\u5b9a\u4e49\u4e00\u4e2aPerson\u7c7b\r\n\t function Person(name){  \r\n        this.name=name;  \r\n    }  \r\n\t\/\/\u6253\u62db\u547c\r\n    Person.prototype.sayHello=function(){  \r\n        alert(\"Hello,my name is \"+this.name);  \r\n    }  \r\n\t \r\n\t\/\/\u5b9a\u4e49Man\u7c7b\uff0c\u7ee7\u627fPerson\u7c7b\r\n\tfunction Man(name,age){\r\n\t\tthis.name=name;\r\n\t\tthis.age=age; \r\n\t}\r\n\tMan.prototype=new Person();\r\n\tvar man= new Man(\"\u9694\u58c1\u8001\u738b\",30);\r\n\tman.sayHello();<\/pre>\n<p><b><span style=\"font-size: medium;\">2.\u6784\u9020\u51fd\u6570\u7ee7\u627f<\/span><\/b><br \/>\n\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b8c\u6210\u7ee7\u627f\u3002<\/p>\n<pre> \/\/\u5b9a\u4e49\u4e00\u4e2aPerson\u7c7b\r\n\t function Person(name){  \r\n        this.name=name;  \r\n\t\tthis.sayHello=function(){  \r\n        alert(\"Hello,my name is \"+this.name);  \r\n\t\t}  \r\n    }  \r\n \r\n\t\/\/\u5b9a\u4e49Man\u7c7b\uff0c\u7ee7\u627fPerson\u7c7b\r\n\tfunction Man(name,age){\r\n\t\tthis.constructor=Person;\r\n\t\tthis.constructor(name);\r\n\t\tthis.age=age;\r\n\t}\r\n\r\n\tvar man= new Man(\"\u9694\u58c1\u8001\u738b\",30);\r\n\tman.sayHello();<\/pre>\n<p><b><span style=\"font-size: medium;\">3.call\/apply\u7ee7\u627f<\/span><\/b><br \/>\n\u5229\u7528call\/apply\u65b9\u6cd5\u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u7ee7\u627f<\/p>\n<pre> \/\/\u5b9a\u4e49\u4e00\u4e2aPerson\u7c7b\r\n\t function Person(name){  \r\n        this.name=name;  \r\n\t\tthis.sayHello=function(){  \r\n        alert(\"Hello,my name is \"+this.name);  \r\n\t\t}  \r\n    }  \r\n \r\n\t\/\/\u5b9a\u4e49Man\u7c7b\uff0c\u7ee7\u627fPerson\u7c7b\r\n\tfunction Man(name,age){\r\n\t\tPerson.call(this,name);\r\n\t\tthis.age=age;\r\n\t}\r\n\r\n\tvar man= new Man(\"\u9694\u58c1\u8001\u738b\",30);\r\n\tman.sayHello();<\/pre>\n<p><b><span style=\"font-size: medium;\">4.extends\u7ee7\u627f<\/span><\/b><br \/>\n\u4f7f\u7528ES6\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\uff0c\u7c7b\u4f3cjava\u5b9a\u4e49\u7c7b\u7684\u65b9\u5f0f\u5b9e\u73b0\u7ee7\u627f\uff0c\u6ce8\u610f\u90e8\u5206\u6d4f\u89c8\u5668\u4e0d\u517c\u5bb9 &#8211; &#8211;<\/p>\n<pre>'use strict';\r\n     \/\/\u5b9a\u4e49\u4e00\u4e2aPerson\u7c7b\r\n\t class Person{  \r\n\t \/\/\u6784\u9020\u51fd\u6570\r\n        constructor(name){\r\n\t\t\tthis.name=name;\r\n\t\t}\r\n\t\tsayHello(){\r\n\t\t\talert(\"My name is\"+this.name);\r\n\t\t}\r\n    }  \r\n\t\r\n\tclass Man extends Person{\r\n\t\tconstructor(name,age){\r\n\t\t\t\/\/\u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\r\n\t\t\tsuper(name);\r\n\t\t\tthis.age=age;\r\n\t\t}\r\n\t\t\r\n\t}\r\n\r\n\tvar man= new Man(\"\u9694\u58c1\u8001\u738b\",30);\r\n\tman.sayHello();<\/pre>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=2669\">JS\u5bf9\u8c61\u7ee7\u627f\u7684\u51e0\u79cd\u65b9\u5f0f\u603b\u7ed3<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5b66\u4e60\u4e86\u4e00\u4e0bjs\u7684\u7ee7\u627f\uff0cjs\u4e2d\u7684\u7ee7\u627f\u4e3b\u8981\u5206\u56db\u79cd\uff0c\u539f\u578b\u7ee7\u627f\uff0c\u6784\u9020\u51fd\u6570\u7ee7\u627f\uff0ccall\/apply\u7ee7\u627f\u4ee5\u53caes6\u7684extend\u7ee7\u627f\u3002 1.\u539f\u578b\u7ee7\u627f\uff1a \u539f\u578b\u7ee7\u627f\u4e3b\u8981\u5229\u7528js\u5bf9\u8c61\u7684prototype\u5f15\u7528\u7236\u7c7b\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,81],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2669"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2669"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2669\/revisions"}],"predecessor-version":[{"id":2670,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2669\/revisions\/2670"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2669"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}