[Javascript] ES6 Class Constructors and the Super Keyword
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 class. constructors 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的更多相关文章
- [转]JavaScript ES6 class指南
[转]JavaScript ES6 class指南 前言 EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建 ...
- JavaScript es6 class类的理解。
本着互联网的分享精神,在本篇文章我将会把我对JavaScript es6 class类的理解分享给大家. JavaScript 类主要是 JavaScript 现有的基于原型的继承的语法糖. 类语法 ...
- JavaScript ES6中export及export default的区别
相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在JavaScript ES6中,export与export default均可用于导出常量.函 ...
- JavaScript ES6 新特性详解
JavaScript ES6 带来了新的语法和新的强大功能,使您的代码更现代,更易读 const , let and var 的区别: const , let 是 ES6 中用于声明变量的新关键字. ...
- JavaScript ES6 核心功能一览
JavaScript 在过去几年里发生了很大的变化.这里介绍 12 个你马上就能用的新功能. JavaScript 历史 新的语言规范被称作 ECMAScript 6.也称为 ES6 或 ES2015 ...
- JavaScript ES6中export及export default的区别以及import的用法
本文原创地址链接:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632,未经博主允许不得转载. 相信很多人都使用过export.e ...
- JavaScript ES6 核心功能一览(转)
原文地址:Overview of JavaScript ES6 features (a.k.a ECMAScript 6 and ES2015+) 原文作者:Adrian Mejia 译文出自:掘金翻 ...
- JavaScript ES6 promiss的理解。
本着互联网的分享精神,我将我对promise的理解分享给大家. JavaScript ES6的promise方法主要应用在处理异步函数返回的结果,注意他不是将异步函数转换为同步函数,而是等异步函数有结 ...
- JavaScript ES6功能概述(ECMAScript 6和ES2015 +)
JavaScript在过去几年中发生了很大的变化.这些是您今天可以开始使用的12项新功能! 该语言的新增内容称为ECMAScript 6.它也称为ES6或ES2015 +. 自1995年JavaScr ...
随机推荐
- JMeter扩展插件实现对自定义协议进行支持 转
本文版权归xmeter.net 所有.欢迎转载,转载请注明出处. 摘要## JMeter本身提供了插件机制,允许第三方扩展JMeter以支持JMeter不支持的协议的测试.本文以扩展一个简单的Apac ...
- malloc,free,calloc,realloc函数
malloc函数 原型:extern void* malloc(unsigned int size): 功能:动态分配内存: 注意:size仅仅为申请内存字节大小,与申请内存块中存储的数据类型无关,故 ...
- 【学习笔记】Docker基础
基本概念 Docker是什么? Docker是一种基于Golang开发的虚拟化技术,开发人员和系统管理员使用容器开发,部署和运行应用程序的平台. 使用Linux容器部署应用程序称为容器化. 容器不是新 ...
- python实现Huffman编码
一.问题 利用二叉树的结构对Huffman树进行编码,实现最短编码 二.解决 # 构建节点类 class TreeNode: def __init__(self, data): "" ...
- Java学习:接口(interface)的使用于注意事项
接口 接口就是一种公共的规范标准.只要符合规范标准,就可以大家通用. 接口就是多个类的公共规范.接口是一种引用数据类型,最重要的内容就是其中的:抽象方法. 如何定义一个接口的格式 如何定义一个接口的格 ...
- Mysql系列(一)—— 基于5.7.22 解压版下载、安装、配置和卸载
1.下载 从官网中直接获取自己想要的版本: MySQL Community Server 5.7.22 2.解压 将下载到的文件解压缩到自己喜欢的位置. 与mysql 5.6不同的是5.7版本中没有d ...
- Asp.Net Core Web Api 使用 Swagger 生成 api 说明文档
最近使用 Asp.Net Core Web Api 开发项目服务端.Swagger 是最受欢迎的 REST APIs 文档生成工具之一,进入我的视野.以下为学习应用情况的整理. 一.Swagger 介 ...
- Python语言控制运算的优先级
Python语言碰上计算式同时出现在一个指令内时,除了括号"(".")"最优外,其余计算优先次序如下: 次方(**). 乘法.除法.求余数(%).求整数(//) ...
- English--定语从句
English|定语从句 从介绍从句开始,英语的句子已经开始逐渐复杂了!做好心理准备,三大从句介绍完毕,会介绍分词短语.废话不多说,直接开始干! 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...
- ES6 字符串&正则表达式
目录 第二章 字符串和正则表达式UTF-16码位codePointAt()方法String.fromCodePoint()方法normalize()方法正则表达式u修饰符其他字符串变更字符串中的字串识 ...