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


Groups > comp.lang.javascript > #34223

Re: removeeventlistener

Path csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.38.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: removeeventlistener
Date Sat, 28 Oct 2017 20:00:26 +0200
Organization PointedEars Software (PES)
Lines 64
Message-ID <19619642.EfDdHjke4D@PointedEars.de> (permalink)
References <e4ednXbpPvgtunnEnZ2dnUU7-bXNnZ2d@westnet.com.au> <2412105.mvXUDI8C0e@PointedEars.de> <1746259.PYKUYFuaPT@PointedEars.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
Injection-Info gwaiyur.mb-net.net; posting-host="38.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.237.38"; logging-data="8638"; mail-complaints-to="abuse@open-news-network.org"
User-Agent KNode/4.14.2
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
Cancel-Lock sha1:n5c3NuMqq33MzN9cCf3Aw/5C0SI=
X-User-ID U2FsdGVkX19lIicknf9BPwBFsNEte6048UV1R8Gm8pdukXxt9wIZxg==
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==
Xref csiph.com comp.lang.javascript:34223

Show key headers only | View raw


Thomas 'PointedEars' Lahn wrote:

>   var elementOvers = [];
> 
>   function setOver (elements)
>   {
>     /*
>      * NOTE:
>      * You may want to use ES 5 [].forEach.apply(elements, …) or
>      * ES 6 “for … of” on [].slice.call(elements) instead.
>      *
>      * In loops, cache the value of the “length” property unless you
>      * expect its value to change and want to handle that.
>      */
>     for (var i = 0, len = elements.length; i < len; ++i)
>     {
>       /* NOTE:
>        * You can still return the function reference instead.
>        * Just keep in mind that in the way it is used here,
>        * there is a closure over “i”.
>        *
>        * Consider using CSS “:hover” selectors based on class selectors
>        * instead of setting style properties here.
>        */
>       var elementOver = elementOvers[i] = function () {
>         this.style...
>       };

Supplemental:

If all listeners for the same event implement the same algorithm, you only 
need to define *one* such function such that it can be shared across 
function execution contexts –

  var elementOver = function () {
    // …
  };

–, and re-use the reference to it:

  function (…)
  {
    for (…)
    {
      elements[i].addEventListener("mouseover", elementOver, false);
    }
  }

  …

  function (…)
  {
    for (…)
    {
      elements[i].removeEventListener("mouseover", elementOver);
    }
  }

That would be much more efficient then.
 
-- 
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 | Find similar | Unroll thread


Thread

removeeventlistener Andrew Poulos <ap_prog@hotmail.com> - 2017-10-16 14:29 +1100
  Re: removeeventlistener Julio Di Egidio <julio@diegidio.name> - 2017-10-15 21:08 -0700
  Re: removeeventlistener Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2017-10-16 13:06 +0800
  Re: removeeventlistener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-16 22:18 +0200
    Re: removeeventlistener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-28 19:48 +0200
      Re: removeeventlistener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-28 20:00 +0200

csiph-web