Jump to content

Javascript class properties and inheritance.

- - - - -

  • Please log in to reply
No replies to this topic

#1
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Hi! I need some help when it comes to the javascript class properties. I know there don't exist such a thing really in JS but you can get the simulated effect with giving properties to the constructor function.

The problem is that in my inherit method where I setup the prototype I don't get the class properties passed over. What I thought I would do was to just loop trough them with a For In loop but then I remembered that won't work because then the class B would have copies or references to the variables of the ones in A. But what I want is that when a class property of A is changed in B then it is also changed in A. To put it simply, they are one and the same object/data in memory.

The result I want is this one:

A.myValue = "Hello";

B.myValue += " world!";

print(A.myValue); // output as 'Hello world!'

The result should work seamless and the developer shouldn't even know what goes on.

Here's my code so far.

Function.prototype.inherit = function inherit(parent) {

    for(var prop in parent) {

        setter = new Function("val", "return this.prototype.class." + prop + " = val;");

        this.__defineSetter__(prop, setter);

        setter.name = prop;

        

        getter = new Function("return this.prototype.class." + prop + ";");

        this.__defineGetter__(prop, getter);

        getter.name = prop;

    }

    this.prototype = new parent;

    this.prototype.class = parent;

    this.prototype.parent = this.prototype;

}


function A() {

    func = A;

    while(func.caller) {

        func = func.caller;

    }

    

    this.class = func;

}

function B() {

    A.call(this);

}


A.prototype.super = function super() {

  name = super.caller.name;

  func = this.class.prototype.class.prototype[name];

  if(func == undefined)

    throw name + " in " + this.parent.toString() + " not found!";

    

  return func.apply(this, arguments);

}


A.toString = function toString() {

    return this.name;

}


A.prototype.toString = function toString() {

    str = "<" + this.class.name + "#";

    for(var property in this) {

        value = this[property];

        str += property;

        if(value instanceof A) {

            str += " => " + "<";

            str += value.class.name + "#...";

            str += ">";

        } else if(value instanceof Function) {

            str += "()";

        } else {

            str += " = ";

            if(value instanceof String) {

              str += '"' + value + '"';

            } else {

                str += value;

            }

        }

        

        str += ", ";

    }

    

    str = str.slice(0, str.length-2);

    str += ">";

    return str;

}


A.prototype.test = function test(a) {

    print(a);

}


A.wtf = "Hello world!";


B.inherit(A);


B.prototype.test = function test(a) {

    this.super(a);

}


b = new B;

b.test("Hey! This is awesome!!!!");

print(b);

print(b.parent);

print(B.wtf);

B.wtf = "Bye bye world!";

print(A.wtf);


If it matter, I am using the V8 engine.

**EDIT**
Never mind! I managed to solve it myself. Here's the Function.inherit function that solves it all! What I do is that I define setters and getters that wrap around the values of the previous "class". Enjoy:

Function.prototype.inherit = function inherit(parent) {

    for(var prop in parent) {

        setter = new Function("val", "return this.prototype.class." + prop + " = val;");

        this.__defineSetter__(prop, setter);

        setter.name = prop;

        

        getter = new Function("return this.prototype.class." + prop + ";");

        this.__defineGetter__(prop, getter);

        getter.name = prop;

    }

    this.prototype = new parent;

    this.prototype.class = parent;

    this.prototype.parent = this.prototype;

}


Edited by Groogy, 28 August 2010 - 10:13 AM.
solved it

My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users