diff --git a/js/tquery.core.js b/js/tquery.core.js index 2312a284..1c46fed0 100644 --- a/js/tquery.core.js +++ b/js/tquery.core.js @@ -420,3 +420,32 @@ tQuery.MicroCache = function(){ } +/** + * mixin for the creator pattern - From https://github.com/jeromeetienne/creatorpattern.js + * + * @param {Function} klass the constructor function of the class + * @param {String|undefined} name the name of the class + */ +tQuery.mixinCreatorPattern = function(klass, name){ + // js code for the creator pattern + var jsCode = [ + "klass.create = (function() {", + " function F(args) {", + " return klass.apply(this, args);", + " }", + " F.prototype = klass.prototype;", + " return function(){", + " return new F(arguments);", + " }", + "})()", + ].join('\n') + // handle klass name default value + // - if the name isnt explicitly given, get the name of the constructor function + name = name || klass.name + // replace the F class with the proper name + jsCode = name ? jsCode.replace(/F/g, name) : jsCode + // eval the code + eval(jsCode) +}; + +