最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

inheritance - How to override a private method in javascript? - Stack Overflow

programmeradmin1浏览0评论

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 your Car 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 newer class 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
 |  Show 1 more ment

1 Answer 1

Reset to default 3

You 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();

发布评论

评论列表(0)

  1. 暂无评论