random thoughts, formed in the twisted mind of a coder... RSS 2.0
# Thursday, 24 September 2009

Got yourself a little familiar with JSON (JavaScript [literal] Object Notation) and like it so much that you want to do everything in JSON? Also defining classes that you can instantiate?

Note: Although Javascript only knows functions and objects (and even the functons are objects), let's, for the sake of argument, also use the well-known definitions "class" and "instance".

You've probably tried something like this...

  var o =
  {
    a : 0,
    b : "z"
  };
  var o2 = new o();

That failed, so you went on and tried something like this...

  var o =
  {
    a : 0,
    b : "z",
    constructor : function()
                  {
                    return this;
                  }
  };
  var o2 = new o();

Hmm no, maybe this...

  var o =
  {
    a : 0,
    b : "z",
    init : function()
           {
             this.a = 0;
             this.b = "z";
             return this;
           }
  };
  var o2 = o.init();

Ehr, no wait...

  var o =
  {
    a : 0,
    b : "z",
    init : function()
           {
             this.a = 0;
             this.b = "z";
             return this;
           }
  };
  var o2 = new o.init();

YEAH... Whoohoo... oh wait...
Wouldn't the old notation be simpler then?

  function o()
  {
    this.a = 0;
    this.b = "z";
  }
  var o2 = new o();

You can look online for answers, but you probably going to find more questions there or got stuk up with a JSON-Create-Class library, like Thomas Frank's classy_json. Although that particular library is quite nice, you may not want yet another library.
And you don't have to!

The problem is that JSON object instances resemble singleton classes. They have no constructor and are not prototype-able. Therefore you can't create instances of them.
For the remaining of this article I will call this object type the JSON type.

Let's give the JSON type a constructor... or better, let's give the constructor a JSON type.

  function o()
  {
    var i =
    {
      a : 0,
      b : "z"
    }
   
    return i;
  }
  var o2 = new o();
 

By calling the function o with the "new" keyword, the function o becomes a constructor of type o. A constructor returns an instance. Normally, when you wouldn't use a return statement in the constructor, the returned type is the same as the type you create ("o" in this case), but in fact you're able to return any type. Also a JSON type.

Please note that the returned type is still a JSON class type. Thus it still doesn't have a constructor and a prototype. But that's the only catch. Every instance has it's own memory space and is really separate from the rest of the instances.

You are also free to adapt the constructor to take parameters or to run additional code...

  function o(_num, _chr)
  {
    var i =
    {
      a : (_num||0),
      b : (_chr||"z"),
     
      show : function()
             {
               alert(this.a + " - " + this.b);
             }
    }
   
    return i;
  }
 
  var o2 = new o(2);
  var o3 = new o(null,"x");
  o2.b = "q";
  o2.show(); //shows "2 - q"
  o3.show(); //shows "0 - x"

Have fun with it!

Oh and btw... you can also omit the "new" keyword :-)

 

Thursday, 24 September 2009 13:56:28 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
Script and HTML
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Martijn Thie
Sign In
Statistics
Total Posts: 18
This Year: 0
This Month: 0
This Week: 0
Comments: 174
All Content © 2017, Martijn Thie
DasBlog theme adapted from 'Business' (originally by delarou)