@@ -795,48 +795,52 @@ function createInjector(modulesToLoad, strictDi) {
795
795
}
796
796
}
797
797
798
- function invoke ( fn , self , locals , serviceName ) {
799
- if ( typeof locals === 'string' ) {
800
- serviceName = locals ;
801
- locals = null ;
802
- }
803
798
799
+ function injectionArgs ( fn , locals , serviceName ) {
804
800
var args = [ ] ,
805
- $inject = createInjector . $$annotate ( fn , strictDi , serviceName ) ,
806
- length , i ,
807
- key ;
801
+ $inject = createInjector . $$annotate ( fn , strictDi , serviceName ) ;
808
802
809
- for ( i = 0 , length = $inject . length ; i < length ; i ++ ) {
810
- key = $inject [ i ] ;
803
+ for ( var i = 0 , length = $inject . length ; i < length ; i ++ ) {
804
+ var key = $inject [ i ] ;
811
805
if ( typeof key !== 'string' ) {
812
806
throw $injectorMinErr ( 'itkn' ,
813
807
'Incorrect injection token! Expected service name as string, got {0}' , key ) ;
814
808
}
815
- args . push (
816
- locals && locals . hasOwnProperty ( key )
817
- ? locals [ key ]
818
- : getService ( key , serviceName )
819
- ) ;
809
+ args . push ( locals && locals . hasOwnProperty ( key ) ? locals [ key ] :
810
+ getService ( key , serviceName ) ) ;
820
811
}
812
+ return args ;
813
+ }
814
+
815
+
816
+ function invoke ( fn , self , locals , serviceName ) {
817
+ if ( typeof locals === 'string' ) {
818
+ serviceName = locals ;
819
+ locals = null ;
820
+ }
821
+
822
+ var args = injectionArgs ( fn , locals , serviceName ) ;
821
823
if ( isArray ( fn ) ) {
822
- fn = fn [ length ] ;
824
+ fn = fn [ fn . length - 1 ] ;
823
825
}
824
826
825
827
// http://jsperf.com/angularjs-invoke-apply-vs-switch
826
828
// #5388
827
829
return fn . apply ( self , args ) ;
828
830
}
829
831
832
+
830
833
function instantiate ( Type , locals , serviceName ) {
831
834
// Check if Type is annotated and use just the given function at n-1 as parameter
832
835
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
833
- // Object creation: http://jsperf.com/create-constructor/2
834
- var instance = Object . create ( ( isArray ( Type ) ? Type [ Type . length - 1 ] : Type ) . prototype || null ) ;
835
- var returnedValue = invoke ( Type , instance , locals , serviceName ) ;
836
-
837
- return isObject ( returnedValue ) || isFunction ( returnedValue ) ? returnedValue : instance ;
836
+ var ctor = ( isArray ( Type ) ? Type [ Type . length - 1 ] : Type ) ;
837
+ var args = injectionArgs ( Type , locals , serviceName ) ;
838
+ // Empty object at position 0 is ignored for invocation with `new`, but required.
839
+ args . unshift ( { } ) ;
840
+ return new ( Function . bind . apply ( ctor , args ) ) ;
838
841
}
839
842
843
+
840
844
return {
841
845
invoke : invoke ,
842
846
instantiate : instantiate ,
0 commit comments