相关疑难解决方法(0)

使用ES6语法和Babel扩展Javascript中的错误

我试图用ES6和Babel扩展Error.它没有成功.

class MyError extends Error {
  constructor(m) {
    super(m);
  }
}

var error = new Error("ll");
var myerror = new MyError("ll");
console.log(error.message) //shows up correctly
console.log(myerror.message) //shows empty string
Run Code Online (Sandbox Code Playgroud)

Error对象永远不会获得正确的消息集.

试试Babel REPL.

现在我已经在SO上看到了一些解决方案(例如这里),但它们看起来都非常非ES6-y.如何以漂亮的ES6方式做到这一点?(那是在Babel工作)

javascript transpiler ecmascript-6 babeljs

128
推荐指数
6
解决办法
5万
查看次数

如何在JavaScript中创建抽象基类?

是否可以在JavaScript中模拟抽象基类?最优雅的方式是什么?

说,我想做一些事情: -

var cat = new Animal('cat');
var dog = new Animal('dog');

cat.say();
dog.say();
Run Code Online (Sandbox Code Playgroud)

应输出:'bark','meow'

javascript oop abstract

102
推荐指数
11
解决办法
9万
查看次数

如何在JavaScript中创建无法实例化的抽象基类

我上课了

function Node() {
    //implementation
}
Run Code Online (Sandbox Code Playgroud)

和另一堂课

function AttributionalNode() {
    this.prototype.setAttr = function (attr) {
        this.atText = attr;
    };
}

AttributionalNode.prototype = new Node();
AttributionalNode.prototype.constructor = AttributionalNode;
Run Code Online (Sandbox Code Playgroud)

如何使类Node()无法实例化?比如我试试的时候

var node = new Node();
Run Code Online (Sandbox Code Playgroud)

所以它引发了一个例外?

javascript inheritance design-patterns

31
推荐指数
3
解决办法
8238
查看次数

什么是"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 中创建类似抽象类的方法。到目前为止,我尝试的一切总是遇到语言和/或其语法的限制(也是我对原型设计的有限知识)。

基本的面向对象编程;我们声明一个类并扩展它。最终类必须访问其超类的某些字段和方法,但不是全部。它还改变了公共方法......

类声明应该是完美的封装,因此除了这段代码之外没有其他东西能够到达它(类似于命名空间)。


到目前为止,我在 ES5 中的实验是错误的......我非常感谢一些建议和帮助。

(function(){

    // ==================================

    function AbstractClass(params) {
        var _myParams = params;
        var _privateField = "Only AbstractClass can see me";
        this.publicField = "Everybody can see me";

        function privateFunc() {
            // Does private stuff
        }
    }
    AbstractClass.prototype.publicFunc = function() {
        // Does public stuff
        privateFunc(); // Works?
    }

    // ==================================

    function FinalClass(params) {
        // How to pass the params to the superclass?
    }
    FinalClass.prototype.publicFunc = function() {
        // Override and calls the superclass.publicFunc()?
        // …
Run Code Online (Sandbox Code Playgroud)

javascript prototype ecmascript-5 ecmascript-6

4
推荐指数
1
解决办法
3400
查看次数

这在超类构造函数调用之前是不允许的

我想使用 super 的构造函数将子类实例传递给超类,但出现此错误

super(this);  
Run Code Online (Sandbox Code Playgroud)

这在超类构造函数调用之前是不允许的

为什么我收到这个错误,我该如何解决这个问题


class Parent
{
    constructor(child)
    {
        this.child = child;
    }

    //...somewhere in code
    //child.doSomething();
}


class Child extends Parent
{
    constructor()
    {
        super(this);   // <==== the error here
    }

    doSomething = () =>
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 es6-class

0
推荐指数
1
解决办法
764
查看次数