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