es6 class 中 constructor 方法 和 super
function Point(x, y) {
this.x = x;
this.y = y;
//console.log("初始化自动执行");
}
Point.prototype.toString = function() {
return '(' + this.x + ',' + this.y + ')';
}
var p = new Point();
console.log(p);
等同于
class Point{
constructor(x, y) {
this.x = x; //在new Point()对象自身属性
this.y = y;
// console.log("初始化自动执行");
}
hello() { //在new Point()对象prototype上的属性
return '(' + this.x + ',' + this.y + ')';
}
}
var class_p = new Point();
console.log(class_p)
class A {
constructor() {
console.log("父类构造函数")
}
}
class B extends A {
constructor() {
super(); //注意:ES6 要求,子类的构造函数constructor 必须执行一次 super 函数,否则会报错!!!!!!!
}
}
1.在普通方法中,指向父类的原型对象;在静态方法中,指向父类。
class A {
c() {
return 2;
}
} class B extends A {
constructor() {
super();
console.log(super.c()); // 2
}
} let b = new B(); 上面代码中,子类 B 当中的 super.c(),就是将 super 当作一个对象使用。
这时,super 在普通方法之中,指向 A.prototype,所以 super.c() 就相当于 A.prototype.c()。 2.通过 super 调用父类的方法时,super 会绑定子类的 this。
class A {
constructor() {
this.x = 1;
}
s() {
console.log(this.x);
}
} class B extends A {
constructor() {
super();
this.x = 2;
}
m() {
super.s(); //通过 super 调用父类的方法时,super 会绑定子类的 this。
}
} let b = new B();
b.m(); // 2 上面代码中,super.s() 虽然调用的是 A.prototytpe.s(),但是 A.prototytpe.s()会绑定子类 B 的 this,导致输出的是 2,而不是 1。
也就是说,实际上执行的是 super.s.call(this)。 3.由于绑定子类的 this,所以如果通过 super 对某个属性赋值,这时 super 就是 this,赋值的属性会变成子类实例的属性。
class A {
constructor() {
this.x = 1;
}
} class B extends A {
constructor() {
super();
this.x = 2;
super.x = 3;
console.log(super.x); // undefined
console.log(this.x); // 3
}
} let b = new B(); 上面代码中,super.x 赋值为 3,这时等同于对 this.x 赋值为 3。
而当读取 super.x 的时候,调用的是 A.prototype.x(),但并没有 x 方法,所以返回 undefined。 《3》注意
1.由于对象总是继承其他对象的,所以可以在任意一个对象中,使用 super 关键字。 2.ES6 要求,子类的构造函数constructor 《必须执行一次》 super 函数,否则会报错!!!!!!! 3.使用 super 的时候,《必须显式》指定是作为函数,还是作为对象使用,否则会报错
class A {}
class B extends A {
constructor() {
super(); //注意点1,必须执行一次super()函数 console.log(super); // 报错
}
}
es6 class 中 constructor 方法 和 super的更多相关文章
- ES6之class 中 constructor 方法 和 super 的作用
首先,ES6 的 class 属于一种“语法糖”,所以只是写法更加优雅,更加像面对对象的编程,其思想和 ES5 是一致的. function Point(x, y) { this.x = x; thi ...
- 详解es6 class语法糖中constructor方法和super的作用
大多数面向对象的编程语言都支持类和类继承的特性,而JS却不支持这些特性,只能通过其他方法定义并关联多个相似的对象,这种状态一直延续到了ES5.由于类似的库层出不穷,最终还是在ECMAScript 6中 ...
- ES6中class方法及super关键字
ES6 class中的一些问题 记录下class中的原型,实例,super之间的关系 //父类 class Dad { constructor(x, y) { this.x = 5; this.y = ...
- react中constructor()和super()的具体含义以及如何使用
1.constructor()---super( )的基本含义 constructor()--构造方法 这是ES6对类的默认方法,通过new命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的 ...
- react中constructor和super()以及super(props)的区别。
react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...
- ES6语法中的class、extends与super的原理
class 首先, 在JavaScript中, class类是一种函数 class User { constructor(name) { this.name = name; } sayHi ...
- [ES6]react中使用es6语法
前言 不论是React还是React-native,facebook官方都推荐使用ES6的语法,没在项目中使用过的话,突然转换过来会遇到一些问题,如果还没有时间系统的学习下ES6那么注意一些常见的写法 ...
- InvocationHandler中invoke()方法的调用问题
转InvocationHandler中invoke()方法的调用问题 Java中动态代理的实现,关键就是这两个东西:Proxy.InvocationHandler,下面从InvocationHandl ...
- constructor()方法
在做微信小程序的时候,需要对传输的数据进行加密,大牛给我介绍constructor()方法,不是很懂这个但是用了一次,今天来用自己的想法来理解这个方法 ———————————————————————— ...
随机推荐
- bzoj2006: [NOI2010]超级钢琴(堆+RMQ)
和上一道题同类型...都是用堆求第k大 考虑对于每一个r,怎么求出一个最优的l.显然只需要求出前缀和,用RMQ查询前面最小的l的前缀和就好了.但是对于一个r,每个l只能选一次,选了一次之后,考虑怎么把 ...
- BZOJ 3709&&AGC 018 C——多段排序的微扰法
BZOJ 3709• 有n只怪物,你的初始生命值为z.• 为了打败第i只怪物,你需要消耗cost[i]点生命值,但怪物死后会使你恢复val[i]点生命值.• 任何时候你的生命值都不能小于等于0.• 问 ...
- openCV实例:Canny边缘检测
http://blog.sina.com.cn/s/blog_737adf530100z0jk.html 在第一次使用openCV程序成功对图像进行打开后,现在开始试验第二个例程试验:Canny边缘检 ...
- C语言 ------ #undef 的使用
#undef 是在后面取消以前定义的宏定义 该指令的形式为 #undef 标识符 其中,标识符是一个宏名称.如果标识符当前没有被定义成一个宏名称,那么就会忽略该指令. 一旦定义预处理器标识符,它将保持 ...
- Qt ------ stylesheet 样式
1.所有的窗口组件都可以用 setStyleSheet() 设置样式 2.使用样式,显示效果可以不受平台影响,比如保证window 7 和 linux 显示效果是一样的 QVariant 如果 sty ...
- win32/linux 线程 log
原文 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #includ ...
- php网摘收藏
1.thinkphp3.2.3开发手册: http://document.thinkphp.cn/manual_3_2.html 2.ThinkPHP3.2.3的函数汇总:http://www.thi ...
- 题解 P1682 【过家家】
P1682 过家家 题目描述 有2n个小学生来玩过家家游戏,其中有n个男生,编号为1到n,另外n个女生,编号也是1到n.每一个女生可以先选择一个和她不吵嘴的男生来玩,除此之外,如果编号为X的女生的朋友 ...
- Qt每次运行都是重新编译问题
按理说,Qt使用了makefile技术只会编译刚修改的源文件,但有时会遇到一运行项目就会重新编译的问题,严重浪费了时间. 问题就出在你的系统时间上,系统时间的不准确会影响makefile机制的判断过程 ...
- HDU 6061 推导 NTT
复函数,递归代入,可以得到最终的式子为$f(x-\sum_{i=1}^{m}{a_i})$,且$f(x) = \sum_{i = 0}^{n}{c_ix^i}$,求最终各个x项的系数. 设$S=\su ...