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