TypeError:超级expression式必须为null或者一个函数,而不是用Babeljs定义的
我目前正在尝试在ES6中使用node.JS和Babel进行多文件inheritance(我使用Babel将ES6中的代码转换为ES5,因为Node现在不实现ES6)。 我正在使用导入/导出来“链接”不同的文件。
其实我有: 父类(文件1)
export class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } }
和: 子类(文件2)
import Point from './pointES5' export class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return super.toString() + ' in ' + this.color; } }
而主要的主要(文件3)
import Point from './pointES5' import ColorPoint from './colorpointES5' var m_point = new Point(); var m_colorpoint = new ColorPoint(); console.log(m_point.toString()); console.log(m_colorpoint.toString());
我正在做'从父母和孩子来testingtoString()方法调用。
那么我使用Babel将代码从ES6转换为ES5,并且我分别运行每个部分来testing它是否正常。
– 点(父)是好的,执行没有错误。
– ColorPoint(孩子)不完全运行,并抛出:
TypeError:超级expression式必须为null或一个函数,而不是未定义的
StackTrace的第一行是:
函数_inherits(subClass,superClass){if(typeof superClass!=='function'&& superClass!== null){抛出新的TypeError('超级expression式必须为null或函数,而不是+ typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}}); if(superClass)Object.setPrototypeOf? Object.setPrototypeOf(subClass,superClass):subClass。 proto = superClass; }(它来自ES5转换后的代码(Babelified),如果需要的话,我可以把它完全发布)。
这是令人沮丧的,因为这段代码非常简单…但是我不明白是什么原因导致了错误。
我尝试不同版本的巴别(5,5.8,6),但没有区别…
我做错了什么?
PS:我忘记说了:当我只用一个文件做这件事的时候,它是完美的。 但是对我来说只有一个档次的课程真的很重要
您的导出与您的导入不符:
export class Point // and import Point from './pointES5'
您正在导出命名符号,但导入了默认值,所以您将在第二个文件中将错误的对象指定为Point
。
你可以使用:
export default class Point
在第一类文件或
import {Point} from './pointES5';
在第二个文件中获取正确的引用。 如果你想要单个文件的每个文件的布局,我会build议前者。 您通常只会导出一个类,所以将其作为默认值是有意义的。
你现在拥有的是相当于:
// in Point module.exports = { Point: Point }; // in ColorPoint var Point = require('./pointES5'); // will point to *the whole object* class SubPoint extends Point
- 节点coffeescript类文件和inheritance
- Node.js / Express.js – 如何重写/拦截res.render函数?
- 为什么nodejs不能inheritance浏览器js
- 带有Node.js的数据库inheritance模型
- Nodejs + expressjs中的类成员inheritance
- 为什么在Node.js中使用util.inherits和.call来inheritance
- Object.create和inheritance
- 编译Coffeescript时不要添加var
- 复杂的循环Node模块依赖抛出“TypeError:”inheritance“的超级构造函数必须有一个原型”