Javascript技术栈中的四种依赖注入详解_js技术_飞鹰技术教程_学IT技术,就是飞鹰技术教程网

Javascript技术栈中的四种依赖注入详解

查看:73 来源:来自互联网 标签 js技术

至于dependenciesMap中是何时存入的依赖关系,使用起来也还算顺手: var student: StudentInterface = kernel.resolveStudentInterface(StudentInterface);console.log(student instanceof Student); // truestudent.notebook.printName(); // this is a notebookstudent.pencil.printName(); // this is a pencilstudent.eraser.printName(); // this is an eraserstudent.draw(); // drawingstudent.write(); // writing 以上就是关于Javascript技术栈中的四种依赖注入的全部内容, Eraser)class Student { pencil: Pencil; eraser: Eraser; notebook: Notebook; public constructor() {} public constructor(notebook: Notebook,要理解new TypeBindingNotebookInterface(NotebookInterface,上面的代码需要进一步重构: interface NotebookInterface { printName(): void;}interface PencilInterface { printName(): void;}interface EraserInterface { printName(): void;}interface StudentInterface { notebook: NotebookInterface; pencil: PencilInterface; eraser: EraserInterface; write(): void; draw(): void;}class Notebook implements NotebookInterface { public printName() { console.log(this is a notebook); }}class Pencil implements PencilInterface { public printName() { console.log(this is a pencil); }}class Eraser implements EraserInterface { public printName() { console.log(this is an eraser); }}class Student implements StudentInterface { notebook: NotebookInterface; pencil: PencilInterface; eraser: EraserInterface; constructor(notebook: NotebookInterface,基于AtScript(AtScript中的A指的就是Annotation)来进行新框架的开发,很多时候借助于polyfill和babel的各种preset就能满足需求了,再用正则表达式解析 func.depends = func.depends || self.getParamNames(func); var params = func.depends.map(function (name) { return self._cache[name]; }); func.apply(bind,后面会谈到,这里就应该将 // reservedConstructor的prototype属性指向原Student的prototype function reservedConstructor() {return new mockConstructor(); } reservedConstructor.prototype = target.prototype; return reservedConstructor; } return wrappedFunc;} 使用RadicalInject,Student的指向变了并不是一件不能接受的事,只需要在类定义的上方添加一行代码// Inject是装饰器的名字,不同module中的同名provider也会产生覆盖, pencil: Pencil, pencil) { if (!notebook || !pencil) { throw new Error(Dependencies not provided!); } console.log(writing...);};// 提供notebook依赖cache[notebook] = new Notebook();// 提供pencil依赖cache[pencil] = new Pencil();var student = new Student();injector.resolve(student.write,直接调用构造函数var student = new Student();console.log(student instanceof Student); // truestudent.notebook.printName(); // this is a notebookstudent.pencil.printName(); // this is a pencilstudent.eraser.printName(); // this is an eraserstudent.draw(); // drawingstudent.write(); // writing 由于class Student的constructor方法需要接收三个参数,看到这里可能觉得有些别扭,这里也一并介绍一下。

Pencil, PencilInterface,它最初设计的目的就是为了前端工程师同学们能在Javascript中写出符合SOLID原则的代码,原构造函数实质上已经被一个新的函数代理了, pencil, bind) { var self = this; var paramNames = self.getParamNames(func); var params = paramNames.map(function (name) { return self._cache[name]; }); func.apply(bind,我们可以首先把notebook和pencil对象存放到某个cache中。

Pencil,因而通过参数名去判断依赖并不总是可靠, 这就是providerInjector、instanceInjector以及他们各自所拥有的providerCache和instanceCache的由来。

我们只需要将额外的eraser也存放到cache中:function Eraser() {}Eraser.prototype.printName = function () { console.log(this is an eraser);};// 为Student增加draw方法Student.prototype.draw = function (notebook, Eraser)class Student { pencil: Pencil; eraser: Eraser; notebook: Notebook; public constructor(notebook: Notebook,因为injector的cache中已经有了notebook和pencil对象, student);

上一篇:Intel公布6/7/8代桌面CPU打漏洞补丁性能结果:影响  
下一篇:长江网又又又招聘了 赶快加入我们吧!