Joose blog

Blog about Joose – advanced class system for JavaScript

Joose.Why

without comments

Class(‘Joose.Why’, {

If someone would came at our IRC channel and would’ve asked me “Why Joose?” we could have the following hypothetical conversation:

Visitor: So why should I care about Joose? I already code in JavaScript – beautiful, functional language, very expressive, I write 500 lines of jQuery/ExtJS/NodeJS code in a day and my boss is happy – why should I bother?

Me: Lets slow down a bit, and clarify the things – JavaScript is not a functional language. Its a dynamic, imperative language, which has a first-class function. But, for example Perl also has the first class function, closures, etc, almost for 2 decades already and no one thinks Perl is a functional language. You are coding in the imperative language – should have no illusions about that.

V: So what?

M: So, the imperative programs tend to quickly turn into mess (“spaghetti code”). Its just the nature of imperative languages.

V: Not my code!

M: Of course, your’s not. But probably on your next job, you’ll have to maintain the codebase, written by someone else, you know what I mean..

V: Yea, other guys just can’t do the things right..

M: And to limit the mess in the imperative world, clever guys invented to separate the functions into groups, and limit their’s side-effects with a single object. They called those groups – classes and the whole thing – Object-Oriented-Programming.

V: Bo-o-ring..

M: Hey, you can do the same thing in JS, and its fun, take a look:

Class('Person', {

    methods : {
        eat : function (food) {
            console.log('yummy')

            return 'yummy'
        }
    }
})

Class('Person.Tidy', {
    isa : Person,

    before : {
        eat : function (food) {
            this.washHands()
        }
    },

    after : {
        eat : function (food) {
            this.brushTeeth()
        }
    },

    methods : {

        washHands : function (food) {
            console.log('washing hands')
        },

        brushTeeth : function (food) {
            console.log('brushing teeth')
        },

        eat : function (food) {
            this.SUPER(food)
        }
    }
})

V: Yet another language, compiling down into JS? No thanks.

M: Nah, its pure JS, just a call to `Class` function, with class name and some JSON, describing the class.

V: Hm..

M: This style is called “Declarative programming”.

V: So I can declare that program should “just work” and it will? :D

M: Almost :) Joose provides declarative constructs for the most common programming patterns, like Roles, attributes delegation, types, Singletons, etc, you can also create your own constructs.

V: My own? Like what?

M: Like if you are writing the Router class, you may make it has not only the attributes and methods, but also “routes”. You decide what the semantic of those “routes” will be and how they will behave. Or for example, you can add additional behavior to attributes (validation, binding, laziness, whatever) or methods (overloading, non-blocking chaining, whatever),

V: I see.. Hey, and does Joose works with NodeJS?

M: Sure: http://samuraijack.github.com/Task-Joose-NodeJS/

V: Ok, cool. Any links to start with?

M: aha: http://bit.ly/joose_manual

})

Written by Nickolay Platonov

January 12th, 2011 at 4:01 pm

Posted in Joose

Tagged with ,