I have come to accept that there is more than one way to do multiple inheritance in javascript. What I don't understand is how to call parent functions. I do it like so
function person(name){
this.name=name;
this.display=function(){alert(this.name);}
this.personDisplay=this.display; //pointer to p开发者_Python百科arent function
}
function employee(name,ID){
this.person=person;
this.person(name);
this.ID=ID;
this.display=function(){this.parentDisplay();alert(this.ID);}
}
but I was wondering if there was a correct way to reuse parent functions such as
function employee(name,ID){
this.person=person;
this.person(name);
this.ID=ID;
this.display=function(){parent.display();alert(this.ID);} //ideally what I want
}
This is not multiple inheritance. Only single inheritance.
In JavaScript, to mimic class-based object-oriented programming, you'll need to use the prototype. JavaScript technically is a prototypical language, not an object-oriented one, but you can simulate OO-style classes quite successfully.
Using the prototype is usually "clasical" or "pseudo-classical" style.
Or you can use the "functional" style popularized by Douglas Crockford (just Google it).
Michael Bolin has a good article discussing these two styles.
Most OO implementations in JavaScript are variants of these two styles.
You are strongly urged to adopt a JavaScript library that does all of this for you. My personal recommendation is the Dojo Toolkit.
I'm not quite following your code (you refer to a parentDisplay
function that isn't defined anywhere; did you meant personDisplay
?), but if you want to take advantage of JavaScript's prototypical inheritance, you can't use any property on this
to refer to the parent because you'll run into the grand-child problem: It's easy to create a parent/child hierarchy that way, but it fails as soon as you try to create a grandparent/parent/child hierarchy. This is because the object instance is always a child, even when parent code is running, and so if parent code tries to use the same property to refer to its parents' code, it ends up referring back to itself and recursing until the stack runs out.
So you use something other than a property on this
. My solution is to use a property on the function instances themselves and a bit of plumbing. When defining the subclass, you assign the superclass's version of the function to a property on the subclass's version of the function. Then code at all levels can use that property to refer to the parent version of the function.
I outline this in detail in this blog post: Simple, Efficient Supercalls in JavaScript. Using the helper created in the article, you can then do this:
var Parent = Helper.makeClass(function(){
function foo() {
return "P";
}
return {foo: foo};
});
var Child = Helper.makeClass(Parent, function(){
function foo() {
return foo.$super.call(this) + " < C";
}
return {foo: foo};
});
var GrandChild = Helper.makeClass(Child, function(){
function foo() {
return foo.$super.call(this) + " < GC";
}
return {foo: foo};
});
var gc = new GrandChild();
alert(gc.hierarchy()); // Alerts "P < C < GC"
(I don't like to call them classes any more, they're not classes, they're constructor functions. But the article still has the makeClass
name, so...)
Note the handy scoping functions so you can have utility functions for your "classes" that are completely hidden away from the outside world; and so your instance functions can have proper names, which is a good thing.
精彩评论