Hi i have parent module like this.
JavaScript
x
14
14
1
// usermgmt.js
2
3
var usermgmt = function () {};
4
5
usermgmt.prototype.test = function () {
6
return "test";
7
};
8
9
usermgmt.private = function () {
10
return "private";
11
};
12
13
module.exports = new usermgmt();
14
and a Child prototype class like this.
JavaScript
1
11
11
1
// authentication.js
2
var usermgmt = require('./usermgmt');
3
4
var authentication = function () {};
5
6
authentication.prototype.callParent = function () {
7
usermgmt.private();
8
};
9
10
module.exports = new authentication();
11
How i implement inheritance? I searched by google but no solution works for me.
Advertisement
Answer
As @jfriend00 said, I write these functions with class
keyword which is a syntactic sugar for your code!
usermgmt.js
JavaScript
1
18
18
1
// usermgmt.js
2
3
class usermgmt {
4
constructor() {
5
6
}
7
8
test() {
9
return "test";
10
}
11
12
private() {
13
return "private";
14
}
15
}
16
17
module.exports = usermgmt;
18
Write authentication like this.
authentication.js
JavaScript
1
20
20
1
// authentication.js
2
var Usermgmt = require('./usermgmt.js');
3
4
class authentication extends Usermgmt {
5
constructor() {
6
super();
7
}
8
9
callParent() {
10
console.log(this.private());
11
}
12
13
authFunction() {
14
console.log(':: authFunction ::');
15
this.callParent();
16
}
17
}
18
19
module.exports = authentication;
20
And usage for authentication will be:
JavaScript
1
10
10
1
var Authentication = require('./authentication.js');
2
3
let auth = new Authentication();
4
5
auth.callParent();
6
7
auth.authFunction();
8
9
console.log(auth.test());
10
1) Use class
and extends
syntax which is easier.
2) Return Class and not its instance