[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 ...
随机推荐
- 2019 ICPC 银川站
I. Base62(高精度进制转换) 比赛当时雷菊苣和队长俩人拿着大数板子摸了一百多行(然后在缺少大数板子的情况下雷菊苣一发过了orz) 今天补题随便摸了个高精度进制转换的板子交上去就过了还贼短,, ...
- Django 安装使用
Django 安装使用 Django 介绍: Django是一个开放源代码的Web应用框架,由Python写成.采用了MVT的框架模式,即模型M,视图V和模版T.它最初是被开发来用于管理劳伦斯出版集团 ...
- 《MySQL实战45讲》学习笔记2——MySQL的日志系统
一.日志类型 逻辑日志:存储了逻辑SQL修改语句 物理日志:存储了数据被修改的值 二.binlog 1.定义 binlog 是 MySQL 的逻辑日志,也叫二进制日志.归档日志,由 MySQL Ser ...
- Spring Boot 整合 MyBatis 实现乐观锁和悲观锁
本文以转账操作为例,实现并测试乐观锁和悲观锁. 完整代码:https://github.com/imcloudfloating/Lock_Demo GitHub Page:http://blog.cl ...
- C#读写修改设置调整UVC摄像头画面-全景
有时,我们需要在C#代码中对摄像头的全景进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...
- MVC学习笔记(六)---遇到的小问题汇总
一.MVC中Controller中返回两个对象的写法如下: , msg = "成功", user = user, userInfo = person }); 二.前台向后台传入带有 ...
- Golang逃逸分析
Golang逃逸分析 介绍逃逸分析的概念,go怎么开启逃逸分析的log. 以下资料来自互联网,有错误之处,请一定告之. sheepbao 2017.06.10 什么是逃逸分析 wiki上的定义 In ...
- JavaScript---js语法,数据类型及方法, 数组及方法,JSON对象及方法,日期Date及方法,正则及方法,数据类型转换,运算符, 控制流程(三元运算),函数(匿名函数,自调用函数)
day46 一丶javascript介绍 JavaScript的基础分为三个 1.ECMAScript:JavaScript的语法标准.包括变量,表达式,运算符,函数,if语句,for语句 ...
- Vue学习之基础及部分指令小结(一)
一.理解MVC和MVVM的关系: MVC:Model View Controller (模型 视图 控制器) 分别为:业务逻辑.界面.用来调度View和Model层 MVVM:Model View V ...
- React学习笔记②
import React,{Component} from 'react'; import Child from './Child.js' class App extends Component{ c ...