');display:flex;inset:0;justify-content:center;position:absolute}.copy-to-clipboard-button[data-copy-state=copy-success]:before{content:url('data:image/svg+xml;utf8, ')}.kg-bookmark-card figcaption,.kg-gallery-card figcaption,.kg-image-card figcaption,.kg-video-card figcaption{padding-top:2px;text-align:center;--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.kg-bookmark-card figcaption:is(.dark *),.kg-gallery-card figcaption:is(.dark *),.kg-image-card figcaption:is(.dark *),.kg-video-card figcaption:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.kg-code-card figcaption{padding-top:2px;text-align:center;--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.kg-code-card figcaption:is(.dark *){--tw-text-opacity:1;color:rgb(15 23 42/var(--tw-text-opacity,1))}.toc{overflow-y:auto}.toc>.toc-list{overflow:hidden;position:relative}.toc>.toc-list li{list-style:none}.js-toc{overflow-y:hidden}.toc-list{margin:0;padding-left:1.5rem}a.toc-link{color:currentColor;height:100%}.is-collapsible{max-height:1000px;overflow:hidden;transition:all .3s ease-in-out}.is-collapsed{max-height:0}.is-position-fixed{position:fixed!important;top:0}.is-active-link{font-weight:700}.toc-link:before{background-color:#eee;content:" ";display:inline-block;height:inherit;left:0;margin-top:-1px;position:absolute;width:2px}.is-active-link:before{background-color:#54bc4b}.toc-list-item{font-size:.95rem;margin-bottom:.5rem}.toc-link{border-radius:.25rem;color:#333;display:block;padding:.25rem .5rem;text-decoration:none;transition:color .2s ease}.toc-link:hover{background-color:#f3f4f6;color:#1e40af}.toc-link.is-active-link{background-color:#e0e7ff;color:#1e40af;font-weight:600}.toc-link.node-name--H2{font-size:1rem}.content img[width="20"],.content img[width="25"]{display:inline;margin-left:.25rem;margin-right:.25rem}.content table td[colspan="2"]{text-align:center}.content table td img{margin:auto}.first-letter\:uppercase:first-letter{text-transform:uppercase}.hover\:border-blue-400:hover{--tw-border-opacity:1;border-color:rgb(96 165 250/var(--tw-border-opacity,1))}.hover\:border-secondary-300:hover{--tw-border-opacity:1;border-color:rgb(203 213 225/var(--tw-border-opacity,1))}.hover\:bg-blue-700:hover{--tw-bg-opacity:1;background-color:rgb(29 78 216/var(--tw-bg-opacity,1))}.hover\:bg-gray-100:hover{--tw-bg-opacity:1;background-color:rgb(243 244 246/var(--tw-bg-opacity,1))}.hover\:bg-gray-50:hover{--tw-bg-opacity:1;background-color:rgb(249 250 251/var(--tw-bg-opacity,1))}.hover\:bg-primary-100:hover{--tw-bg-opacity:1;background-color:rgb(224 242 254/var(--tw-bg-opacity,1))}.hover\:bg-primary-200:hover{--tw-bg-opacity:1;background-color:rgb(186 230 253/var(--tw-bg-opacity,1))}.hover\:bg-primary-50:hover{--tw-bg-opacity:1;background-color:rgb(240 249 255/var(--tw-bg-opacity,1))}.hover\:bg-primary-50\/80:hover{background-color:rgba(240,249,255,.8)}.hover\:bg-primary-700:hover{--tw-bg-opacity:1;background-color:rgb(29 78 216/var(--tw-bg-opacity,1))}.hover\:bg-red-100:hover{--tw-bg-opacity:1;background-color:rgb(254 226 226/var(--tw-bg-opacity,1))}.hover\:bg-secondary-100:hover{--tw-bg-opacity:1;background-color:rgb(241 245 249/var(--tw-bg-opacity,1))}.hover\:from-blue-600:hover{--tw-gradient-from:#2563eb var(--tw-gradient-from-position);--tw-gradient-to:rgba(37,99,235,0) var(--tw-gradient-to-position);--tw-gradient-stops:var(--tw-gradient-from),var(--tw-gradient-to)}.hover\:to-blue-700:hover{--tw-gradient-to:#1d4ed8 var(--tw-gradient-to-position)}.hover\:text-accent-600:hover{--tw-text-opacity:1;color:rgb(217 119 6/var(--tw-text-opacity,1))}.hover\:text-blue-600:hover{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.hover\:text-blue-800:hover{--tw-text-opacity:1;color:rgb(30 64 175/var(--tw-text-opacity,1))}.hover\:text-gray-400:hover{--tw-text-opacity:1;color:rgb(156 163 175/var(--tw-text-opacity,1))}.hover\:text-gray-700:hover{--tw-text-opacity:1;color:rgb(55 65 81/var(--tw-text-opacity,1))}.hover\:text-green-600:hover{--tw-text-opacity:1;color:rgb(22 163 74/var(--tw-text-opacity,1))}.hover\:text-primary-600:hover{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.hover\:text-primary-700:hover{--tw-text-opacity:1;color:rgb(29 78 216/var(--tw-text-opacity,1))}.hover\:text-secondary-700:hover{--tw-text-opacity:1;color:rgb(51 65 85/var(--tw-text-opacity,1))}.hover\:opacity-90:hover{opacity:.9}.hover\:shadow-lg:hover{--tw-shadow:0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);--tw-shadow-colored:0 10px 15px -3px var(--tw-shadow-color),0 4px 6px -4px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.focus\:border-transparent:focus{border-color:transparent}.focus\:bg-primary-50:focus{--tw-bg-opacity:1;background-color:rgb(240 249 255/var(--tw-bg-opacity,1))}.focus\:text-primary-600:focus{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.focus\:outline-none:focus{outline:2px solid transparent;outline-offset:2px}.focus\:ring-1:focus{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.focus\:ring-blue-500:focus{--tw-ring-opacity:1;--tw-ring-color:rgb(59 130 246/var(--tw-ring-opacity,1))}.focus\:ring-gray-500:focus{--tw-ring-opacity:1;--tw-ring-color:rgb(107 114 128/var(--tw-ring-opacity,1))}.focus\:ring-offset-2:focus{--tw-ring-offset-width:2px}.group:focus-within .group-focus-within\:opacity-100{opacity:1}.group:hover .group-hover\:visible,.group\/level2:hover .group-hover\/level2\:visible{visibility:visible}.group:hover .group-hover\:block{display:block}.group\/item:hover .group-hover\/item\:translate-x-1{--tw-translate-x:0.25rem}.group\/item:hover .group-hover\/item\:translate-x-1,.group\/level2:hover .group-hover\/level2\:translate-x-0{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group\/level2:hover .group-hover\/level2\:translate-x-0{--tw-translate-x:0px}.group\/level2:hover .group-hover\/level2\:translate-x-1{--tw-translate-x:0.25rem}.group:hover .group-hover\:-translate-x-1,.group\/level2:hover .group-hover\/level2\:translate-x-1{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group:hover .group-hover\:-translate-x-1{--tw-translate-x:-0.25rem}.group:hover .group-hover\:translate-x-1{--tw-translate-x:0.25rem}.group:hover .group-hover\:rotate-180,.group:hover .group-hover\:translate-x-1{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group:hover .group-hover\:rotate-180{--tw-rotate:180deg}.group\/bookmark:hover .group-hover\/bookmark\:fill-current,.group\/comment:hover .group-hover\/comment\:fill-current,.group\/like:hover .group-hover\/like\:fill-current{fill:currentColor}.group:hover .group-hover\:fill-black\/75{fill:rgba(0,0,0,.75)}.group:hover .group-hover\:fill-current{fill:currentColor}.group:hover .group-hover\:text-primary-600{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.group:hover .group-hover\:opacity-100,.group\/level2:hover .group-hover\/level2\:opacity-100{opacity:1}.dark\:border-primary-400:is(.dark *){--tw-border-opacity:1;border-color:rgb(56 189 248/var(--tw-border-opacity,1))}.dark\:border-secondary-200:is(.dark *){--tw-border-opacity:1;border-color:rgb(226 232 240/var(--tw-border-opacity,1))}.dark\:border-secondary-600:is(.dark *){--tw-border-opacity:1;border-color:rgb(71 85 105/var(--tw-border-opacity,1))}.dark\:border-secondary-700:is(.dark *){--tw-border-opacity:1;border-color:rgb(51 65 85/var(--tw-border-opacity,1))}.dark\:bg-blue-900\/20:is(.dark *){background-color:rgba(30,58,138,.2)}.dark\:bg-gray-800:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:bg-gray-800\/95:is(.dark *){background-color:rgba(31,41,55,.95)}.dark\:bg-gray-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(17 24 39/var(--tw-bg-opacity,1))}.dark\:bg-gray-900\/80:is(.dark *){background-color:rgba(17,24,39,.8)}.dark\:bg-primary-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(30 58 138/var(--tw-bg-opacity,1))}.dark\:bg-secondary-700:is(.dark *){--tw-bg-opacity:1;background-color:rgb(51 65 85/var(--tw-bg-opacity,1))}.dark\:bg-secondary-800:is(.dark *){--tw-bg-opacity:1;background-color:rgb(30 41 59/var(--tw-bg-opacity,1))}.dark\:bg-secondary-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(15 23 42/var(--tw-bg-opacity,1))}.dark\:bg-slate-400:is(.dark *){--tw-bg-opacity:1;background-color:rgb(148 163 184/var(--tw-bg-opacity,1))}.dark\:fill-white\/60:is(.dark *){fill:hsla(0,0%,100%,.6)}.dark\:fill-white\/70:is(.dark *){fill:hsla(0,0%,100%,.7)}.dark\:stroke-white\/60:is(.dark *){stroke:hsla(0,0%,100%,.6)}.dark\:text-\[rgba\(255\2c 255\2c 255\2c 0\.5\)\]:is(.dark *){color:hsla(0,0%,100%,.5)}.dark\:text-blue-400:is(.dark *){--tw-text-opacity:1;color:rgb(96 165 250/var(--tw-text-opacity,1))}.dark\:text-gray-100:is(.dark *){--tw-text-opacity:1;color:rgb(243 244 246/var(--tw-text-opacity,1))}.dark\:text-gray-200:is(.dark *){--tw-text-opacity:1;color:rgb(229 231 235/var(--tw-text-opacity,1))}.dark\:text-gray-300:is(.dark *){--tw-text-opacity:1;color:rgb(209 213 219/var(--tw-text-opacity,1))}.dark\:text-gray-400:is(.dark *){--tw-text-opacity:1;color:rgb(156 163 175/var(--tw-text-opacity,1))}.dark\:text-primary-400:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:text-secondary-100:is(.dark *){--tw-text-opacity:1;color:rgb(241 245 249/var(--tw-text-opacity,1))}.dark\:text-secondary-300:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.dark\:text-secondary-400:is(.dark *){--tw-text-opacity:1;color:rgb(148 163 184/var(--tw-text-opacity,1))}.dark\:text-secondary-600:is(.dark *){--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.dark\:text-slate-200:is(.dark *){--tw-text-opacity:1;color:rgb(226 232 240/var(--tw-text-opacity,1))}.dark\:text-white:is(.dark *){--tw-text-opacity:1;color:rgb(255 255 255/var(--tw-text-opacity,1))}.dark\:shadow-gray-800\/50:is(.dark *){--tw-shadow-color:rgba(31,41,55,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:shadow-gray-900\/50:is(.dark *){--tw-shadow-color:rgba(17,24,39,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:ring-gray-700:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(55 65 81/var(--tw-ring-opacity,1))}.dark\:ring-secondary-600:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(71 85 105/var(--tw-ring-opacity,1))}.dark\:hover\:border-secondary-600:hover:is(.dark *){--tw-border-opacity:1;border-color:rgb(71 85 105/var(--tw-border-opacity,1))}.dark\:hover\:bg-gray-700:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(55 65 81/var(--tw-bg-opacity,1))}.dark\:hover\:bg-gray-700\/80:hover:is(.dark *){background-color:rgba(55,65,81,.8)}.dark\:hover\:bg-gray-800:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:hover\:bg-secondary-700:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(51 65 85/var(--tw-bg-opacity,1))}.dark\:hover\:text-accent-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(251 191 36/var(--tw-text-opacity,1))}.dark\:hover\:text-gray-200:hover:is(.dark *){--tw-text-opacity:1;color:rgb(229 231 235/var(--tw-text-opacity,1))}.dark\:hover\:text-green-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(74 222 128/var(--tw-text-opacity,1))}.dark\:hover\:text-primary-300:hover:is(.dark *){--tw-text-opacity:1;color:rgb(125 211 252/var(--tw-text-opacity,1))}.dark\:hover\:text-primary-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:hover\:text-secondary-300:hover:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.dark\:focus\:bg-gray-700:focus:is(.dark *){--tw-bg-opacity:1;background-color:rgb(55 65 81/var(--tw-bg-opacity,1))}.dark\:focus\:bg-gray-800:focus:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:focus\:text-primary-400:focus:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:focus\:ring-offset-gray-800:focus:is(.dark *){--tw-ring-offset-color:#1f2937}.dark\:focus\:ring-offset-gray-900:focus:is(.dark *){--tw-ring-offset-color:#111827}.dark\:focus\:ring-offset-secondary-800:focus:is(.dark *){--tw-ring-offset-color:#1e293b}.dark\:focus\:ring-offset-secondary-900:focus:is(.dark *){--tw-ring-offset-color:#0f172a}.group:hover .dark\:group-hover\:fill-white\/75:is(.dark *){fill:hsla(0,0%,100%,.75)}.group:hover .dark\:group-hover\:text-primary-400:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}@media (min-width:640px){.sm\:col-span-1{grid-column:span 1/span 1}.sm\:mx-1{margin-left:.25rem;margin-right:.25rem}.sm\:mx-2{margin-left:.5rem;margin-right:.5rem}.sm\:inline{display:inline}.sm\:max-w-screen-sm{max-width:640px}.sm\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.sm\:p-8{padding:2rem}.sm\:px-5{padding-left:1.25rem;padding-right:1.25rem}.sm\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\:px-8{padding-left:2rem;padding-right:2rem}.sm\:py-10{padding-bottom:2.5rem;padding-top:2.5rem}.sm\:py-3{padding-bottom:.75rem;padding-top:.75rem}}@media (min-width:768px){.md\:col-span-1{grid-column:span 1/span 1}.md\:h-96{height:24rem}.md\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.md\:grid-cols-4{grid-template-columns:repeat(4,minmax(0,1fr))}.md\:text-4xl{font-size:2.25rem;line-height:2.5rem}}@media (min-width:1024px){.lg\:invisible{visibility:hidden}.lg\:absolute{position:absolute}.lg\:left-0{left:0}.lg\:col-span-1{grid-column:span 1/span 1}.lg\:col-span-2{grid-column:span 2/span 2}.lg\:col-span-3{grid-column:span 3/span 3}.lg\:row-span-3{grid-row:span 3/span 3}.lg\:row-span-4{grid-row:span 4/span 4}.lg\:row-span-6{grid-row:span 6/span 6}.lg\:mt-2{margin-top:.5rem}.lg\:block{display:block}.lg\:inline{display:inline}.lg\:flex{display:flex}.lg\:hidden{display:none}.lg\:h-\[496px\]{height:496px}.lg\:w-48{width:12rem}.lg\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.lg\:grid-cols-4{grid-template-columns:repeat(4,minmax(0,1fr))}.lg\:grid-cols-5{grid-template-columns:repeat(5,minmax(0,1fr))}.lg\:grid-cols-\[auto_1fr_auto\]{grid-template-columns:auto 1fr auto}.lg\:flex-row{flex-direction:row}.lg\:items-center{align-items:center}.lg\:gap-2{gap:.5rem}.lg\:rounded-lg{border-radius:.5rem}.lg\:p-6{padding:1.5rem}.lg\:px-8{padding-left:2rem;padding-right:2rem}.lg\:py-2{padding-bottom:.5rem;padding-top:.5rem}.lg\:pl-0{padding-left:0}.lg\:text-3xl{font-size:1.875rem;line-height:2.25rem}.lg\:text-5xl{font-size:3rem;line-height:1}.lg\:opacity-0{opacity:0}.lg\:shadow-lg{--tw-shadow:0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);--tw-shadow-colored:0 10px 15px -3px var(--tw-shadow-color),0 4px 6px -4px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.lg\:ring-1{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.lg\:ring-black{--tw-ring-opacity:1;--tw-ring-color:rgb(0 0 0/var(--tw-ring-opacity,1))}.lg\:ring-opacity-5{--tw-ring-opacity:0.05}.group:focus-within .lg\:group-focus-within\:visible{visibility:visible}.group:hover .lg\:group-hover\:visible{visibility:visible}.group:hover .lg\:group-hover\:opacity-100{opacity:1}.dark\:lg\:shadow-gray-900\/50:is(.dark *){--tw-shadow-color:rgba(17,24,39,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:lg\:ring-gray-700:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(55 65 81/var(--tw-ring-opacity,1))}}.gh-post-upgrade-cta{align-items:center;color:#fff;display:flex;flex-direction:column;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;font-size:16px;text-align:center;width:100%}.gh-post-upgrade-cta-content{border-radius:8px;padding:40px 4vw}.gh-post-upgrade-cta h2{color:#fff;font-size:28px;letter-spacing:-.2px;margin:0;padding:0}.gh-post-upgrade-cta p{margin:20px 0 0;padding:0}.gh-post-upgrade-cta small{font-size:16px;letter-spacing:-.2px}.gh-post-upgrade-cta a{cursor:pointer;font-weight:500}.gh-post-upgrade-cta a,.gh-post-upgrade-cta a:hover{box-shadow:none;color:#fff;text-decoration:underline}.gh-post-upgrade-cta a:hover{opacity:.8}.gh-post-upgrade-cta a.gh-btn{background:#fff;border-radius:4px;display:block;font-size:16px;font-weight:600;margin:28px 0 0;padding:8px 18px;text-decoration:none}.gh-post-upgrade-cta a.gh-btn:hover{opacity:.92}#search.loading{pointer-events:none}#search.loading i,#search.loading span,#search.loading svg{opacity:0;visibility:hidden}#search.loading:before{animation:search-loading-spin .8s linear infinite;border:3px solid rgba(0,0,0,.1);border-radius:50%;border-top-color:currentcolor;height:20px;margin:-10px 0 0 -10px;width:20px;z-index:1}#search.loading:after,#search.loading:before{content:"";left:50%;position:absolute;top:50%}#search.loading:after{animation:search-loading-pulse 1.5s ease-in-out infinite;background:rgba(0,0,0,.05);border-radius:50%;height:32px;margin:-16px 0 0 -16px;width:32px}@keyframes search-loading-spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}@keyframes search-loading-pulse{0%,to{opacity:.5;transform:scale(.8)}50%{opacity:.8;transform:scale(1.2)}}
JavaScript 类继承使用 extends & super 在本教程中,您将学习如何在 ES6 使用 extends
和 super
实现 JavaScript 继承。
使用 extends 和 super 实现 JavaScript 继承 在 ES6 之前,实现适当的继承需要多个步骤。最常用的策略之一是原型继承 。
下面说明了如何使用原型继承技术,使 Bird
继承 Animal
属性:
function Animal(legs) {
this.legs = legs;
}
Animal.prototype.walk = function() {
console.log('walking on ' + this.legs + ' legs');
}
function Bird(legs) {
Animal.call(this, legs);
}
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Animal;
Bird.prototype.fly = function() {
console.log('flying');
}
var pigeon = new Bird(2);
pigeon.walk(); // walking on 2 legs
pigeon.fly(); // flying
ES6 通过使用 extends
和 super
关键词简化了这些步骤。
下面的例子定义 Animal
和 Bird
类,并通过 extends
和 super
关键词建立继承关系。
class Animal {
constructor(legs) {
this.legs = legs;
}
walk() {
console.log('walking on ' + this.legs + ' legs');
}
}
class Bird extends Animal {
constructor(legs) {
super(legs);
}
fly() {
console.log('flying');
}
}
let bird = new Bird(2);
bird.walk();
bird.fly();
代码是如何运行的。
首先,使用 extends
关键词使 Bird
类继承 Animal
类:
class Bird extends Animal { // ...}
Animal
类称为基类 或父类 ,而 Bird
类称为派生类 或子类 。通过 extends 关键词,Bird
类继承 Animal
类的所有方法和属性。
其次,在 Bird
的构造函数调用 super()
并使用 legs
参数调用 Animal
的构造函数。
如果子类在有构造函数,JavaScript 在情况下要求调用 super()
。正如您在 Bird
类中看到的,这 super(legs)
等同于 ES5 的以下语句:
Animal.call(this, legs);
如果 Bird
类没有构造函数,则无需执行任何其他操作:
class Bird extends Animal {
fly() {
console.log('flying');
}
}
它等效于以下类声明:
class Bird extends Animal {
constructor(...args) {
super(...args);
}
fly() {
console.log('flying');
}
}
但是,子类有一个构造函数,它需要调用 super()
。例如,以下代码会导致错误:
class Bird extends Animal {
constructor(legs) {
}
fly() {
console.log('flying');
}
}
错误:
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
因为 super()
初始化 this
对象,所以需要在访问 this
对象之前调用 super()
。在调用 super()
之前尝试访问 this
也会导致错误。
例如,如果你想初始化 Bird
类的属性 color
,你可以这样做:
class Bird extends Animal {
constructor(legs, color) {
super(legs);
this.color = color;
}
fly() {
console.log("flying");
}
getColor() {
return this.color;
}
}
let pegion = new Bird(2, "white");
console.log(pegion.getColor());
影子方法 ES6 允许子类和父类有同名的方法。在这种情况下,当你调用子类对象的方法时,子类中的方法会遮蔽父类的方法。
下面的 Dog
类扩展 Animal
类并重新定义 walk()
方法:
class Dog extends Animal {
constructor() {
super(4);
}
walk() {
console.log(`go walking`);
}
}
let bingo = new Dog();
bingo.walk(); // go walking
要在子类中调用父类的方法,可以这样调用 super.method(arguments)
:
class Dog extends Animal {
constructor() {
super(4);
}
walk() {
super.walk();
console.log(`go walking`);
}
}
let bingo = new Dog();
bingo.walk();
// walking on 4 legs
// go walking
继承静态成员 除了属性和方法外,子类还继承父类的所有静态属性和方法。例如:
class Animal {
constructor(legs) {
this.legs = legs;
}
walk() {
console.log('walking on ' + this.legs + ' legs');
}
static helloWorld() {
console.log('Hello World');
}
}
class Bird extends Animal {
fly() {
console.log('flying');
}
}
在此示例中,Animal
类有静态方法 helloWorld()
,并且此方法 Bird.helloWorld()
与 Animal.helloWorld()
方法的行为相同:
Bird.helloWorld(); // Hello World
从内置类型继承 JavaScript 允许您通过继承扩展内置类型,例如 Array 、 String 、Map 和 Set 。
下面 Queue
类扩展引用类型 Array
。语法比 Queue
使用构造函数/原型模式 实现的要简洁得多。
class Queue extends Array {
enqueue(e) {
super.push(e);
}
dequeue() {
return super.shift();
}
peek() {
return !this.empty() ? this[0] : undefined;
}
empty() {
return this.length === 0;
}
}
var customers = new Queue();
customers.enqueue('A');
customers.enqueue('B');
customers.enqueue('C');
while (!customers.empty()) {
console.log(customers.dequeue());
}
结论 在 ES6 使用 extends 关键词实现继承。要扩展的类称为基类或父类。扩展基类或父类的类称为派生类或子类。 super(arguments)
在子类的构造函数中调用调用父类的构造函数。使用 super
关键词在子类的方法中调用父类的方法。 © 2025 myfreax. All rights reserved.