admin管理员组文章数量:1026989
What is the right way to use a class defined in one file and extend it another, in node.js?
Currently I have:
'use strict'
class BasePageHandler {
constructor(app, settings, context) {
}
}
return module.exports;
In the 'child' class file I have:
'use strict'
var BasePageHandler = require ('./../BasePageHandler.js');
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
This fails with the following error:
TypeError: Class extends value #<Object> is not a function or null
Note, if I have the BasePageHandler in the same file then it works, so it is really when the class is in another file I have an issue.
Currently using node 4.4.0.
What is the right way to use a class defined in one file and extend it another, in node.js?
Currently I have:
'use strict'
class BasePageHandler {
constructor(app, settings, context) {
}
}
return module.exports;
In the 'child' class file I have:
'use strict'
var BasePageHandler = require ('./../BasePageHandler.js');
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
This fails with the following error:
TypeError: Class extends value #<Object> is not a function or null
Note, if I have the BasePageHandler in the same file then it works, so it is really when the class is in another file I have an issue.
Currently using node 4.4.0.
Share Improve this question asked Mar 30, 2016 at 21:15 Andre MAndre M 7,5249 gold badges61 silver badges105 bronze badges 1 |2 Answers
Reset to default 39You need to correctly export your class inBasePageHandler.js
file:
module.exports = BasePageHandler;
The accepted answer is technically fine, but really if you're using ES6 then you should go all in and use ES6 export/import.
/*jshint esversion: 6 */
class BasePageHandler {
constructor(app, settings, context) {
}
}
export default BasePageHandler;
and then:
/*jshint esversion: 6 */
import BasePageHandler from './../BasePageHandler.js';
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
What is the right way to use a class defined in one file and extend it another, in node.js?
Currently I have:
'use strict'
class BasePageHandler {
constructor(app, settings, context) {
}
}
return module.exports;
In the 'child' class file I have:
'use strict'
var BasePageHandler = require ('./../BasePageHandler.js');
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
This fails with the following error:
TypeError: Class extends value #<Object> is not a function or null
Note, if I have the BasePageHandler in the same file then it works, so it is really when the class is in another file I have an issue.
Currently using node 4.4.0.
What is the right way to use a class defined in one file and extend it another, in node.js?
Currently I have:
'use strict'
class BasePageHandler {
constructor(app, settings, context) {
}
}
return module.exports;
In the 'child' class file I have:
'use strict'
var BasePageHandler = require ('./../BasePageHandler.js');
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
This fails with the following error:
TypeError: Class extends value #<Object> is not a function or null
Note, if I have the BasePageHandler in the same file then it works, so it is really when the class is in another file I have an issue.
Currently using node 4.4.0.
Share Improve this question asked Mar 30, 2016 at 21:15 Andre MAndre M 7,5249 gold badges61 silver badges105 bronze badges 1-
You may want to use ES6 native modules with the Common JS transform plugin for Babel, instead
module.exports
. That way, your code will stay future-ready for when Node implements modules natively. – Dan Dascalescu Commented Oct 3, 2016 at 1:51
2 Answers
Reset to default 39You need to correctly export your class inBasePageHandler.js
file:
module.exports = BasePageHandler;
The accepted answer is technically fine, but really if you're using ES6 then you should go all in and use ES6 export/import.
/*jshint esversion: 6 */
class BasePageHandler {
constructor(app, settings, context) {
}
}
export default BasePageHandler;
and then:
/*jshint esversion: 6 */
import BasePageHandler from './../BasePageHandler.js';
class FrontpagePageHandler extends BasePageHandler {
constructor(app, settings, context) {
super(app, settings, context);
this.settings = settings;
this.context = context;
}
}
本文标签: javascriptES6 classeswith parent in different fileand nodejsStack Overflow
版权声明:本文标题:javascript - ES6 classes, with parent in different file, and node.js? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737453815a1474163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
module.exports
. That way, your code will stay future-ready for when Node implements modules natively. – Dan Dascalescu Commented Oct 3, 2016 at 1:51