Permalink
Browse files

Improved code for creating new instances

Signed-off-by: Phil Mander <philip.mander@gmail.com>
  • Loading branch information...
philmander committed Aug 12, 2013
1 parent f884abd commit cc29478b1422786272450f07ca497b40fc8a6f8b
Showing with 10 additions and 38 deletions.
  1. +10 −38 src/inverted/ProtoFactory.js
@@ -122,44 +122,16 @@ define("inverted/ProtoFactory", [ "inverted/DependencyTree", "inverted/Util" ],
this._checkImplements(protoId, proto.prototype, interfaces);
}
- // ugly but works. would like a better way
- switch(args.length) {
- case 0:
- instance = new proto();
- break;
- case 1:
- instance = new proto(args[0]);
- break;
- case 2:
- instance = new proto(args[0], args[1]);
- break;
- case 3:
- instance = new proto(args[0], args[1], args[2]);
- break;
- case 4:
- instance = new proto(args[0], args[1], args[2], args[3]);
- break;
- case 5:
- instance = new proto(args[0], args[1], args[2], args[3], args[4]);
- break;
- case 6:
- instance = new proto(args[0], args[1], args[2], args[3], args[4], args[5]);
- break;
- case 7:
- instance = new proto(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
- break;
- case 8:
- instance = new proto(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
- break;
- case 9:
- instance = new proto(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
- break;
- case 10:
- instance = new proto(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
- break;
- default:
- throw Util.createError("Could not instantiate proto. Instances have a 10 arg limit");
- }
+ //magic constructor fn
+ var construct = function(constructorFn, args) {
+
+ var newConstructorFn = function () {
+ constructorFn.apply(this, args);
+ };
+ newConstructorFn.prototype = constructorFn.prototype;
+ return new newConstructorFn();
+ };
+ instance = construct(proto, args);
//add the dependency to the dependency tree.
depTree.addProto(protoId, instance);

0 comments on commit cc29478

Please sign in to comment.