2015-05-29 35 views
0

我實際上正在開發一個簡單的應用程序,我有這個類,其中我需要console.log函數傳遞參數的結果。TypeScript和成員,不給出相同的值

語境

餘米試圖使基於NG2一個簡單的DI容器(簡單,要真正瞭解它的工作方式)基於API。

所以,我有一個Injector類,一個Binding類和一個ResolvedBinding類。 噴油器接受參數綁定創建ResolvedBinding對象

這裏噴油器類:

import {ResolvedBinding} from "./ResolvedBinding"; 
import {Binding} from "./Binding"; 


export class Injector { 


    private _binds:Array<ResolvedBinding>; 


    constructor(bindings:ResolvedBinding[]) { 
     this._binds = bindings; 
    } 

    public static ResolveAndCreate(binds:Array<Binding>):Injector { 
     return new Injector(Injector.resolve(binds)); 
    } 

    get(token:any) { 
     for (var i in this._binds) { 
      if (this._binds[i].key === token) { 
       return this._binds[i].factory(); 
       /*if(new this._binds[i].factory instanceof Object) 

       else 
       return this._binds[i].factory();*/ 
      } 
     } 

     return null; 
    } 

    static resolve(binds:Array<Binding>):Array<ResolvedBinding> { 
     var resolvedBindings = new Array<ResolvedBinding>(); 

     for (var i in binds) { 
      var factory; 

      if (binds[i].object.toClass) { 
       factory = binds[i].object.toClass; 
      } else { 
       if (binds[i].object.toFactory) { 
        factory = binds[i].object.toFactory; 
       } 
       else { 
        if (binds[i].object.toValue) { 
         factory =()=> { 
          return binds[i].object.toValue; 
         } 
        } 
        else { 
         /* what else ? */ 
        } 
       } 
      } 

      resolvedBindings.push(new ResolvedBinding(binds[i].token, factory)); 
     } 


     return resolvedBindings; 
    } 
} 

這裏的綁定類:

export class Binding { 

    private _token:any; 
    private _object:any; 

    constructor(token, object) { 
     this._token = token; 
     this._object = object; 
    } 

    public get token():any { 
     return this._token; 
    } 

    public get object():any { 
     return this._object; 
    } 
} 

而且ResolvedBinding類:

export class ResolvedBinding { 

    private _key:any; 
    private _factory:any; 

    constructor(key:any, factory:any) { 
     this._key = key; 
     this._factory = factory; 
     console.log(this._factory()); // gives the good result; 
    } 

    public get key():any { 
     return this._key; 
    } 

    public set key(value:any) { 
     this._key = value; 
    } 

    public get factory():any { 
     console.log(this._factory()); // gives undefined; 
     return this._factory; 
    } 

} 

以下是我如何從我的主要調用注入器SS:

import { Injector } from './Ioc/Injector'; 
import { Binding } from './Ioc/Binding'; 
import { Car } from './Ioc/Car'; 

var injector = Injector.ResolveAndCreate([ 
    new Binding(Car, {toClass: Car}), 
    new Binding(String, {toValue: "Hello you !"}), 
    new Binding(Number, { 
     toFactory:() => { 
      return 1 + 2; 
     } 
    }) 
]); 

var carC = injector.get(Car); 
var myString = injector.get(String); 
var myNumber = injector.get(Number); 


console.log(carC); // undefined 
console.log(myString); // undefined 
console.log(myNumber); // 3 

問題

的問題似乎是當我實例ResolvedBinding類並將其推到陣列。實際上,_factory屬性在構造函數中有一個值,但是當我嘗試將其記錄到getter中時,它給了我一個未定義的值,因此在嘗試獲取Car實例時會得到一個未定義的值。

+0

請提供您正在使用運行/使用這個類的代碼。 – Brocco

+0

我編輯了我的文章 – mfrachet

+0

我已經通過操場(http://www.typescriptlang.org/Playground)在鉻和Safari瀏覽器中運行此操作,並且獲得了成功的結果,而不是未定義的 – Brocco

回答

相關問題