何时重要的props是super(),为什么?
class MyComponent extends React.Component {
constructor(props) {
super(); // or super(props) ?
}
}
Run Code Online (Sandbox Code Playgroud) 从文档学习React 并遇到了这个例子:
class Square extends React.Component {
constructor() {
super();
this.state = {
value: null,
};
}
...
}
Run Code Online (Sandbox Code Playgroud)
根据Mozilla,super允许您this在构造函数中使用.有没有其他理由单独使用一个站点super(我知道也super允许你访问父类的方法)但是React是否有任何其他只能super()自己调用的用例?
我正在玩JavaScript/ES6中的新东西.我得到了Uncaught ReferenceError: this is not defined(...) player.js:5我的代码.据我所知,这里没有错误!这是一个错误吗?任何解决方法?
的index.html
<html>
<head>
<script type="text/javascript" src="js/entity.js"></script>
<script type="text/javascript" src="js/player.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
<title>Test</title>
</head>
<body>
<canvas id="screen" width=500 height=500></canvas>
<script type="text/javascript">initialize();</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
entity.js
"use strict";
class Entity {
constructor() {
console.log("Entity");
}
}
Run Code Online (Sandbox Code Playgroud)
player.js
"use strict";
class Player extends Entity {
constructor() {
console.log("Created"); // <- error here
}
}
Run Code Online (Sandbox Code Playgroud) ECMAScript 2015规范提到关键字(或单词?)new.target正好3次 - 在14.2.3中 1次:
通常,Contains不会查看大多数函数表单但是,Contains用于检测ArrowFunction中的new.target,this和super用法.
在14.2.16中两次:
ArrowFunction不为arguments,super,this或new.target定义本地绑定.对ArrowFunction中的参数,super,this或new.target的任何引用 都必须解析为词汇封闭环境中的绑定
MDN提到它,但非常模糊,页面不完整.
巴别塔似乎不支持它.尝试在函数(箭头或其他)中使用new.target时出现语法错误.
它是什么,它应该如何使用?
我正在使用ES6类,而我的类(A)扩展了类B,类B扩展了类C。A如何扩展一个方法,然后调用该方法的C版本。
class C {
constructor() {
console.log('class c');
}
}
class B extends C {
constructor() {
super()
console.log('no, I don't want this constructor.');
}
}
class A extends B {
constructor() {
// What should I be doing here? I want to call C's constructor.
super.super();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢,我将停止尝试做这个愚蠢的事情。在我的情况下,代码重用的次要收获不值得杂技。
我正在使用Gulp运行babelify 7.2.0并且我在以下代码中收到错误:
class One {}
class Two extends One {
constructor() {
this.name = 'John';
}
}
Run Code Online (Sandbox Code Playgroud)
以下是错误的关键:
SyntaxError: [the file path in question]: 'this' is not allowed before super()
20 | class Two extends One {
21 | constructor() {
> 22 | this.name = 'John';
| ^
23 | }
24 | }
25 |
Run Code Online (Sandbox Code Playgroud)
在我看来,这不应该被解雇,因为我根本没有super在构造函数中进行任何调用,因此没有冲突的风险.我已经在Github上提交了一个问题,但我想知道是否有办法可以在同一时间关闭它.
我对所有扩展抽象类的子类使用依赖项注入。
在抽象构造函数类中,如果需要的话,我启动了一个计划在其子级中覆盖的方法。
我陷入了一个问题,即从super启动的重写类中看不到注入的依赖项。
这是代码示例:
abstract class Base {
constructor(view: string) {
this._assemble();
}
protected _assemble(): void {
console.log("abstract assembling for all base classes");
}
}
class Example extends Base {
constructor(view: string, private helper: Function) {
super(view);
console.log(this.helper);
}
public tryMe(): void {
this._assemble();
}
protected _assemble(): void {
super._assemble();
// at first run this.helper will be undefined!
console.log("example assembling", this.helper);
}
}
let e = new Example("hoho", function () { return; })
console.log("So now i will try to reassemble..."); …Run Code Online (Sandbox Code Playgroud) 编辑:这个问题不同于如何扩展一个类而不必在 ES6 中使用超级?- 虽然答案是相关的,但这显然是一个不同的问题。它涉及到一个特定的错误,并且两大类参与Person和CreationEvent实际上并不相互继承。
我有两个 ES6 类,Person并且CreationEvent(CreationEvent继承自Event)。我希望new CreationEvent在我制作 a时制作 a new Person(因为这CreationEvent是个人帐户历史中事件的一部分)。
运行new CreationEvent()它自己的工作正常。但是我不能运行new Person()。
即使使用简化版本的代码仍然失败:
class Event {
constructor() {
this.time = Date.now()
this.tags = []
}
}
class CreationEvent extends Event {
constructor() {
this.description = "Created"
}
}
class Person {
constructor(givenName, familyName, email) {
var creationEvent = new CreationEvent()
}
} …Run Code Online (Sandbox Code Playgroud) ecmascript-6 ×7
javascript ×7
reactjs ×2
babeljs ×1
constructor ×1
es6-class ×1
gulp ×1
transpiler ×1
typescript ×1