Re: inheritance, yes, but how?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: benben (benhongh_at_yahoo.com.au)
Date: 04/27/04


Date: Tue, 27 Apr 2004 21:52:59 +1000

Thanks for your advices! I tested the functions and they worked. But it is
still not what I have expected. What I have come up with is a set of
functions that simulate a class definition...but they seem way too
bulky...:-)

ben

> "benben" <benhongh@yahoo.com.au> writes:
>
> > How could I do class inheritance in JScript...not in .Net version. It is
ok
> > even if I have to go the ugly way...I tried the following function;
>
> Making class based inheritance in Javascript is hard because Javscript
> doesn't have classes (it has constructors, but it's not the same),
> and inheritance is between objects instead.
>
> Example:
> ---
> var superObj = new Object();
> function Foo(){};
> Foo.prototype = superObj;
> var foo = new Foo();
> alert(foo instanceof Foo); // true
>
> function Bar(){};
> Bar.prototype = superObj;
> alert(foo instanceof Bar); // true, because foo inherits from superObj,
> // no matter what constructor it sits on.
> Foo.prototype = null;
> alert(foo instanceof Foo); // false, because foo inherits from superObj,
> // and not really from Foo
>
> ---
> Now, if you understand this, you can see that treating constructors as
> classes is not correct. We will do so anyway, and promise not to
> change the prototypes of constructors after we create them.
>
> The next one tries is:
> ---
> function Foo(){};
> function Bar(){};
> Bar.prototype = new Foo(); // Bar inherits from Foo?
> var bar = new Bar();
> alert(bar instanceof Foo); // yes ... or?
> ---
> No, Bar does not inherit from the "class" Foo, but from an *instance*
> of it. From an object, not a class. The more one looks at it, the
> further it seems from real class based inheritance. It is not the way
> to go!
>
> My best suggestion so far is this function:
> ---
> function extend(classA, extenderB) {
> var aLen = classA.length; // number of parameters to classA constructor
> function extended() { // the constructor to create
> // convert arguments to arrays and call constructors to initialize
> var argArrayA = Array.prototype.slice.call(arguments,0,aLen);
> classA.apply(this,argArrayA);
> var argArrayB = Array.prototype.slice.call(arguments,aLen);
> extenderB.apply(this,argArrayB);
> }
> // create new prototype, extending classA.prototype without changing
it.
> var newProto = cloneObj(classA.prototype);
> for(var i in extenderB.prototype) {
> newProto[i] = extenderB.prototype[i];
> }
> extended.prototype = newProto;
> return extended;
> }
>
> // utility function
> function cloneObj(obj){
> function dummy(){};
> dummy.prototype = obj;
> return new dummy();
> }
> ---
>
> You can then use it like this:
> ---
> function Foo(z) {
> this.x = z;
> }
> Foo.prototype.alertX = function(){alert(this.x);};
> function Bar(z,w) {
> this.y = z+w;
> }
> Bar.prototype.alertY = function(){alert(this.y);};
>
> var Baz = extend(Foo,Bar);
> var baz = new Baz(42,32,5); // first argument sent to Foo, remaining to
Bar
> baz.alertX(); // alerts 42
> baz.alertY(); // alerts 37
>
> alert(baz instanceof Foo); // alerts true - "real" inheritance from A
> alert(baz instanceof Bar); // alerts false - not inheriting from B
>
> Foo.prototype.z = 87;
> alert(baz.z); // alerts 87 - dynamic inheritance and all!
> ---
>
> The "extenderB" function should not be seen as a real class, but just as
> a collection of an initializer function and some properties to *copy*
> to the new constructor. The "classA" function is really being inherited
> from.
>
> It requires a correct implementation of ECMAScript to work :)
> The ones in the JScript that comes with IE 5.5 and IE 6 works, but
> IE 5.0 doesn't.
>
> Enjoy!
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> 'Faith without judgement merely degrades the spirit divine.'



Relevant Pages

  • Re: Constructor inheritance (or lack thereof)
    ... "You don't overload a signature - " ... I will post back on the exact error ... "It is a means to achieve inheritance because without ... that exactly is hard to say, given that constructors aren't inherited ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The Language I want
    ... abstract classes are like a contract saying this class cannot be ... class constructors and destructors as well as object ... instantiation), inheritance is useless. ...
    (comp.lang.misc)
  • Copy Constructors/Assignment Operator w/ Inheritance
    ... I'm trying to get inheritance and constructors clear in my head (and ... simply because in the case of D and E they'll call the base class' ... their members "memberwise", and call the base class copy constructor. ...
    (comp.lang.cpp)
  • Re: inheritance, yes, but how?
    ... Making class based inheritance in Javascript is hard because Javscript ... var superObj = new Object; ... var foo = new Foo; ... Now, if you understand this, you can see that treating constructors as ...
    (microsoft.public.scripting.jscript)