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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.