venerdì 18 novembre 2011

The code is rewritten...

Hey guys ! The framework has been a major change and needed a better organization... but do not worry, have a good 30% from the first release. has been integrated object "Element" in the core package with integration to different listeners, and this was made possible by the introduction of the class interface! Oh yeah, of course, that's right, you can write Java-style interfaces, with the obligation to override methods declared! When I arrived at a generous 50% will be released the first alpha version, so stay tuned!

venerdì 22 luglio 2011

Declare a Class

Hi everyone!
Today I post a new feature of the framework, the Class object.
With this feature, you can create javascript objects like java-style.
Let's take a look at the code:

supported browser : Firefox,Google Chrome,Opera

/** Primitive **/
fujon.core.Primitive = function(){
  return function(){} ;
}
fujon.core.Primitive.prototype = {
  constructor: fujon.core.Primitive
}

/** Class **/
fujon.core.Class = function(object){
  if(object instanceof Object){
    var primitive = new fujon.core.Primitive() ;
    
     for(var property in object){
      var tag_property = property.split('$');
      if(tag_property.length > 1){
        if(tag_property[0] == 'static'){
          primitive[tag_property[1]] = object[property] ;
        }
      }else{
        if(property == 'constructor'){
          primitive = object.constructor
          primitive.prototype = {
            constructor: primitive
          }
          primitive.toString = function(){return '[object Class]';};
        }else if(property == 'extend'){
          primitive.super = {}
          for(var extend_property in object.extend){
            if(extend_property != 'constructor'){
              primitive.prototype[extend_property] = object.extend[extend_property] ;
            }
            primitive.super[extend_property] = object.extend[extend_property] ;
          }
        }else{
          primitive.prototype[property] = object[property];
        }
        if(property == 'toString'){primitive.toString = object[property];}
      }
     }
      return primitive ;
    }
}
fujon.core.Class.prototype = {
  constructor: fujon.core.Class,
  super:{},
  toString: function(){
    if(this.name){
      return this.name ;
    }else return '[object Class]';
  }
  
}

Well, now we see how to create a class in JavaScript, using the Class object and "extending" a second class with the first set!

var Class = fujon.core.Class ;

Point = new Class({
    constructor:function(x,y){
     this.x = x ;
     this.y = y ;
    },
    set:function(what,value){
      switch(what){
        case 'x' :
          this.x = value ;
          break;
        case 'y' :
          this.y = value ;
          break;
      }
    },
    get:function(what){
      switch(what){
        case 'x' :
          return this.x ;
        case 'y' :
          return this.y ;
      }
    },
    toString:function(){return '[object Point]';}
   });
   
   Point3 = new Class({
    constructor:function(x,y,z){
      // Call the super constructor !
      Point3.super.constructor(x,y);
      this.z = z ;
    },
    // Extend feature ! 
    // it's important that the statement "extend" is declared after the constructor
    extend: new Point(),
    // Override !
    set:function(what,value){
      if(what == "z"){
        this.z = value ;
      }else{
        Point3.super.set(what,value);
      }
    },
    // Override again !
    get:function(what){
      if(what == "z"){
        return this.z ;
      }else {
        return Point3.super.get(what);
      }
    },
    toString:function(){return "[object Point3]";}
   });

// Create a new Point3 object !
var p = new Point3(1,2,3);
// and try to see the three values
alert(p.get("x"));
alert(p.get("y"));
alert(p.get("z"));

Ok that's all folk today !
Next time I will speak of the forced-typing in javascript
Stay tuned!