Skip to content

Commit 1cd54f2

Browse files
new version
1 parent ddae353 commit 1cd54f2

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-ioc",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "A Lightweight annotation-based dependency injection container for typescript.",
55
"author": "Thiago da Rosa de Bustamante <[email protected]>",
66
"dependencies": {

src/typescript-ioc.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export function AutoWired(target: Function) {
137137
let existingInjectedParameters: number[] =
138138
Reflect.getOwnMetadata("params_inject", target) || [];
139139
let newConstructor;
140+
const _isClass = isClass(target);
140141
if (existingInjectedParameters.length > 0) {
141142
existingInjectedParameters.reverse();
142143
const paramTypes: Array<any> =
@@ -149,14 +150,14 @@ export function AutoWired(target: Function) {
149150
newArgs.push(IoCContainer.get(paramTypes[index]));
150151
}
151152
}
152-
let ret = construct(target, newArgs, ioc_wrapper, this);
153+
let ret = construct(target, newArgs, ioc_wrapper, this, _isClass);
153154
return ret;
154155
}, target);
155156
}
156157
else {
157158
newConstructor = InjectorHanlder.decorateConstructor(function ioc_wrapper(...args: any[]) {
158159
IoCContainer.assertInstantiable(target);
159-
let ret = construct(target, args, ioc_wrapper, this);
160+
let ret = construct(target, args, ioc_wrapper, this, _isClass);
160161
return ret;
161162
}, target);
162163
}
@@ -551,8 +552,12 @@ declare var Map: MapConstructor;
551552

552553
// Polyfill for Reflect.construct. Thanks to https://github.com/Mr0grog/newless
553554
function isSyntaxSupported(example) {
554-
try { return !!Function("", "'use strict';" + example); }
555-
catch (error) { return false; }
555+
try { return !!Function("", "'use strict';" + example); }
556+
catch (error) { return false; }
557+
}
558+
559+
function isClass(v): boolean {
560+
return typeof v === 'function' && /^\s*class\s+/.test(v.toString());
556561
}
557562

558563
let _constructFunc = global['Reflect'] && global['Reflect'].construct || (function() {
@@ -605,13 +610,13 @@ let _constructFunc = global['Reflect'] && global['Reflect'].construct || (functi
605610
}
606611
})();
607612

608-
construct = function(constructor, args, target, _this) {
609-
if (Object['setPrototypeOf']) { //ES2015+
610-
let ret = _constructFunc(constructor, args, target);
613+
construct = function(target, args, constructor, _this, isClass) {
614+
if (isClass) { //ES2015+
615+
let ret = _constructFunc(target, args, constructor);
611616
// fix up the prototype so it matches the intended one, not one who's
612617
// prototype is the intended one :P
613618
Object['setPrototypeOf'](ret, target.prototype);
614619
return ret;
615620
}
616-
return constructor.apply(_this, args); //ES5
621+
return target.apply(_this, args); //ES5
617622
}

0 commit comments

Comments
 (0)