简体   繁体   中英

Javascript access class methods outside of class?

See my example below:

(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");  

})();

What i would like is to be able to access warbleSocket.send from outside of connectRetry

How can i accomplish this?

to expose API from IIFE in JS:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM