TypeScript and member that doesn't give the same value -
i'm developing simple application , have class, in need console.log result of function passed in parameters.
context
i m trying make simple di container based on ng2 1 (simpler, understand way of work) based on api.
so, have injector class, binding class , resolvedbinding class. injector accepts binding in parameters create resolvedbinding objects
here injector class :
import {resolvedbinding} "./resolvedbinding"; import {binding} "./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) { (var 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>(); (var 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 { /* else ? */ } } } resolvedbindings.push(new resolvedbinding(binds[i].token, factory)); } return resolvedbindings; } }
here's binding class :
export class binding { private _token:any; private _object:any; constructor(token, object) { this._token = token; this._object = object; } public token():any { return this._token; } public object():any { return this._object; } }
and resolvedbinding class :
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 result; } public key():any { return this._key; } public set key(value:any) { this._key = value; } public factory():any { console.log(this._factory()); // gives undefined; return this._factory; } }
here's how call injector main class :
import { injector } './ioc/injector'; import { binding } './ioc/binding'; import { car } './ioc/car'; var injector = injector.resolveandcreate([ new binding(car, {toclass: car}), new binding(string, {tovalue: "hello !"}), 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
problem
the problem seems when instantiate resolvedbinding class , push array. in fact, _factory property has value in constructor, when try log getter, gives me undefined, , undefined when trying car instance.
the for
loop incorrectly captures loop variable here:
if (binds[i].object.tovalue) { factory = ()=> { return binds[i].object.tovalue; // <-- bad! } }
see javascript closure inside loops – simple practical example
Comments
Post a Comment