Classical inheritance in JavaScript really possible?

Hello!

I read currently an article about classical inheritance in JavaScript. Although the author, Douglas Crockford, concludes that his "early attempts to support the classical model in JavaScript as a mistake", it is an interesting approach to an object-based language that does not have any classes or methods.

As an example, I created several projects in eclipse, where one is called "Utils" with reusable code. One of the reusable objects is a Set that saves the data in an array, iff the element is not already an element. ECMAScript 6 does support a set, but it is still experimental; so, I decided to write my own object.

To test the classical inheritance approach, I created an OrderedSet that uses most of the functions of Set, but always displays the set contents in order. To test the functionality, I created an OrderedSet.html file and corrected errors until everything looks fine.

As I am somebody with modular thinking, I decided to uses the OrderedSet in prime number calculation. Interestingly, the OrderedSet does not function anymore, although the test with OrderedSet.html works still fine. The error message is
"use strict";

var gSet;   // Global test variable

OrderedSet.inherits(Set);

/**
 * Constructor function of the ordered set class
 * 
 * @param The value to be passed
 */
function OrderedSet(value) {
    this.setValue(add2Array(value));    // <<<<<< this.setValue undefined!
}
[…]

Open in new window

Actually, the object OrderedSet does not have any setValue function, but it should use the one from Set, from which it inherits all functions/methods:
"use strict";
[…]
/**
 * Setter for value
 * 
 * @param value
 * @returns this
 */
Set.method('setValue', function (value) {
    if (isArray(value)) {
        this.value = value;
    } else if (isSet(value)) {
        this.value = value.toArray();
    } else {
        if (!this.contains(value)) {
            if (isNumeric(value)) {
                value = parseFloat(value);
            }
            this.value[this.value.length] = value;
        }
    }
    return this;
});

Open in new window


Does anybody experience similar problems when trying to experiment with classical inheritance? Unfortunately, I cannot upload the code so that you can have a try.

Thank you for your response!
Notice: For those who want to check the code: first, test the "OrderedSet.html", then the "Maths.html". You will see that the ordered set works properly, but Maths cannot use it because of the error message given above.
TestProject.zip
LVL 1
Ahmet Ekrem SABANSenior IT consultantAsked:
Who is Participating?
 
Rainer JeschorCommented:
Hi,
took some time but quite easy to fix:
you have to load the set.js BEFORE loading the OrderedSet.js file!

Just move this line
<script type="text/javascript" src="../Utils/Maths/js/Set.js"></script>

Open in new window

upwards before
<script type="text/javascript" src="../Utils/Maths/js/OrderedSet.js"></script>

Open in new window


HTH
Rainer
0
 
Rainer JeschorCommented:
And you will still get an error - as the function/method "add" is not implemented in the Set.js

The error will come from Primes.js - line 167/168

KR
Rainer
0
 
Ahmet Ekrem SABANSenior IT consultantAuthor Commented:
Excellent, sir! Congratulations!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.