Advertisement

02.25.2008 at 09:04AM PST, ID: 23190796
[x]
Attachment Details

Javascript Inheritance

Asked by m00003643 in JavaScript

Tags: Javascript, Inheritance, prototype, constructor, extend

Hey Experts -

This question is a continuation of a question I begun over here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23179299.html

The solution that I accepted works just as advertised, but there was still one thing that I didn't care for.  It seems to me that if I want a class to extend another, I should only have to specify that once.  In the solution on the other page, I have to call the extendClass function following the sub class' constructor, and then call the base class' constructor from within the sub class constructor.

While those steps seem inevitable, I couldn't help but take a stab at a solution that only requires one step.  I'm adding my solution below.  My question is if there are any obvious pit falls to what I am doing here, and if anyone can find a case in which this would not work as expected (whether that be browser compatibility or code implementation).  This script passed the simple test below in FF2, IE6+, and Safari 3 (for windows).

The first half of the extendClass function does nearly the exact same thing as the one in the previous post (I ended up opting for the naming convention I found in the book "Expressive JavaScript").  The main difference is that my version attempts to dynamically insert the call to the super class' constructor from within the extendClass function.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
function extendClass(subClass, superClass){
    function F(){};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;
 
    subClass.superClass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
    
    // insert call to superClass constructor
    var constructorStr = subClass.prototype.constructor + ""; // convert to string
    var splitAt = constructorStr.indexOf('{') + 1;
    var start = constructorStr.substring(0, splitAt);
    var end = constructorStr.substring(splitAt, constructorStr.length);
 
    var newMethod = "arguments.callee.superClass.constructor.call(this);";
 
    var newConstructor = start + newMethod + end;
 
    subClass.prototype.constructor = eval(newConstructor);
}
 
 
// ------------- BEGIN BaseObject ------------- //
function BaseObject() {
}
 
BaseObject.prototype.method = function() {
    alert("Alert from BaseObject.method");
}
 
// ------------- END BaseObject ------------- //
 
// ------------- BEGIN InheritsBaseObject ------------- //
function InheritsBaseObject() {
}
extendClass(InheritsBaseObject, BaseObject);
 
InheritsBaseObject.prototype.method = function() {
    alert('I overrode BaseObject.method.');
}
// ------------- END InheritsBaseObject ------------- //
 
var x = new InheritsBaseObject();
x.method();
[+][-]02.25.2008 at 09:30AM PST, ID: 20977361

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.25.2008 at 09:54AM PST, ID: 20977575

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.25.2008 at 10:54AM PST, ID: 20978131

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: Javascript, Inheritance, prototype, constructor, extend
Sign Up Now!
Solution Provided By: Bill-Hanson
Participating Experts: 1
Solution Grade: A
 
 
[+][-]02.25.2008 at 11:32AM PST, ID: 20978443

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906