相关疑难解决方法(0)

使用es6类时,React中的"super()"和"super(props)"有什么区别?

何时重要的propssuper(),为什么?

class MyComponent extends React.Component {
  constructor(props) {
    super(); // or super(props) ?
  }
}
Run Code Online (Sandbox Code Playgroud)

ecmascript-6 reactjs

493
推荐指数
10
解决办法
18万
查看次数

在React构造函数中调用super()有什么作用?

文档学习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 constructor ecmascript-6 reactjs

60
推荐指数
2
解决办法
3万
查看次数

在类构造函数中"未捕获的ReferenceError:未定义"

我正在玩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)

javascript ecmascript-6

42
推荐指数
1
解决办法
2万
查看次数

什么是"new.target"?

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时出现语法错误.

它是什么,它应该如何使用?

javascript ecmascript-6

25
推荐指数
2
解决办法
6139
查看次数

如何在ES6中调用班级的父级的父级构造函数?

我正在使用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)

编辑:谢谢,我将停止尝试做这个愚蠢的事情。在我的情况下,代码重用的次要收获不值得杂技。

javascript ecmascript-6

5
推荐指数
1
解决办法
542
查看次数

有没有办法在babelify中关闭"超级之前不允许这样的规则"?

我正在使用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上提交了一个问题,但我想知道是否有办法可以在同一时间关闭它.

javascript ecmascript-6 gulp babeljs

3
推荐指数
1
解决办法
1102
查看次数

TypeScript应该在ES5的编译输出中的_super调用之前分配给this?

我对所有扩展抽象类的子类使用依赖项注入。

在抽象构造函数类中,如果需要的话,我启动了一个计划在其子级中覆盖的方法。

我陷入了一个问题,即从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)

javascript transpiler typescript

3
推荐指数
1
解决办法
366
查看次数

在另一个对象的构造函数中创建新的 ES6 对象失败

编辑这个问题不同于如何扩展一个类而不必在 ES6 中使用超级?- 虽然答案是相关的,但这显然是一个不同的问题。它涉及到一个特定的错误,并且两大类参与PersonCreationEvent实际上并不相互继承。

我有两个 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)

javascript ecmascript-6 es6-class

-2
推荐指数
1
解决办法
1181
查看次数