{"id":2699,"date":"2017-06-18T13:01:06","date_gmt":"2017-06-18T05:01:06","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2699"},"modified":"2017-06-18T13:01:06","modified_gmt":"2017-06-18T05:01:06","slug":"es6%e5%ad%a6%e4%b9%a0-class","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2699","title":{"rendered":"ES6\u5b66\u4e60 &#8212; Class"},"content":{"rendered":"<p>\u4f20\u7edf\u7684JavaScript\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u662f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u539f\u578b\u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5b50\uff1a<\/p>\n<pre>function Num(n, m){\r\n   this.n = n;\r\n   this.m = m;\r\n}\r\nNum.prototype.sum = function(){\r\n   return this.n + this.m;\r\n}\r\nvar num = new Num(4, 5);\r\nconsole.log(num.sum());  \/\/9<\/pre>\n<p>\u5728ES6\u89c4\u8303\u91cc\u6269\u5c55\u4e86\u201cclass\u201d\u7684\u6982\u5ff5\uff0c\u57fa\u672c\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/\u5b9a\u4e49\u7c7b\r\nclass Num {\r\n    \/\/\u6784\u9020\u65b9\u6cd5\r\n   constructor(n, m){\r\n   \tthis.n = n;\r\n   \tthis.m = m;\r\n   }\r\n    \/\/\u5b9a\u4e49\u65b9\u6cd5\r\n   sum(){\r\n   \treturn this.n + this.m;\r\n   }\r\n}\r\nvar num = new Num(4, 5);\r\nconsole.log(num.sum());  \/\/9<\/pre>\n<p>\u4e0e\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u4e00\u6837\uff0cES6\u7684class\u4e5f\u6709\u81ea\u5df1\u7684\u6784\u9020\u65b9\u6cd5\u3002\u521b\u5efa\u7c7b\u65f6\u4f1a\u6784\u9020\u4e00\u4e2a\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u5e76\u4e14\u5728new\u65f6\u7acb\u5373\u6267\u884c\u5b83\uff1a<\/p>\n<pre>class Num {\r\n   \t \t\r\n}\r\n\/\/\u7b49\u540c\u4e8e\r\nclass Num {\r\n\tconstructor(){\r\n\t\t\r\n\t}\r\n}<\/pre>\n<p>\u4fee\u6539\u6784\u9020\u65b9\u6cd5\uff1a<\/p>\n<pre>class Num {\r\n\tconstructor(){\r\n\t\tconsole.log(9+9);\r\n\t}\r\n}\r\nvar num = new Num();  \/\/18<\/pre>\n<p>class\u901a\u8fc7new\u5173\u952e\u5b57\u5b9e\u4f8b\u5316\uff0c\u5982\u679c\u5fd8\u8bb0new\u5173\u952e\u5b57\u4f1a\u62a5\u9519\u3002<\/p>\n<pre>class Num {\r\n\tconstructor(){\r\n\t\tconsole.log(9+9);\r\n\t}\r\n}\r\n\r\nvar num = Num();  \/\/\u62a5\u9519 Class constructor Num cannot be invoked without 'new'\r\n\r\nvar num = new Num();  \/\/18<\/pre>\n<p>\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u901a\u8fc7\u8868\u8fbe\u5f0f\u53bb\u63a5\u6536\u4e00\u4e2aclass\u4e4b\u540e\u7c7b\u540d\u5c31\u662f\u8868\u8fbe\u5f0f\u540d\u5b57\uff0c\u518d\u53bbnew\u8001\u7c7b\u540d\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<pre>var MyNum = class Num {\r\n\tconstructor(){\r\n\t\tconsole.log(9+9);\r\n\t}\r\n}\r\nvar num =  new Num();  \/\/\u62a5\u9519 Num is not defined<\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\uff0cNum\u53ea\u80fd\u5728\u201cclass\u201d\u5185\u90e8\u4f7f\u7528\uff0c\u4e0d\u80fd\u518d\u88ab\u5f53\u505a\u201c\u7c7b\u201d\u6765\u521d\u59cb\u5316<br \/>\n\u901a\u8fc7\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u5b8c\u6210\u7acb\u5373\u6267\u884c\u7684\u201cclass\u201d<\/p>\n<pre>var num = new class {\r\n\tconstructor(){\r\n\t\tconsole.log(9+9);\r\n\t}\r\n}\r\n\/\/ 18<\/pre>\n<p>\u5728\u201cclass\u201d\u91cc\u6709\u201cset\u201d\u548c\u201cget\u201d\u4e24\u4e2a\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5b9e\u73b0get\uff0cset\u65b9\u6cd5\u3002<\/p>\n<pre>class MyClass {\r\n\tget name(){\r\n\t\treturn '\u674e\u56db';\r\n\t}\r\n\tset name(val){\r\n\t\tconsole.log(val);  \/\/\u5f20\u4e09\r\n\t}\r\n}\r\nvar myclass = new MyClass();\r\nconsole.log(myclass.name);  \/\/\u674e\u56db\r\nmyclass.name = '\u5f20\u4e09';<\/pre>\n<p>\u201cclass\u201d\u901a\u8fc7\u201cextends\u201d\u5173\u952e\u5b57\u5b9e\u73b0\u7ee7\u627f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>class Fruit {\r\n\tconstructor(name){\r\n\t\tthis.name = name;\r\n\t}\r\n\tshow(){\r\n\t\tconsole.log(this.name);  \/\/\u7ea2\u5bcc\u58eb\r\n\t}\r\n}\r\nclass Apple extends Fruit {\r\n\t\r\n}\r\nvar apple = new Apple('\u7ea2\u5bcc\u58eb');\r\napple.show();<\/pre>\n<p>\u5982\u679c\u5b50\u7c7b\u9700\u8981\u4fee\u6539constructor\u65b9\u6cd5\uff0c\u5219\u5fc5\u987b\u5728constructor\u65b9\u6cd5\u4e2d\u8c03\u7528super\u65b9\u6cd5\uff0c\u5426\u5219\u5c31\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u5b9e\u4f8b\u5316\u5b50\u7c7b\u65f6\u6ca1\u6709\u81ea\u5df1\u7684this\u5bf9\u8c61\uff0c\u800c\u662f\u7ee7\u627f\u7236\u7c7b\u7684this\u5bf9\u8c61\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u52a0\u5de5\u3002\u5982\u679c\u4e0d\u8c03\u7528super\u65b9\u6cd5\uff0c\u5b50\u7c7b\u5c31\u5f97\u4e0d\u5230this\u5bf9\u8c61\u3002<\/p>\n<pre>class Fruit {\r\n\tconstructor(name){\r\n\t\tthis.name = name;\r\n\t}\r\n\tshow(){\r\n\t\tconsole.log(this.name);\r\n\t}\r\n}\r\n\r\nclass Apple extends Fruit {\r\n\tconstructor(name){\r\n\t\t\/\/\u4e0d\u8c03\u7528super\u65b9\u6cd5\u62a5\u9519 this is not defined\r\n\t}\r\n}\r\n\r\nclass Apple extends Fruit {\r\n\tconstructor(name){\r\n\t\tsuper(name);\r\n\t}\r\n}\r\nvar apple = new Apple('\u7ea2\u5bcc\u58eb');\r\napple.show();<\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684super\u6307\u5411\u7236\u7c7b\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7super\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<br \/>\nsuper\u5173\u952e\u5b57\u53ef\u4ee5\u505a\u4e3a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u5bf9\u8c61\u4f7f\u7528\u3002\u4e0a\u9762\u4f8b\u5b50\u4e2d\u662f\u4f5c\u4e3a\u51fd\u6570\u4f7f\u7528\uff0c\u5982\u679csuper\u4f5c\u4e3a\u51fd\u6570\u4f7f\u7528\uff0c\u53ea\u80fd\u7528\u5728\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e4b\u4e2d\uff0c\u7528\u5728\u5176\u4ed6\u5730\u65b9\u5c31\u4f1a\u62a5\u9519\u3002<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u4f5c\u4e3a\u5bf9\u8c61\u4f7f\u7528\uff0csuper\u4f5c\u4e3a\u5bf9\u8c61\u65f6\uff0c\u5728\u666e\u901a\u65b9\u6cd5\u4e2d\uff0c\u6307\u5411\u7236\u7c7b\u7684\u539f\u578b\u5bf9\u8c61\uff1b\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\uff0c\u6307\u5411\u7236\u7c7b\u3002<\/p>\n<pre>class Fruit {\r\n\tconstructor(name){\r\n\t\tthis.name = name;\r\n\t}\r\n\tshowName(){\r\n\t\treturn '\u7236\u7c7b\u7684'+this.name;\r\n\t}\r\n}\r\nclass Apple extends Fruit {\r\n\tconstructor(name){\r\n\t\tsuper(name);\r\n\t}\r\n\tshow(){\r\n\t\treturn super.showName();\r\n\t}\r\n\t\r\n}\r\nvar apple = new Apple('\u7ea2\u5bcc\u58eb');\r\nconsole.log(apple.show());<\/pre>\n<p>&nbsp;<\/p>\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=2699\">ES6\u5b66\u4e60 &#8212; Class<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u4f20\u7edf\u7684JavaScript\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u662f\u901a\u8fc7\u6784\u9020\u51fd\u6570\u539f\u578b\u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5b50\uff1a function Num(n, m){ this.n = n; this.m = m; } Num.prototype.su [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[186],"tags":[385,143],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2699"}],"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=2699"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2699\/revisions"}],"predecessor-version":[{"id":2700,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2699\/revisions\/2700"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2699"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}