Ticket #2 (closed defect: invalid)

Opened 3 years ago

Last modified 2 years ago

Inheritance mistake in new development trunk?

Reported by: helder.magalhaes@gmail.com Assigned to: Jan-Klaas Kollhof
Priority: normal Milestone: 2.0
Component: jsolait-core Version: 2.0
Severity: major Keywords: inheritance
Cc:

Description (Last modified by Jan-Klaas Kollhof)

Hi! First of all, I'd like to congratulate you for the development of jsolait.

Recently, I began testing the inheritance (according to the tutorial) an was getting error messages.

Today, I decided to test again using the first release (1.0) version and everything went well...

The code I used to test (basically a copy of a sample taken from the yahoo groups):

SomeClass = Class(function(publ, supr){
    var private_static_obj = "Hello World";
    //the initialization method is called when
    //an object of the class is created
    publ.init = function(abcd){
        this.prop1 = abcd;
    }
    //a public method
    publ.someMethod = function(){
        print(private_static_obj);
        print(this.prop1);
    }
})

SubClass = Class(SomeClass, function(publ, supr){
    var a_module_private_obj = "myPrivateData";
    publ.init = function(){
        supr(this).init("easy to call super's methods");
    }
    publ.someMethod = function(){
        supr(this).someMethod();
        print(a_module_private_obj);
    }
})

var obj1 = new SomeClass("foo");
var obj2 = new SubClass("bar");
obj1.someMethod(); 
obj2.someMethod(); 

Expected result (obtained with 1.0 release):

Hello World
foo
Hello World
easy to call super's methods
myPrivateData

Erroneous result (obtained with both 2.0a small development trunk and subversion):

Hello World
undefined

And Javascript console fired:

Error: supr is not a function

Hope this can help! If this is a syntax mistake from me, please document 2.0 version's inheritance (tutorial?).

Thank you in advance for the time spent analysing this!

Helder

Change History

11/18/05 15:05:19 changed by Jan-Klaas Kollhof

  • status changed from new to closed.
  • resolution set to invalid.
  • description changed.

Hi Helder,

I guess I should publish the changes that will be made in jsolait 2.0. On of the main changes is breaking some backwards compatibility :(.

all jsolait related extensions will have a "pythonesque" style and are easyly recognized by __someName__. Why? This way if one iterates over an object's properties one can esyly identify jsolait specific methods, also they should not conflict with users own names for objects and methods.

The next thing that will change is the handling of supr. It was a function that when called with 'this' would generate a proxy object that would have wrapper functions for all super class' public methods bound to the this object which when called would call the super class' method. The proxy object created by supr is then stored as part of the this objct so it only needs to be created once. The setup takes quite some time as supr has to iterate over all methods of the super class and generate method proxies. Calling a method also takes longer as the proxy's method is called which in turn calls the real method. supr seems nice to use but the ease of use does not justify the performance decrease. Another issue is multiple inheritance. I guess for each baseclass there would need to be a supr method. Instead I decided to go back to the very early impl, just passing in each base class' prototype. this means using the ugly base1.__init__.call(this, param1, ...) approach. Unless people convince me to impl. supr(this) I will stick to the new/old way.

The next change (not breaking your example)is that the class scope expects a priv argument if more arguments are given then the number of base classes + 1(for publ param). This is a string and can be used to access a private instance object using this[priv].something This priv-string is unique for each class.

here your example rewritten:

SomeClass = Class(function(publ,supr){
    var private_static_obj = "Hello World";
    //the initialization method is called when
    //an object of the class is created
    publ.__init__ = function(abcd){
        this.prop1 = abcd;
    }
    //a public method
    publ.someMethod = function(){
        print(private_static_obj);
        print(this.prop1);
    }
})

SubClass = Class(SomeClass, function(publ,supr){
    var a_module_private_obj = "myPrivateData";
    publ.__init__ = function(){
        supr.__init__.call(this,"easy to call super's methods");
    }
    publ.someMethod = function(){
        supr.someMethod.call(this);
        print(a_module_private_obj);
    }
})

var obj1 = new SomeClass("foo");
var obj2 = new SubClass("bar");
obj1.someMethod(); 
obj2.someMethod(); 

I'll post these changes on teh wiki and on teh forum to get people's feedback,

Jan