Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37122
| Path | csiph.com!.POSTED.208-71-239-109.cust.centrio.cz!not-for-mail |
|---|---|
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: bind and addEventListener/removeEventListener |
| Date | Tue, 13 Aug 2019 20:43:44 +0200 |
| Organization | csiph.com Internet News Service |
| Message-ID | <qiv0d0$9vr$1@csiph.com> (permalink) |
| References | <qiukun$4ck$1@csiph.com> <975e2d9e-b69c-4fe2-9ede-a88eddddb6f7@googlegroups.com> <4b5c991a-e951-40a6-b2ca-7baaac4cb78e@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 13 Aug 2019 18:43:45 -0000 (UTC) |
| Injection-Info | csiph.com; posting-host="208-71-239-109.cust.centrio.cz:109.239.71.208"; logging-data="10235"; mail-complaints-to="admin@kev009.com" |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 |
| In-Reply-To | <4b5c991a-e951-40a6-b2ca-7baaac4cb78e@googlegroups.com> |
| Content-Language | en-US |
| Xref | csiph.com comp.lang.javascript:37122 |
Show key headers only | View raw
On 13/08/2019 20:29, Julio Di Egidio wrote:
> On Tuesday, 13 August 2019 17:43:17 UTC+2, Julio Di Egidio wrote:
>> On Tuesday, 13 August 2019 17:28:29 UTC+2, Cezary Tomczyk wrote:
>>> Bind
>>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
>>> creates a new function.
>>>
>>> Let's say we have #1:
>>>
>>> document.body.addEventListener('click', myExampleClass.method.bind(this));
>>>
>>> and then:
>>>
>>> document.body.removeEventListener('click', myExampleClass.method);
>>>
>>> based on my research it should not be correct as the function created by
>>> bind is different than the original passed by reference in
>>> document.body.removeEventListener.
>>>
>>> If it is not correct then the approach like:
>>>
>>> var myRef = myExampleClass.method.bind(this);
>>
>> myExampleObject please...
>>
>>> document.body.addEventListener('click', myRef);
>>> document.body.removeEventListener('click', myRef);
>>>
>>> should be more appropriate, I guess.
>>>
>>> Need some thoughts on that.
>>
>> Sounds correct to me.
>
> But I think you could in fact do something like this:
>
> document.body.addEventListener('click', myobj.method.bind(this));
>
> document.body.removeEventListener('click', myobj.method.bind(this));
>
> as long as the two happen within the same closure...
>
> My point being, the two handlers do not have to match by some kind of
> low-level reference equality, it's about being the same closure (functions
> are closures in JS) and the comparison is essentially lexical (JS is a
> functional language).
That's the thing I am not sure - reference equality.
> (Anyone please correct me if I am wrong, cannot find the relevant piece of
> documentation right now: but I do remember using e.g. inline functions and
> other complications no prob...)
Okay, let's see if anyone else have opinions on that.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
bind and addEventListener/removeEventListener Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2019-08-13 17:28 +0200
Re: bind and addEventListener/removeEventListener Julio Di Egidio <julio@diegidio.name> - 2019-08-13 08:43 -0700
Re: bind and addEventListener/removeEventListener Julio Di Egidio <julio@diegidio.name> - 2019-08-13 11:29 -0700
Re: bind and addEventListener/removeEventListener Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2019-08-13 20:43 +0200
Re: bind and addEventListener/removeEventListener Julio Di Egidio <julio@diegidio.name> - 2019-08-13 11:48 -0700
Re: bind and addEventListener/removeEventListener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-08-13 20:58 +0200
Re: bind and addEventListener/removeEventListener Julio Di Egidio <julio@diegidio.name> - 2019-08-13 12:36 -0700
csiph-web