Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7767392

Browse files
committed
fix($injector): instantiate returns instance, if non-object value returned from constructor
1 parent 3173d86 commit 7767392

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/Injector.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,15 @@ function createInjector(modulesToLoad) {
429429
}
430430
}
431431

432-
function instantiate(Type, locals){
433-
var Constructor = function(){},
434-
instance;
432+
function instantiate(Type, locals) {
433+
var Constructor = function() {},
434+
instance, returnedValue;
435+
435436
Constructor.prototype = Type.prototype;
436437
instance = new Constructor();
437-
return invoke(Type, instance, locals) || instance;
438+
returnedValue = invoke(Type, instance, locals);
439+
440+
return isObject(returnedValue) ? returnedValue : instance;
438441
}
439442

440443
return {

test/InjectorSpec.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,12 @@ describe('injector', function() {
606606

607607

608608
it('should allow constructor to return different object', function() {
609-
var t = $injector.instantiate(function() { return 'ABC'; });
610-
expect(t).toBe('ABC');
609+
var obj = {};
610+
var Class = function() {
611+
return obj;
612+
};
613+
614+
expect($injector.instantiate(Class)).toBe(obj);
611615
});
612616

613617

@@ -616,6 +620,25 @@ describe('injector', function() {
616620
$injector.instantiate(function() { throw 'MyError'; });
617621
}).toThrow('MyError');
618622
});
623+
624+
625+
it('should return instance if constructor returns non-object value', function() {
626+
var A = function() {
627+
return 10;
628+
};
629+
630+
var B = function() {
631+
return 'some-string';
632+
};
633+
634+
var C = function() {
635+
return undefined;
636+
};
637+
638+
expect($injector.instantiate(A) instanceof A).toBe(true);
639+
expect($injector.instantiate(B) instanceof B).toBe(true);
640+
expect($injector.instantiate(C) instanceof C).toBe(true);
641+
});
619642
});
620643

621644
describe('protection modes', function() {

0 commit comments

Comments
 (0)