本文介绍了使用RequireJS进行依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我可以伸展RequireJS为我的应用程序提供依赖注入多少?举个例子,假设我有一个我希望成为单身人士的模特。自执行getInstance()中不是单例 - 类型单例,而是上下文强制单例(每个上下文一个实例)。我想做点什么...How much can I stretch RequireJS to provide dependency injection for my app? As an example, let's say I have a model that I want to be a singleton. Not a singleton in a self-enforcing getInstance()-type singleton, but a context-enforced singleton (one instance per "context"). I'd like to do something like...require(['mymodel'], function(mymodel) { ...}让mymodel成为MyModel类的一个实例。如果我要在多个模块中执行此操作,我希望mymodel是相同的共享实例。And have mymodel be an instance of the MyModel class. If I were to do this in multiple modules, I would want mymodel to be the same, shared instance.我已成功完成此工作,方法是将mymodel模块设为这样:I have successfully made this work by making the mymodel module like this:define(function() { var MyModel = function() { this.value = 10; } return new MyModel();});这种用法是预期和常见的,还是我滥用RequireJS?有没有更合适的方式我可以使用RequireJS执行依赖注入?感谢您的帮助。仍然试图掌握这一点。Is this type of usage expected and common or am I abusing RequireJS? Is there a more appropriate way I can perform dependency injection with RequireJS? Thanks for your help. Still trying to grasp this.推荐答案这实际上不是依赖注入,而是服务位置:您的其他模块通过字符串key请求类,然后返回n的实例是服务定位器(在这种情况下是RequireJS)已被连线以提供它们。This is not actually dependency injection, but instead service location: your other modules request a "class" by a string "key," and get back an instance of it that the "service locator" (in this case RequireJS) has been wired to provide for them.依赖注入将涉及返回 MyModel 构造函数,即返回MyModel ,然后在中心组合根中注入 MyModel 到其他实例中。我在这里汇总了一个如何工作的样本: https://gist.github.com/1274607 (也在下面引用)Dependency injection would involve returning the MyModel constructor, i.e. return MyModel, then in a central composition root injecting an instance of MyModel into other instances. I've put together a sample of how this works here: https://gist.github.com/1274607 (also quoted below)这样,组合根确定是否分发 MyModel的单个实例(即使它成为单例作用域)或每个需要它的类(实例作用域)或其中之间的新类。该逻辑既不属于MyModel的定义,也不属于要求其实例的类。This way the composition root determines whether to hand out a single instance of MyModel (i.e. make it singleton scoped) or new ones for each class that requires it (instance scoped), or something in between. That logic belongs neither in the definition of MyModel, nor in the classes that ask for an instance of it.(旁注:虽然我没有使用它, wire.js 是一个看起来非常酷的JavaScript的完整依赖注入容器。)(Side note: although I haven't used it, wire.js is a full-fledged dependency injection container for JavaScript that looks pretty cool.)你并不一定像你一样使用它来滥用RequireJS,虽然你所做的似乎有点迂回,即声明一个类而不是返回一个新的实例。为什么不这样做呢?You are not necessarily abusing RequireJS by using it as you do, although what you are doing seems a bit roundabout, i.e. declaring a class than returning a new instance of it. Why not just do the following?define(function () { var value = 10; return { doStuff: function () { alert(value); } };});您可能缺少的类比是模块在大多数其他语言中等同于名称空间,尽管您可以将函数和值附加到的命名空间。 (因此更像Python而不是Java或C#。)它们不等同于类,尽管如您所示,您可以使模块的导出等于给定类实例的导出。The analogy you might be missing is that modules are equivalent to "namespaces" in most other languages, albeit namespaces you can attach functions and values to. (So more like Python than Java or C#.) They are not equivalent to classes, although as you have shown you can make a module's exports equal to those of a given class instance.因此,您可以通过将函数和值直接附加到模块来创建单例,但这有点像使用静态类创建单例:它非常不灵活,通常不是最佳实践。但是,大多数人都把它们的模块视为静态类,因为正确构建一个依赖注入系统需要从一开始就考虑很多,这在JavaScript中并不是真正的常态。So you can create singletons by attaching functions and values directly to the module, but this is kind of like creating a singleton by using a static class: it is highly inflexible and generally not best practice. However, most people do treat their modules as "static classes," because properly architecting a system for dependency injection requires a lot of thought from the outset that is not really the norm in JavaScript.这是 https://gist.github.com/1274607 内联:// EntryPoint.jsdefine(function () { return function EntryPoint(model1, model2) { // stuff };});// Model1.jsdefine(function () { return function Model1() { // stuff };});// Model2.jsdefine(function () { return function Model2(helper) { // stuff };});// Helper.jsdefine(function () { return function Helper() { // stuff };});// composition root, probably your main moduledefine(function (require) { var EntryPoint = require("./EntryPoint"); var Model1 = require("./Model1"); var Model2 = require("./Model2"); var Helper = require("./Helper"); var entryPoint = new EntryPoint(new Model1(), new Model2(new Helper())); entryPoint.start();}); 这篇关于使用RequireJS进行依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 01:02