无法使用ES6/babel-node进行子类化 [英] Can't subclass with ES6 / babel-node

查看:118
本文介绍了无法使用ES6/babel-node进行子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:要点

index.js 尝试实例化基本的"Auth"类,但在其构造函数中,auth类充当对象工厂,并传递回Auth的子类.

The index.js attempts instantiate a base "Auth" class but in it's constructor the auth class acts as an object factory and passes back a subclass of Auth instead.

'use strict';
import Auth from './Auth';

let o = new Auth({type:'Oauth1'});
console.log(o);
o.getToken();

Auth.js 类定义如下:

'use strict';
import Oauth1 from './Oauth1';

export default class Auth {
    constructor(config) {
        if (this instanceof Auth) {
            return new Oauth1(config);
        } else {
            this.config = config;
        }
    }

    getToken() {
        console.log('Error: the getToken module must be implemented in the subclass');
    }
}

Oauth1.js 类的定义为:

'use strict';
import Auth from './Auth';

export default class Oauth1 extends Auth {
    getToken() {
        console.log('Auth: ', Auth);
    }
}

使用babel-node index.js运行时,出现以下错误:

When running with babel-node index.js I get the following error:

TypeError :上级表达式必须为null或函数,且未定义

TypeError: Super expression must either be null or a function, not undefined

at _inherits (/repos/mine/test-app/Oauth1.js:1:14)
at /repos/mine/test-app/Oauth1.js:4:28
at Object.<anonymous> (/repos/mine/test-app/Oauth1.js:4:28)
at Module._compile (module.js:434:26)
at normalLoader (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)

如果我从Oauth1类中删除extends表达式,它将执行,但是我没有得到想要的继承.

If I remove the extends expression from the Oauth1 class it executes but then I am not getting the inheritance I want.

推荐答案

您的问题与babel无关.真正的问题是您的代码中有循环依赖项.

Your issue has nothing to do with babel. The real problem is that you have circular dependencies in your code.

要解决此问题,您应该从其父Auth类中删除Oauth1依赖项:

To resolve this issue you should remove Oauth1 dependency from its parent Auth class:

'use strict';
export default class Auth {
    constructor(config) {
        this.config = config;
    }

    getToken() {
        console.log('Error: the getToken module must be implemented in the subclass');
    }
}

'use strict';
import Auth from './Auth';

export default class Oauth1 extends Auth {
    getToken() {
        console.log('Auth: ', Auth);
    }
}

如果您不想从基类中删除this instanceof Auth支票,则可以在运行时require子类require而不是在模块初始化期间将其导入:

If you don't want to remove this instanceof Auth check from your base class, you could require your Oauth1 subclass in run-time instead of importing it during module initialization:

constructor(config) {
    if (this instanceof Auth) {
        let Oauth1 = require('./Oauth1');
        return new Oauth1(config);
    }
    this.config = config;
}

这篇关于无法使用ES6/babel-node进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆