簡體   English   中英

Javascript訪問類之外的類方法?

[英]Javascript access class methods outside of class?

請參閱下面的示例:

(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

我怎樣才能做到這一點?

在JS中從IIFE公開API:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM