Johntron

Python, PHP, Zend, Django, Mobile, and Linux web development guru



Javascript classes using Object Notation (JSON) with constructor


If you’re like me, you get really tired of looking at ugly JavaScript. Sure, JavaScript sourcecode can be very clean and concise, but why is there so much ugliness? JSON is our saving grace, but only if you know how to wield it.

If you want a history lesson on JSON,

var animal = function( species ) {
    return {
        species: null,
        init: function( species ) {
            this.species = species
            return this
        },
        toString: function() {
            return 'This is a ' + this.species
        }
    }.init( species )
}
var cat = new animal( 'cat' )
var dog = new animal( 'dog' )
document.write( cat.toString() )
document.write( dog.toString() )


Tags: ,