如何使用闭包访问 Twitter @Anywhere javascript api?
我想做一些在经典的面向对象语言(如 Java、C# 等)中很容易做到的事情。我只是想访问实例化对象的属性。该对象在浏览器的窗口对象中具有全局作用域,并由 twitter @anywhere API 提供。
对于我的代码示例,假设您已经登录用户。
例如,如果我使用 java,我会说(假设所有字段都是公共的:
twttr = new twtter();
String screenName = twtter.currentUser.data('screen_name');
出于某种原因,这在 Javascript 中很难。我已经找到了一种解决方法 ,然后将其取出,但这很丑陋,但我只想直接访问它。
在 twitter Anywhere API 使用的匿名方法中,我将想要的值设置为 DOM元素 它甚至没有通过 eclipse 中的语法检查:
function AnywhereFacade()
{
var twitterReference;
window.twttr.anywhere
(
return function(T)
{
twitterReference = T;
};
)
getValue(propertyToGet)
{
return twitterReference.currentUser.data(propertyToGet);
}
};
var anywhereFacade = AnywhereFacade();
var screen_name = anywhereFacade.getValue("screen_name");
alert("screen name is: " + propertyGetter);
帮忙!为什么 Javascript 这么难使用?我想使用闭包。
请
I want to do something that in a classical object oriented language like Java, C# etc. is very easy to do. I simply want to access a property of an instantiated object. The object is globally scoped in the browser's window object, and provided by the twitter @anywhere API.
For my code examples, assume you have already logged the user in.
If I were using java for instance, I would say (assuming all fields were public:
twttr = new twtter();
String screenName = twtter.currentUser.data('screen_name');
For some reason, this is way hard in Javascript. I've gotten a workaround working where inside the anonymous method that the twitter anywhere API is using, I set the value I want to a DOM element, and fish it out later. This is ugly though. I just want to access it directly.
Here's what I have so far, which doesn't even pass syntax checks in eclipse:
function AnywhereFacade()
{
var twitterReference;
window.twttr.anywhere
(
return function(T)
{
twitterReference = T;
};
)
getValue(propertyToGet)
{
return twitterReference.currentUser.data(propertyToGet);
}
};
var anywhereFacade = AnywhereFacade();
var screen_name = anywhereFacade.getValue("screen_name");
alert("screen name is: " + propertyGetter);
Please help! Why is Javascript so hard to use anyway? What I'm trying to do is use a closure I think.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自从我使用 Facebook JavaScript SDK 和 Twitter SDK 并且希望提供一致的接口来访问这两者以来,我在我的应用程序中做了类似的事情。因此,我在
App
下为变量命名。对于任何地方的 Twitter,这就是捕获变量的方式。我们正在调用
anywhere
函数并向其传递一个回调函数。需要回调函数是因为此时可能尚未加载 Anywhere 库。通过传递整个函数,我们是说只要加载 Anywhere 库,就应该执行该函数。现在,当库加载时,该函数将执行,定义包含
getValue
函数的App.Twitter
属性。任何地方或T
对象都会在闭包中捕获。如果您现在调用,
则实际的任意位置对象 (
T
) 将用于获取 screen_name 属性。I have done something similar in my app since I am using the Facebook JavaScript SDK and Twitter SDK and want to provide a consistent interface to access both. So I namespace the variables under
App
. For twitter anywhere, this is how the variable is captured.We are calling the
anywhere
function and passing it a callback function. The callback function is needed because the anywhere library might not be loaded at this point. By passing the entire function, we are saying that this function should be executed whenever the anywhere library is loaded.Now when the library does load, this function will execute, define the
App.Twitter
property which contains agetValue
function. The anywhere orT
object is captured in the closure.If you now call,
the actually anywhere object (
T
), will be used to get the screen_name property.这就是我需要做的。
这让我意识到我的问题只是当 Twitter 从异步调用返回时我必须使用回调。这帮助我解决了如何将其包装为 gwt 的最初问题。
this is all I needed to do.
this made me realize my issue was just that I had to use a callback for when twitter returned from the async call. that helped me solve my initial problem of how to wrap it for gwt.