Lyo Kato - Class.Setup-0.04

Documentation | Source

NAME

Class.Setup - builds new class easily

SYNOPSIS

    var MyClass = Class.Setup({
        static: {
            hoge: function () {
                alert('hoge');
            }
        },
        instance: {
            initialize: function () {
                this.name = 'MyClass';
            },
            getName: function () {
                alert(this.name);
            }
        }
    });

    MyClass.hoge();
    // it'll print out 'hoge';

    var myclass_obj = new MyClass;

    myclass_obj.getName();
    // it'll print out 'MyClass';

    var MyChild = MyClass.extend({
        instance: {
            initialize: function () {
                this.name = 'MyChild';
            }
        }
    });

    MyChild.hoge();
    // it'll print out 'hoge', MyChild inherits method 'hoge()' from MyClass.

    var mychild_obj = new MyChild;
    mychild_obj.getName();
    // it'll print out 'MyChild', because 'initialize()' is override by MyChild.

DESCRIPTION

This provides a easy way to build new classes. You can controll static, and daynamic properties and methods.

Many ideas are from Class. See document of Class.

TYPE

You need to pass object to "Class.Setup()", You can set follow two types of properties(and methods).

static

static(class) variables and method.

instance

variables and method of instance(object);

CONSTRUCTOR

Set 'initialize()' method as instance method. this is called as constructor.

INHERITANCE

The class made with this module can use 'extend()' method. Use this method like as 'Class.Setup()'.

And you can call __super__ in your class's instance methods.

    MyChild = MyClass.extend({
        instance: {
            hoge: function() {
                this.__super__('hoge');
            }
        }
    });

SEE ALSO

Class

AUTHOR

Lyo Kato <kato@lost-season.jp>

LICENSE

This library is free software; you can redistribute it and/or modify it under the terms of the "Artistic License"; http://dev.perl.org/licenses/artistic.html.

try{
    JSAN.use('Upgrade.Function.apply');
    JSAN.use('Upgrade.Array.push');
} catch (e) {
    throw "Class.Setup requires JSAN to be loaded";
}

if(Class == undefined) var Class = {};

Class.Setup = function ( setting ) {
    var newClass = function () {
        this.initialize.apply(this, arguments);
    };
    for ( type in setting ) {
        switch (type) {
            case 'static':
                for( prop in setting[type] )
                    newClass[prop] = setting[type][prop];
                break;
            case 'instance':
                for( prop in setting[type] )
                    newClass.prototype[prop] = setting[type][prop];
                break;
        }
    }
    newClass._extend = function (source, extension) {
        var childClass = function () {
            this.initialize.apply(this, arguments);
        };
        for ( prop in source )
            childClass[prop] = source[prop];

        for ( prop in source.prototype )
            childClass.prototype[prop] = source.prototype[prop];

        for ( type in extension ) {
            switch (type) {
                case 'static':
                    for( prop in extension[type] )
                        childClass[prop] = extension[type][prop];
                    break;
                case 'instance':
                    for( prop in extension[type] )
                        childClass.prototype[prop] = extension[type][prop];
                    break;
            }
        }
        childClass.prototype.__super__ = function ( methname ) {
            if( typeof source.prototype[methname] == 'function' ) {
                var args = [];
                for ( var i = 1; i < arguments.length; i++ )
                    args.push( arguments[i] );

                return source.prototype[methname].apply( this, args );
            }
        };
		childClass.extend = function (extension) {
			return childClass._extend( childClass, extension );
		};
        return childClass;
    };
	newClass.extend = function (extension) {
		return newClass._extend( newClass, extension );
	};
    return newClass;
};

Class.Setup.VERSION = 0.04;


/*

=head1 NAME

Class.Setup - builds new class easily

=head1 SYNOPSIS

    var MyClass = Class.Setup({
        static: {
            hoge: function () {
                alert('hoge');
            }
        },
        instance: {
            initialize: function () {
                this.name = 'MyClass';
            },
            getName: function () {
                alert(this.name);
            }
        }
    });

    MyClass.hoge();
    // it'll print out 'hoge';

    var myclass_obj = new MyClass;

    myclass_obj.getName();
    // it'll print out 'MyClass';

    var MyChild = MyClass.extend({
        instance: {
            initialize: function () {
                this.name = 'MyChild';
            }
        }
    });

    MyChild.hoge();
    // it'll print out 'hoge', MyChild inherits method 'hoge()' from MyClass.

    var mychild_obj = new MyChild;
    mychild_obj.getName();
    // it'll print out 'MyChild', because 'initialize()' is override by MyChild.

=head1 DESCRIPTION

This provides a easy way to build new classes. You can controll static, and daynamic properties and methods.

Many ideas are from L<Class>. See document of L<Class>.

=head1 TYPE

You need to pass object to "Class.Setup()",
You can set follow two types of properties(and methods).

=over 4

=item static

static(class) variables and method.

=item instance

variables and method of instance(object);

=back

=head1 CONSTRUCTOR

Set 'initialize()' method as instance method.
this is called as constructor.

=head1 INHERITANCE

The class made with this module can use 'extend()' method.
Use this method like as 'Class.Setup()'.

And you can call __super__ in your class's instance methods.

    MyChild = MyClass.extend({
        instance: {
            hoge: function() {
                this.__super__('hoge');
            }
        }
    });

=head1 SEE ALSO

Class

=head1 AUTHOR

Lyo Kato E<lt>kato@lost-season.jpE<gt>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the terms of the "Artistic License";
L<http://dev.perl.org/licenses/artistic.html>.

=cut

*/