When the ES6 class shipped back in 2015, a number of additional keywords came with it. Two of these are constructor and super. Both of these are specific to the class keyword and make working with classes manageable. Both are utilized when the new keyword is used to create a new instance of a classconstructors are called initially with the new keyword and super is how a subclass can utilize it's parent's methods (like it's parent's constructor function) within that child class

You have to call super() function when you extends one class:

class Rectangle {
constuctor(height, width) {
this.name = 'Rectangle'
this.height = height
this.width = width
}
} class Square extends Rectangle {
constructor(length) {
super(length, length)
this.name = 'Square'
}
} const myShape = new Square(1) console.log(myShape)

Again, even if our rectangle class did not have a constructor, we'd still need to call super within the square's constructor because it is required when we're working with subclasses that have constructors and when the this keyword is used in the constructor.

class Rectangle {

}

class Square extends Rectangle {
constructor(length) {
super()
this.name = 'Square'
}
}

[Javascript] ES6 Class Constructors and the Super Keyword的更多相关文章

  1. [转]JavaScript ES6 class指南

    [转]JavaScript ES6 class指南 前言 EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建 ...

  2. JavaScript es6 class类的理解。

    本着互联网的分享精神,在本篇文章我将会把我对JavaScript  es6 class类的理解分享给大家. JavaScript 类主要是 JavaScript 现有的基于原型的继承的语法糖. 类语法 ...

  3. JavaScript ES6中export及export default的区别

    相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在JavaScript ES6中,export与export default均可用于导出常量.函 ...

  4. JavaScript ES6 新特性详解

    JavaScript ES6 带来了新的语法和新的强大功能,使您的代码更现代,更易读 const ,  let and var 的区别: const , let 是 ES6 中用于声明变量的新关键字. ...

  5. JavaScript ES6 核心功能一览

    JavaScript 在过去几年里发生了很大的变化.这里介绍 12 个你马上就能用的新功能. JavaScript 历史 新的语言规范被称作 ECMAScript 6.也称为 ES6 或 ES2015 ...

  6. JavaScript ES6中export及export default的区别以及import的用法

    本文原创地址链接:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632,未经博主允许不得转载. 相信很多人都使用过export.e ...

  7. JavaScript ES6 核心功能一览(转)

    原文地址:Overview of JavaScript ES6 features (a.k.a ECMAScript 6 and ES2015+) 原文作者:Adrian Mejia 译文出自:掘金翻 ...

  8. JavaScript ES6 promiss的理解。

    本着互联网的分享精神,我将我对promise的理解分享给大家. JavaScript ES6的promise方法主要应用在处理异步函数返回的结果,注意他不是将异步函数转换为同步函数,而是等异步函数有结 ...

  9. JavaScript ES6功能概述(ECMAScript 6和ES2015 +)

    JavaScript在过去几年中发生了很大的变化.这些是您今天可以开始使用的12项新功能! 该语言的新增内容称为ECMAScript 6.它也称为ES6或ES2015 +. 自1995年JavaScr ...

随机推荐

  1. 【webrtc】webrtc的rtp重传代码分析

    pgm不太能用,没有想象中的可靠,重传机制貌似仍然使用组播重传,丢包率80%的网络感觉没啥改进,如果有所好转延迟估计也是个不小的问题. 后听说rtp也有nack机制,webrtc基于rtp实现了重传在 ...

  2. python3 根据时间获取本月一号和月末日期

    一.概述 有一个统计报表需求,需要知道上个月的第一天和最后一天,来进行上个月的数据统计. 二.代码实现 #!/usr/bin/env python3 # coding: utf-8 import ca ...

  3. 「UNR#1」奇怪的线段树

    「UNR#1」奇怪的线段树 一道好题,感觉解法非常自然. 首先我们只需要考虑一次染色最下面被包含的那些区间,因为把无解判掉以后只要染了一个节点,它的祖先也一定被染了.然后发现一次染色最下面的那些区间一 ...

  4. 什么是SQL ?

    SQL 1.什么是SQL ? Structured Query Languange:结构化查询语言 其实就是定义了操作所有关系型数据库的规则.每一种数据库操作的方式存在不一样的地方,称为“方言”. 2 ...

  5. laravel 查询随机数据

    laravel 中 随机搜索数据 $tweet = Tweet::where('id',$id) ->orderBy(\DB::raw('RAND()')) ->take(1) -> ...

  6. 关于Java链接c#的webapi的注意事项

    最近写了一个关于ad域的项目,ad域我也是第一次接触,对ad域的总结我会晚一些时间写出来.在此我先总结一下关于Java调用c#的webapi的一个注意点. [HttpPost] public Dict ...

  7. C#使用表达式树动态调用方法并实现99乘法表

    我们在使用C#编程的时候,经常使用反射来动态调用方法,但有时候需要动态的生成方法,下面介绍使用表达式树的方式来自动生成方法,并调用. 首先需要说明什么是表达式,熟悉Linq的程序猿都用过类似于下面的代 ...

  8. 象棋中“车”的攻击范围_C#

    如题: var a = new String[8,8]; int h, l; Console.WriteLine("输入车所在的行(0-7):"); h = int.Parse(C ...

  9. Restart container within pod

    https://stackoverflow.com/questions/46123457/restart-container-within-pod 命令 kubectl exec -it [POD_N ...

  10. 关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)

    关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能:可能和其他插件有冲突)