Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32414

Re: ES6 class properties are undefined ?

From Martin Honnen <martin.honnen@gmx.de>
Newsgroups comp.lang.javascript
Subject Re: ES6 class properties are undefined ?
Date 2017-01-29 12:01 +0100
Organization Liberty Development
Message-ID <o6ki2m$ek5$1@news.albasani.net> (permalink)
References <o6kc0n$jep$1@shakotay.alphanet.ch>

Show all headers | View raw


On 29.01.2017 10:18, Une Bévue wrote:
> I've a class like that (simplified for clarity):
>
> let SocketService = class SocketService {
>   constructor(url) {
>     this.url = url;
>     this.ws  = null;
>     this.timerID = 0;
>     this.init();
>   }
>   init() {
>     let that = this;
>     this.ws = new WebSocket(this.url);
>     this.ws.onclose = function() {
>       that.timerID = window.setInterval(that.init, 5000);
>     };
>   }
> }
>
> when I shut down the WebSocket server in order to test my script
> i get : this.url = undefined; (that.url too)
> only the first time init() is activated gives the correct value, why ?
> it seems "this" is lost...

Try whether using
   that.timerID = window.setInterval(function() { that.init(); }, 5000);
instead of
   that.timerID = window.setInterval(that.init, 5000);
improves things.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ES6 class properties are undefined ? Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-29 10:18 +0100
  Re: ES6 class properties are undefined ? Martin Honnen <martin.honnen@gmx.de> - 2017-01-29 12:01 +0100
    Re: ES6 class properties are undefined ? Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-29 12:14 +0100
      Re: ES6 class properties are undefined ? Martin Honnen <martin.honnen@gmx.de> - 2017-01-29 12:55 +0100
        Re: ES6 class properties are undefined ? Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-29 15:50 +0100
    Re: ES6 class properties are undefined ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-29 14:00 +0100
      Re: ES6 class properties are undefined ? Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-29 15:56 +0100

csiph-web