嗨,我有这样的父模块。
// usermgmt.js
var usermgmt = function () {};
usermgmt.prototype.test = function () {
return "test";
};
usermgmt.private = function () {
return "private";
};
module.exports = new usermgmt();
和这样的Child原型类。
// authentication.js
var usermgmt = require('./usermgmt');
var authentication = function () {};
authentication.prototype.callParent = function () {
usermgmt.private();
};
module.exports = new authentication();
我如何实现继承?我通过Google搜索,但没有适合我的解决方案。
正如@ jfriend00所说,我用class
关键字编写这些函数,这对于您的代码来说是语法糖!
usermgmt.js
// usermgmt.js
class usermgmt {
constructor() {
}
test() {
return "test";
}
private() {
return "private";
}
}
module.exports = usermgmt;
这样写身份验证。
authentication.js
// authentication.js
var Usermgmt = require('./usermgmt.js');
class authentication extends Usermgmt {
constructor() {
super();
}
callParent() {
console.log(this.private());
}
authFunction() {
console.log(':: authFunction ::');
this.callParent();
}
}
module.exports = authentication;
身份验证的用法将是:
var Authentication = require('./authentication.js');
let auth = new Authentication();
auth.callParent();
auth.authFunction();
console.log(auth.test());
1)使用class
和extends
语法更容易。
2)返回类而不是其实例
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句