如何访问先前 YUI.use() 调用中定义的函数
[我是 YUI 新手]
我正在编写一个 Chrome 扩展程序,需要更改使用 YUI3 框架创建的网页的内容。
我已经确定,加载加载后在页面中运行的 JavaScript 的扩展必须调用先前在 YUI.add() 调用中定义的函数。
运行的原始 YUI 代码是这样的:
YUI.add("uuu", function (c) {
...
c.theObject = niceStuff;
}
...
YUI().use("uuu", function (c) {
c.theObject.doSomething();
}
是否有可能在这段代码运行后,我可以访问 c.theObject 的函数?
(我知道这可能违背 YUI3 良好的沙箱机制,但是这就是我在这里完成工作所需要的)。
[I'm a YUI newbie]
I'm writing a Chrome extension that needs to change the contents of a web page created using the YUI3 framework.
I've identified that the extension, which injects javascript that runs in the page after it is loaded, must call a function that was previously defined in a YUI.add() call.
The original YUI code that runs is something like this:
YUI.add("uuu", function (c) {
...
c.theObject = niceStuff;
}
...
YUI().use("uuu", function (c) {
c.theObject.doSomething();
}
Is it possible that after this code runs, I can access a function of c.theObject?
(I understand this might go against YUI3's nice sandbox mechanism, but it's what I need to get the job done here).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会遇到问题,因为每当创建 YUI() 实例时,它都会为您构建一个新的沙箱。除了少数例外,YUI 模块完全由其沙箱上下文包围。例如:
如果从未将其分配给沙箱函数范围之外的变量,则您很可能无法访问所需的
theObject
的特定实例。如果 theObject 的任何实例都可以,您只需调用 YUI API 并获取您自己的版本即可使用。You might have problems because any time a YUI() instance is created, it builds you a new sandbox. With a few exceptions, YUI modules are completely boxed by their sandbox context. For example:
It's very possible that you may not be able to access the specific instance of
theObject
that you need, if it's never assigned to a variable outside the sandbox function scope. If any instance of theObject will do, you can just call into the YUI API and get your own version to play with.这对我有用: http://jsfiddle.net/sMAQx/1/
This works for me: http://jsfiddle.net/sMAQx/1/
一种方法是在“使用”YUI() 实例后捕获它。像这样:
One way to do it is to capture the YUI() instance after you 'use' it. Like this: