不能使用 ES6 / babel-node 进行子类化
Can't subclass with ES6 / babel-node
我有以下文件:gist
index.js 尝试实例化一个基础 "Auth" class 但在它的构造函数中 auth class 充当对象工厂并传回 Auth 的 subclass。
'use strict';
import Auth from './Auth';
let o = new Auth({type:'Oauth1'});
console.log(o);
o.getToken();
Auth.jsclass定义如下:
'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.jsclass的定义是:
'use strict';
import Auth from './Auth';
export default class Oauth1 extends Auth {
getToken() {
console.log('Auth: ', Auth);
}
}
当 运行 babel-node index.js
我得到以下错误:
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 class 中删除 extends
表达式,它会执行,但我没有得到我想要的继承。
您的问题与babel
无关。真正的问题是您的代码中有 circular dependencies。
要解决此问题,您应该从其父级 Auth
中删除 Oauth1
依赖项 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);
}
}
如果您不想从您的基地 class 中移除 this instanceof Auth
支票,您可以 require
您的 Oauth1
subclass 在 运行-time 而不是在模块初始化期间导入它:
constructor(config) {
if (this instanceof Auth) {
let Oauth1 = require('./Oauth1');
return new Oauth1(config);
}
this.config = config;
}
我有以下文件:gist
index.js 尝试实例化一个基础 "Auth" class 但在它的构造函数中 auth class 充当对象工厂并传回 Auth 的 subclass。
'use strict';
import Auth from './Auth';
let o = new Auth({type:'Oauth1'});
console.log(o);
o.getToken();
Auth.jsclass定义如下:
'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.jsclass的定义是:
'use strict';
import Auth from './Auth';
export default class Oauth1 extends Auth {
getToken() {
console.log('Auth: ', Auth);
}
}
当 运行 babel-node index.js
我得到以下错误:
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 class 中删除 extends
表达式,它会执行,但我没有得到我想要的继承。
您的问题与babel
无关。真正的问题是您的代码中有 circular dependencies。
要解决此问题,您应该从其父级 Auth
中删除 Oauth1
依赖项 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);
}
}
如果您不想从您的基地 class 中移除 this instanceof Auth
支票,您可以 require
您的 Oauth1
subclass 在 运行-time 而不是在模块初始化期间导入它:
constructor(config) {
if (this instanceof Auth) {
let Oauth1 = require('./Oauth1');
return new Oauth1(config);
}
this.config = config;
}