Javascript访问类外的类方法?

时间:2013-10-03 09:09:58

标签: javascript

请参阅下面的示例:

(function() {
  // Initialize the socket & handlers
  var connectToServer = function() {
    var warbleSocket = new SockJS('http://url.com:5555/warble');

    warbleSocket.onopen = function() {
      clearInterval(connectRetry);
      $('.connect-status')
        .removeClass('disconnected')
        .addClass('connected')
        .text('Connected');
    };

    warbleSocket.onmessage = function(e) {
      $('#warble-msg').text(e.data);
    };

    warbleSocket.onclose = function() {
      clearInterval(connectRetry);
      connectRetry = setInterval(connectToServer, 1000);
      $('.connect-status')
        .removeClass('connected')
        .addClass('disconnected')
        .text('Disconnected');
    };

    // Connect the text field to the socket
    $('.msg-sender').off('input').on('input', function() {
      warbleSocket.send($('.msg-sender input').val()); 
    });
  };
  var connectRetry = setInterval(connectToServer, 1000);



  connectRetry.warbleSocket.send("Hi there");  

})();

我希望能够从warbleSocket.send

之外访问connectRetry

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

在JS中公开来自IIFE的API:

...
    return {
        send: warbleSocket.send
    }
})();

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

相关问题