Ticket #5 (closed: fixed)

Opened 3 years ago

Last modified 3 years ago

rmxbwclgh rbvtpx

Reported by: Slava Assigned to: Jan-Klaas Kollhof
Priority: Milestone: 1.0
Component: jsolait-core Version: 1.x
Severity: blocker Keywords: rmxbwclgh rbvtpx
Cc: rmxbwclgh rbvtpx

Description (Last modified by Jan-Klaas Kollhof)

If B inherits from A, the priv object created during B construction is only valid for B. Any references to this[priv] in A will cause undefined exception, since priv string is different in A from B.

Steps to reproduce:

var C=Class(function(publ,priv){
  print(priv)

  publ.__init__=function(){
  
      print(priv)
      print(this[priv]);
  }
});

var D=Class(C,function(publ,priv, b){
  print(priv)

})
new D()

JSOLaitLive code to reproduce

Possible solution would be to require developer to create private object manually in __init__:

this[priv]={};

or to supply some function that would do that:

createPriv(this);

This way priv object will be created only when needed.

Another solution would be to pass priv string from parent to the child class scope function. This would probably create conflicts with multiple inheritance.

Change History

01/03/06 10:37:08 changed by Jan-Klaas Kollhof

  • status changed from new to assigned.
  • description changed.

This seems to be a tricky one. For jsolait to take care of this it would have to go through all the base classes of a class recursively and create priv objects at instanciation time wether the priv object is ever needed or not. I'd rather not do that as it potentially slows things down. So, personally I'd go for the solution, requireing users to create their own priv object. I'll play around with it a bit and see if I come up with something better.

01/04/06 12:12:29 changed by anonymous

I did some profiling on possible solutions:

The first one is as described above. I.e. the user is responsible for the private object and jsoliat only provided the key passed in as priv. accessing the private object is simply using this[priv] The disadvantage is that the object might not have been initialized.

Another solution is to use priv as a function. A call to priv(this) will always return an object eithe a new one if none exists for the object passed in or the exsisting one respectively. The disadvantage is that it is a bit slower compared to this[priv].

some profiling to show the difference:

100000 times accessing this[priv].x takes 85ms
100000 times accessing priv(this).x takes 450ms

jsolait live code

05/15/06 13:57:26 changed by Jan-Klaas Kollhof

  • status changed from assigned to closed.
  • resolution set to fixed.

I have decided that priv will be provided as part of the class scope function but it is up to the class to decide what to do with it. The atvantage is that no base class needs to create a private object this[priv] = {} which makes object instanciation faster for classes that do not use priv.