Changeset 26

Show
Ignore:
Timestamp:
11/25/05 11:05:58 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

fixed a problem with classes not being able to overwrite str

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jsolait/jsolait.js

    r23 r26  
    6161    //set up the 'public static' fields of the class 
    6262    var __class__={__isArray__ : false, 
    63                      __name__ : name, 
    64                      __bases__: bases, 
    65                      __id__:classID, 
    66                      __hash__: function(){ 
    67                         return this.__id__; 
    68                      }, 
    69                      __str__ : function(){ 
     63                         __name__ : name, 
     64                         __bases__: bases, 
     65                         __id__:classID, 
     66                         __hash__: function(){ 
     67                            return this.__id__; 
     68                         }, 
     69                         __str__ : function(){ 
    7070                            return "[class %s]".format(this.__name__); 
    71                        
     71                       
    7272                    }; 
    7373 
     
    7979            return "[%s %s]".format(this.__class__.prototype.__call__ === undefined ? 'object' : 'callable', this.__class__.__name__); 
    8080        }; 
    81         //because toString is not apearing in a for in loop we will just use __str__ and always assign it to toString 
    82         //this makes prototype creatin a bit simpler 
    83         proto.toString=proto.__str__; 
    8481        __class__.__bases__=[Object]; 
    8582    }else{ //inherit from all base classes 
    86         //inheritance is done by 
    8783        var baseProto; 
    8884        for(var i=0;i<bases.length;i++){ 
     
    114110            } 
    115111        } 
    116         //make sure the toString points to __str__, this will make overwriting __str__ after object construction impossible (todo ?) 
    117         //but will be faster than having toString call __str__, also overwriting methods unless they are ment to be overridden is not cool anyways. 
    118         proto.toString=proto.__str__; 
    119112    } 
    120113    //make sure all jsolait objects have a hash method 
     
    129122    proto.__class__=__class__; 
    130123 
    131     var privId = '__priv__' + __class__.__id__; 
    132  
    133     //run teh class setup function provided as classScope 
     124    //run the class setup function provided as classScope 
    134125    if(classScope.length-1 > baseProtos.length){ 
     126        var privId = '__priv__' + __class__.__id__; 
    135127        classScope.apply(this,[proto, privId].concat(baseProtos)); 
    136128    }else{ 
    137129        classScope.apply(this,[proto].concat(baseProtos)); 
    138130    } 
    139  
     131     
     132    //make sure the toString points to __str__, this will make overwriting __str__ after object construction impossible (todo ?) 
     133    //but will be faster than having toString call __str__, also overwriting methods unless they are ment to be overridden is not cool anyways. 
     134    proto.toString=proto.__str__; 
     135         
    140136    //allthough a single constructor would suffice for generating normal objects, Arrays and callables, 
    141137    //we use 3 different ones. This will minimize the code inside the constructor and therefore 
     
    212208    proto.constructor = NewClass; 
    213209    proto.__class__= NewClass;//no, it is not needed, just like __str__ is not, but it is nicer than constructor 
    214  
     210         
    215211    //this is where the inheritance realy happens 
    216212    NewClass.prototype = proto; 
     
    225221}; 
    226222Class.__idcount__=0; 
    227 Class.toString = function(){ 
    228     return "[object Class]"; 
    229 }; 
    230  
    231 Class.__createProto__=function(){ 
    232     throw "Can't use Class as a base class."; 
    233 }; 
     223Class.__str__=Class.toString = function(){return "[object Class]";}; 
     224Class.__createProto__=function(){ throw "Can't use Class as a base class.";}; 
    234225 
    235226Array.__isArray__=true; 
     
    237228Array.__createProto__=function(){ var r =[]; r.__str__ = Array.prototype.toString;  return r; }; 
    238229Object.__str__=Object.toString=function(){return "[class Object]";}; 
    239 Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callabel interface instead using YourClass::__call__.";}; 
    240  
    241  
     230Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 
    242231 
    243232/** 
     
    370359Module("jsolait", "$Revision$", function(mod){ 
    371360    jsolait=mod; 
    372  
     361     
    373362    mod.modules={}; 
    374363