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


Groups > comp.lang.javascript > #32417

Re: ES6 class properties are undefined ?

Path csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: ES6 class properties are undefined ?
Date Sun, 29 Jan 2017 14:00:40 +0100
Organization PointedEars Software (PES)
Lines 75
Message-ID <1771785.oMNUckLgyt@PointedEars.de> (permalink)
References <o6kc0n$jep$1@shakotay.alphanet.ch> <o6ki2m$ek5$1@news.albasani.net>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1485694841 1318 eJwFwYEBwCAIA7CXYGuLnKMg/59gwl+uCogCh1OzvCsTWdhup7cj2iKouU7eMrjJYsG+E3odRxBt (29 Jan 2017 13:00:41 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sun, 29 Jan 2017 13:00:41 +0000 (UTC)
User-Agent KNode/4.14.2
X-NNTP-Posting-Host eJwFwQkBACAIA8BK8g2MI+L6R/AuDIKbjoAHgx3qsc/lUB+O06Ylc1Xrs1FhlpjYnOXs2vgnsBEu
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Cancel-Lock sha1:3R6PeieOOnLBypE7YDslrb/Iozc=
X-User-ID eJwNy9ENACEIBNGWWGDBK0dA+y/hzPy9ZGiB6PRgOO8rtMieppSU9a05tpBVRwt4tCXxHbrOVvZ6A9J2j3tS8ANfSBU3
Xref csiph.com comp.lang.javascript:32417

Show key headers only | View raw


Martin Honnen wrote:

> On 29.01.2017 10:18, Une Bévue wrote:
>> let SocketService = class SocketService {
       ^^^^^^^^^^^^^         ^^^^^^^^^^^^^
DRY.

>>   constructor(url) {
>>     this.url = url;
>>     this.ws  = null;
>>     this.timerID = 0;

Consider

  Object.assign(this, {
    url: url,
    ws: null,
    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.

Things can be improved more (i.e. more efficient) if the function is not 
created every time that the event listener is called:

  var init = function () { that.init(); };

  this.ws.onclose = function () {
    that.timerID = window.setInterval(init, 5000);
  };

Or, given that ES6 is already being used,

  var init = this.init.bind(this);

  this.ws.onclose = (function () {
    this.timerID = window.setInterval(init, 5000);
  }).bind(this);

so that the “that” variable becomes unnecessary.  [However, this comes at 
the price of flexibility: with .bind() for the event listener, “this” can no 
longer be used in it to refer to the object that fired the event.  So one 
might only want to use the first .bind() here.]

But in fact, this code does not make sense.  ISTM that the 
window.setInterval() should be window.setTimeout(); at least there should be 
a window.clearInterval() in .init() for the window.setInterval() in the 
event listener.  Otherwise whenever the WebSocket instance fires a “close” 
event, new "intervals" will pile up.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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