How can I access parent prototype variable 'id' in child prototype.
const util = require('util');
const Parent = function () {};
Parent.prototype.access = function() {
var id = 1;
};
const Child = function () {};
util.inherits(Child, Parent);
child.prototype.access = function() {
//access Parent.prototype.access variable 'id' here
}
Any Thoughts??
You can do this using JS Classes.
class Parent {
constructor(id) {
this.id = id;
}
access() {
this.id = 1;
console.log('Parent access');
}
}
class Child extends Parent {
access() {
console.log(`Child access: ${this.id}`);
}
}
const d = new Child(5);
d.access(); // Child access: 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.