作为javascript开发,简述super()和super(props)有什么区别?


在JavaScript中,`super()`是一个关键字,用于调用父类的构造函数。在类继承中,子类在创建实例时,会自动调用父类的构造函数,但是父类的构造函数中并不能访问子类的实例变量,这时就需要通过`super()`来调用父类的构造函数。 `super()`的使用方式主要有两种: 1. 在子类的构造函数中调用父类的构造函数,不传任何参数: ```javascript class Parent { constructor(name) { this.name = name; } } class Child extends Parent { constructor(name, age) { super(name); // 调用父类的构造函数 this.age = age; } } ``` 2. 在子类的构造函数中调用父类的构造函数,并传入参数: ```javascript class Parent { constructor(name) { this.name = name; } } class Child extends Parent { constructor(name, age) { super(name, age); // 调用父类的构造函数,并传入参数 this.age = age; } } ``` 在第一种情况下,`super()`调用的是父类的构造函数,而没有传入任何参数,所以在子类的构造函数中,`this.name`是父类的实例变量,`this.age`是子类的实例变量。 在第二种情况下,`super()`调用的是父类的构造函数,并传入了参数,所以`this.name`是父类的实例变量,`this.age`是子类的实例变量。 `super()`的作用是调用父类的构造函数,如果在子类的构造函数中调用父类的构造函数,并传入参数,那么`super()`会调用父类的构造函数,并传入相同的参数。 [2023-11-09 01:43:06 | AI写代码神器 | 396点数解答]
- 作为javascript开发,简述super()和super(props)有什么区别?(396点数解答 | 2023-11-09 01:43:06)200
- 作为javascript开发,react ⾼阶组件、render props、hooks 有什么区别,为什么要 不断迭代 ?(354点数解答 | 2023-11-09 01:52:18)264
- 作为javascript开发,react 16.x 中 props 改变后在哪个⽣命周期中处理 ?(153点数解答 | 2023-11-09 01:52:54)215
- 作为javascript开发,简述state 和 props 触发更新的⽣命周期分别有什么区别? ?(206点数解答 | 2023-11-09 01:53:03)297
- subroutine vumat &(amat, stress, statev, ddsdde, sse, spd, scd, rpl, &ddsddt, drpl, dt, cmname, nblock, ndir, nshr, nstatev, props, &coords, dvolume, temp, dtemp, predef, nload, lanneal, isw, noel, &const, np, nprops, m, n, nn, mat, nstatec, nconst, ithermal, &tempold, ddsddeold, drplold, dtold, ipredef, ithermalold) implicit none !include 'vaba_param.inc' ! define constants real*8, parameter :: zero = 0.0d0, one = 1.0d0, two = 2.0(2664点数解答 | 2024-08-17 10:19:58)164
- 作为javascript开发,简述 javascript 盒子模型?(264点数解答 | 2023-11-09 00:57:12)405
- 作为javascript开发,简述 javascript 中的高阶函数是什么?(266点数解答 | 2023-11-09 01:03:58)193
- 作为**工程师,简述 loadrunner 分哪三部分?(145点数解答 | 2023-11-09 18:25:29)257
- 作为**工程师,简述 iphone全机型分辨率和尺寸 ?(360点数解答 | 2023-11-09 18:31:43)287
- 在ios开发中,当我们释放我们的对象时,为什么需要调用[super dealloc]方法,它的位置又是如何的呢?(313点数解答 | 2023-11-09 17:37:49)176
- 在ios开发中,解释self = [super init]方法?(381点数解答 | 2023-11-09 17:38:07)235
- package net.mooctest; import java.util.*; public class astar extends searchalgorithm { public astar(graph graph, node startnode, node endnode, vehicle vehicle, trafficcondition trafficcondition, weathercondition weathercondition, int currenttime) { super(graph, startnode, endnode, vehicle, trafficcondition, weathercondition, currenttime); } public double heuristic(node node) { double distancetoend = math.abs(node.getnodeid() - endnode.getnodeid())(688点数解答 | 2024-10-27 10:06:00)154