开发者

Referencing Other Functions from Name Spaced JavaScript

So I am trying to consolidate a bunch of code into some nice NameSpaced functions, but am having a tough time getting it to all work together. For example, I have this (edited down for clarity):

YW.FB = function() {
   return {
        init: function(fncSuc, fncFail) {
                FB.init(APIKey, "/services/fbconnect/xd_receiver.htm");
                FB.Bootstrap.requireFeatures(["Conn开发者_运维技巧ect"]);
                if(typeof fncSuc=='function') fncSuc();
    },

        login: function(fncSuc) {
            this.FB.Connect.requireSession(function() { 
                if(typeof fncSuc=='function') fncSuc();
            });
      },
        getUserInfo: function() {
            var userInfo = new Object;
            FB.Facebook.apiClient.users_getInfo([FB.Facebook.apiClient.get_session().uid],["name"],function(result, ex){
                userInfo.name = result[0]['name'];
                userInfo.uid = result[0]['uid'];
                userInfo.url  = FBName.replace(/\s+/g, '-');
                return userInfo;
            })
        }
   };
}();

On a normal page I can just do:

  FB.init(APIKey, "/services/fbconnect/xd_receiver.htm");
  FB.Bootstrap.requireFeatures(["Connect"]);
  var userInfo = new Object;
  FB.Facebook.apiClient.users_getInfo([FB.Facebook.apiClient.get_session().uid],["name"],function(result, ex){
                userInfo.name = result[0]['name'];
                userInfo.uid = result[0]['uid'];
                userInfo.url  = FBName.replace(/\s+/g, '-');
                return userInfo;
            })

And it works.

I have been trying to do:

YW.init();
YW.login();
YW.getUserInfo();

But it doesn't work. I keep getting 'FB.Facebook is undefined' from YW.getUserInfo I could be doing this all wrong too. So the FB.init, FB.Facebook stuff is using the facebook connect libraries. Am I doing this all wrong?


If you look at the JavaScript that your browser has parsed in Firebug or a similar web debugger do you see the Facebook Connect JavaScript there? Looks like it's not in scope, and since FB is at the global level that means it's not in scope at all. Has nothing to do with namespaces. Global in JavaScript is global everywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜