Skip to content

Commit

Permalink
added creatorpattern.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromeetienne committed Apr 23, 2013
1 parent 0ee7252 commit 7463a7d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions js/tquery.core.js
Expand Up @@ -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)
};


0 comments on commit 7463a7d

Please sign in to comment.