Nickolay - JooseX.Class.SimpleConstructor-0.03

Documentation | Source

Name

JooseX.Class.SimpleConstructor - a trait for class, making the 'new' keyword optional during instantiation

SYNOPSIS

    Class('Some.Class', {

        trait : JooseX.Class.SimpleConstructor,

        has : {
            ...
        },

        methods : {
            ...
        }

    })

    // no need in `new`
    var instance = Some.Class()

    var check = instance instanceOf Some.Class // true

DESCRIPTION

JooseX.Class.SimpleConstructor is a very simple trait, which makes the new keyword optional for instantiation. The class constructor will just return a new instance every time. This trait should be applied last among all your traits, modifying the constructor behavior.

GETTING HELP

This extension is supported via github issues tracker: http://github.com/SamuraiJack/JooseX-Class-SimpleConstructor/issues

For general Joose questions you can also visit #joose on irc.freenode.org or the forum at: http://joose.it/forum

SEE ALSO

Web page of this module: http://github.com/SamuraiJack/JooseX-Class-SimpleConstructor/

General documentation for Joose: http://openjsan.org/go/?l=Joose

BUGS

All complex software has bugs lurking in it, and this module is no exception.

Please report any bugs through the web interface at http://github.com/SamuraiJack/JooseX-Class-SimpleConstructor/issues

AUTHORS

Nickolay Platonov nplatonov@cpan.org

COPYRIGHT AND LICENSE

This software is Copyright (c) 2010 by Nickolay Platonov nplatonov@cpan.org.

This is free software, licensed under:

The GNU Lesser General Public License, Version 3, June 2007

Role('JooseX.Class.SimpleConstructor', {
    
    has : {
        forceInstance           : Joose.I.Object
    },
    
    
    
    override : {
        
        defaultConstructor : function () {
            var meta        = this
            var previous    = this.SUPER()
            
            this.adaptConstructor(previous)
            
            return function (forceInstance, params) {
                if (forceInstance == meta.forceInstance) return previous.apply(this, params) || this
                
                return new meta.c(meta.forceInstance, arguments)
            }
        }        
    }

})