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


Groups > comp.lang.javascript > #25915

Re: eventDispath how that work

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: eventDispath how that work
Date 2014-10-15 23:39 +0200
Organization PointedEars Software (PES)
Message-ID <2144937.rP2AIkvika@PointedEars.de> (permalink)
References <m1jblb$4a7$1@shakotay.alphanet.ch> <16yzpmtikrwk0$.1schjt968gcx$.dlg@40tude.net> <1791221.7QuZn8ShVB@PointedEars.de> <m1mk3b$9mh$1@shakotay.alphanet.ch>

Show all headers | View raw


Une Bévue wrote:

> Le 15/10/14 15:24, Thomas 'PointedEars' Lahn a écrit :
>> You need to write
>>
>>    var event = …;
>>
>> or you could accidentally access (and perhaps overwrite) the built-in
>> “event” property of the Window object of the global execution context,
>> which likely is in the scope chain.
> 
> 
> I posted :
> 
> event = ...
> 
> because this part of the script is "inside" another function.

Non sequitur.  There is no good (obvious) reason why you should declare 
“event” outside of the function, and every (obvious) reason to declare it 
local, inside.  In fact, the code that you posted at first –

     elem.addEventListener('build', function (e) {
         console.log("'elem' listener: event.type = " + event.type);
         console.log("'elem' listener: event.detail = " + event.detail);
       },
       false
     );

– *cannot* work without “event” referring to an object (or a value 
convertible to one) *before* the listener is called (which does not make 
sense).  [It has a chance to work with “var event = (typeof e == 
"undefined") ? window.event : e;” as first statement of the function.]
 
> clearly i have :
> 
>    var elem, event, test, body;
> ____________^^^^^____________  // out of any function then belongs to
> window object.

First of all, you had not posted this declaration before, and all our 
crystal balls are as b0rken as is the From header field value of your 
postings (which you want to fix before your next posting).

Second, why do you declare that many global variables?  The rule of thumb is 
to declare as few global variables as reasonably possible in order to avoid  
“spoiling the global namespace”.
 
> actually the elements listening to this event are :
> 
> document, body, test (the button who fired this event), para (a
> paragraphe) and i've added a pre (log).

In general, you do not need to declare global variables for elements that 
have listeners added for an event.  In fact, you want to avoid that because 
of potential memory leaks.
 
> for the time being only document, body, test are receiving this event,
> the paragraphe named para (a <p />) and a <pre /> named log aren't
> receiving this event the html structure is :
> 
> <html>
>    <head />

Invalid, see <http://validator.w3.org/>.

>    <body>
>      <p id='para'>...
>      <button id='test' data-date='14 oct 2014'>sendTest</button>
>      <div><pre id='log' ...

A “button” element inside a “p” element?  A “pre” element inside a “div” 
element?  Really?

> Then clearly, what said "JJ" :
>> Because dispatchEvent() trigger the event only on the element and its
>> child elements.
> 
> is not right

Yes, indeed.

> because document and body did received the event and body
> and document aren't child of test who fired this event.

Yes, dispatchEvent() dispatches custom events as any other event is 
dispatched.  It depends on the event (bubbling or not; cancelable and not 
canceled, or not) and the listeners (capturing or not) in which dispatch 
phase (capturing or bubbling), if any, the event can be handled.

<http://www.w3.org/TR/DOM-Level-3-Events/#event-flow>
 
> In fact I've found a workaround using some modified version of @author
> Erik Karlsson, www.nonobtrusive.com "class" Dispatcher().

Oh my.
 
> And this works very well talking into account suggestions of Aubrey
> Taylor <http://pastie.org/1913292#1> and Tobias
> <http://pastie.org/2975411#7,36> i can now dispatch event to any element
> regardless of the node structure.

For suitable values of “very well”.  You are blissfully unaware of the low 
quality of the code that you are using.

BTW, in English you need to write the standalone pronoun “I” with a capital 
letter *always*, and in general no space is written before punctuation.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
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

eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-14 16:23 +0200
  Re: eventDispath how that work JJ <jj4public@vfemail.net> - 2014-10-15 03:24 +0700
    Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-15 10:49 +0200
    Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-15 15:24 +0200
      Re: eventDispath how that work JJ <jj4public@vfemail.net> - 2014-10-16 02:10 +0700
        Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-15 23:02 +0200
          Re: eventDispath how that work Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-15 18:59 -0300
            Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-16 02:48 +0200
          Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-16 18:11 +0200
      Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-15 22:06 +0200
        Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-15 23:39 +0200
          Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-16 14:26 +0200
            Re: eventDispath how that work Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-16 14:39 -0300
              Re: eventDispath how that work Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-16 19:44 -0300
                Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-17 09:00 +0200
                Re: eventDispath how that work Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-17 10:16 -0300
                Re: eventDispath how that work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-17 21:40 +0200
                Re: eventDispath how that work Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-17 17:03 -0300
                Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-18 15:46 +0200
              Re: eventDispath how that work Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-17 08:26 +0200

csiph-web