1

I create a class with a function like this

var Obj=function(){this.children=[];this.parent=null;}//a base class
Obj.prototype.index=function(child){
  // the index of current obj
  if(arguments.length==0){
    return this.parent?this.parent.index(this):0;
  }
  // the index of a child matchs specific obj [to be override]
  return -1;
}

basically it is just an overload function composed of index() and index(child). Then I create a sub class,SubObj or whatever, inherits from Obj

SubObj.prototype.prototype=Obj;

Now, it's time to override the index(child) function,however, index() is also in the function an I don't want to overwrite it too.

One solution is to write like this

var Obj=function(){this.children=[];this.parent=null;}//a base class
Obj.prototype.index=function(child){
  // the index of current obj
  if(arguments.length==0){
    return this.parent?this.parent.index(this):0;
  }
  // the index of a child matchs specific obj [to be override]
  return this._index(this);
}
Obj.prototype._index=function(this){
  return -1;
}
SubObj.prototype._index=function(this){/* overwriteing */}

But this will easily mislead other coders as _index(child) should be both private(should not be used except index() function) and public(is an overload function of index(),which is public)

you guys have better idea?

Guan Yuxin
  • 410
  • 6
  • 12
  • http://stackoverflow.com/questions/4777522/javascript-inheritance-and-method-overriding may help. – Paul Grime Mar 19 '12 at 10:05
  • Make it more clear, You want to override the index function in SubObj? – Nemoy Mar 19 '12 at 10:13
  • index function is a function composed of two overload functions,index() and index(child).I want to override part of the index function,the part with parms. – Guan Yuxin Mar 19 '12 at 10:21
  • 2
    The standard answer is "javascript doesn't have classes". Anyhow, seems like a pretty confused question. See Paul's comment, or search for javascript class emulation patterns. You'll probably eventually come to the same conclusion as Douglas Crockford: trying to emulate classic class inheritance with javascript is not worth the effort. – RobG Mar 19 '12 at 11:44

1 Answers1

0

From my understanding, what you are trying to do should be quite doable. Although, I'd take RobG's advice and move away from trying to force a classical design onto JavaScript; JavaScript is about objects, not classes. Anyways, I digress. Here's a solution you can try out:

var Obj = function () {
    this.children = [];
    this.parent = null;
};
Obj.prototype.index = function (child) {
    if (arguments.length === 0) {
        return this.parent ? this.parent.index(this) : 0;
    }

    return -1;
};

var SubObj = function() {};
SubObj.prototype = new Obj();
SubObj.prototype.index = (function (base) {
    var someIndex = 10;
    return function (child) {
        // If child is defined then we
        // do our own processing.
        if (child && arguments.length === 1) {
            return someIndex;
        }
        // Otherwise we call our base/overriden version.
        return base.call(this);
    };
}(SubObj.prototype.index));

// Usage:
var o = new Obj(), so = new SubObj();
o.index(); // Returns 0
so.index(); // Returns 0
so.index(o); // Returns 10

There were a few issues with your prototype chain construction (SubObj.prototype.prototype = Obj doesn't actually do anything) and the definition of the index() method on the SubObj.prototype object (namely the use of this as an argument -- this will likely cause a world of pain when you try to run this in virtually any browser). I've made the fixes and implemented the override you seek (at least I think it's what you're after). Let me know if I misunderstood anything.

Darren
  • 275
  • 1
  • 3
  • 9