admin管理员组文章数量:1430224
I'm trying to use javascript object inheritance where I override a "private" method in the base "class" (in other words, make it a protected method).
Is it possible? Here's my best attempt (which doesn't work)
function Vehicle(color) {
this.color = color;
}
Vehicle.prototype.drive = function() {
_getMessage.call(this);
}
function _getMessage() {
console.log("override this method!")
}
//----------------------------------------------------------------
var Car = (function() {
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
function Car(color) {
Vehicle.call(this, color)
}
function _getMessage() {
console.log("The " + this.color + " car is moving!")
}
return Car;
}());
//----------------------------------------------------------------
$(function() {
var c = new Car('blue');
c.drive()
})
I'm trying to use javascript object inheritance where I override a "private" method in the base "class" (in other words, make it a protected method).
Is it possible? Here's my best attempt (which doesn't work)
function Vehicle(color) {
this.color = color;
}
Vehicle.prototype.drive = function() {
_getMessage.call(this);
}
function _getMessage() {
console.log("override this method!")
}
//----------------------------------------------------------------
var Car = (function() {
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
function Car(color) {
Vehicle.call(this, color)
}
function _getMessage() {
console.log("The " + this.color + " car is moving!")
}
return Car;
}());
//----------------------------------------------------------------
$(function() {
var c = new Car('blue');
c.drive()
})
https://plnkr.co/edit/ZMB9izK1W9VuFQHPNsvu?p=preview
Share Improve this question asked Jan 24, 2017 at 3:22 SkylerSkyler 8151 gold badge12 silver badges34 bronze badges 6-
2
You can't this way. Also, the original
_getMessage
function is not actually part of your class. – Jorg Commented Jan 24, 2017 at 3:26 -
2
Here's my best attempt (which doesn't work)
- what's wrong with it? What do you expect? What do you get instead? – Jaromanda X Commented Jan 24, 2017 at 3:27 - _getMessage is global, it's not "private" in any way, just replace it. If it actually was private, you can replace it if you have a privileged function specifically to do that. – RobG Commented Jan 24, 2017 at 3:33
-
_getMessage
in yourCar
object is "private" in the sense that it is only visible in the scope of that IIFE ... and no amount of prototypal trickery or the newerclass
sugary goodness is going to make that function available outside that scope unless you make it available, therefore making it not "private" – Jaromanda X Commented Jan 24, 2017 at 3:37 - @JaromandaX—but the "private" _getMessage is never called… so what's the point? ;-) – RobG Commented Jan 24, 2017 at 4:09
1 Answer
Reset to default 3You could introduce a privileged method that can change the private one:
// IIFE to create constructor
var Car = (function(){
// Private method
function _getMessage(text){
console.log('original: ' + text);
}
// Normal constructor stuff
function Car(make){
this.make = make;
}
Car.prototype.getMake = function(){
return this.make;
}
Car.prototype.getMessage = function(){
_getMessage(this.make);
}
// Privileged function to access & change private method
Car.changeGetMessage = function(fn) {
_getMessage = fn;
}
return Car;
}());
// Demonstration
// Create instance
var ford = new Car('Ford');
console.log(ford.getMake());
// Call original method
ford.getMessage();
// Replace original
Car.changeGetMessage(function(text){
console.log('new message: ' + text);
});
// Existing instances get new method
ford.getMessage();
// Create new instance
var volvo = new Car('Volvo');
console.log(volvo.getMake());
// New instances get method too
volvo.getMessage();
本文标签: inheritanceHow to override a private method in javascriptStack Overflow
版权声明:本文标题:inheritance - How to override a private method in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745514043a2661444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论