gpt4 book ai didi

javascript - XPCOM 中的依赖注入(inject)

转载 作者:行者123 更新时间:2023-11-28 03:01:08 28 4
gpt4 key购买 nike

我的一个 XPCOM 组件使用了其他 XPCOM 组件。当我测试它时,我发现它的依赖关系很麻烦。然后我想到使用依赖注入(inject)在我的构造函数中传递其他组件。我没有成功。是否可以将其他组件的引用传递到构造函数中?

 var _foo = Components.classes["@foo.com/foo;1"].createInstance(bar);
this.foo = _foo.QueryInterface(Components.interfaces.IFoo);

例如,foo组件需要bar。我可以通过 foo 的构造函数传入 bar 吗?我尝试了上面的方法,但没有成功。

最佳答案

Is it possible to pass in references of other components into your constructor?

没有。 createInstance 的参数是一个接口(interface); .createInstance(interface).createInstance().QueryInterface(interface) 的快捷方式。

您的问题的答案在于您在问题中未描述的区域 - 很可能您不需要从“构造函数”访问其他组件,或者您偶然发现了可以解决的问题,但相反“依赖项的麻烦 cos”并继续。

关于javascript - XPCOM 中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/844481/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com