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


Groups > comp.lang.javascript > #23182 > unrolled thread

How to detect key pressed plus mouse click, e.g., ctrl + left click

Started by"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
First post2014-02-21 10:45 -0800
Last post2014-02-23 23:39 +0100
Articles 19 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  How to detect key pressed plus mouse click, e.g., ctrl + left click "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-02-21 10:45 -0800
    Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-21 20:04 +0100
      Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-02-21 11:21 -0800
        Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-21 20:32 +0100
          Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-02-21 14:24 -0800
            Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-22 00:31 +0100
              Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-02-21 17:41 -0800
          Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Andrew Poulos <ap_prog@hotmail.com> - 2014-02-22 12:48 +1100
            Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-22 10:36 +0100
            Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 10:54 +0100
              Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-22 11:19 +0100
                Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 11:27 +0100
                  Re: How to detect key pressed plus mouse click, e.g., ctrl + left click "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-22 11:39 +0100
                    Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 12:50 +0100
                      Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 12:56 +0100
                      Re: How to detect key pressed plus mouse click, e.g., ctrl + left click John Harris <niam@jghnorth.org.uk.invalid> - 2014-02-23 14:00 +0000
              Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 23:48 +0100
                Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-23 20:04 -0300
              Re: How to detect key pressed plus mouse click, e.g., ctrl + left click Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 23:39 +0100

#23182 — How to detect key pressed plus mouse click, e.g., ctrl + left click

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-02-21 10:45 -0800
SubjectHow to detect key pressed plus mouse click, e.g., ctrl + left click
Message-ID<pe7fg9dfqg4omh5u888q0vnduas63i47d3@4ax.com>
I know how to detect a mouse click and I know how to return a key's value,
but I can't figure out how to detect the combination of key + mouse. I
need to detect when a user presses the ctrl key and clicks a mouse button.
Can someone point the way?

TIA,
-- 
Joey

[toc] | [next] | [standalone]


#23183

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-21 20:04 +0100
Message-ID<XnsA2DBCC37E59CCeejj99@194.109.133.133>
In reply to#23182
"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21 feb 
2014 in comp.lang.javascript:

> I know how to detect a mouse click and I know how to return a key's value,
> but I can't figure out how to detect the combination of key + mouse. I
> need to detect when a user presses the ctrl key and clicks a mouse button.
> Can someone point the way?

Indeed you are still learning, 10 seconds googling gave me an answer:

<http://www.javascripter.net/faq/ctrl_alt.htm>

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23184

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-02-21 11:21 -0800
Message-ID<tn9fg95v13j6c51bjs89qidr75s5kttk3g@4ax.com>
In reply to#23183
Evertjan. wrote:

>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21 feb 
>2014 in comp.lang.javascript:
>
>> I know how to detect a mouse click and I know how to return a key's value,
>> but I can't figure out how to detect the combination of key + mouse. I
>> need to detect when a user presses the ctrl key and clicks a mouse button.
>> Can someone point the way?
>
>Indeed you are still learning, 10 seconds googling gave me an answer:
>
><http://www.javascripter.net/faq/ctrl_alt.htm>

Thanks. That's probably the only page I didn't find when searching Google.

And, yes, I am still learning and hopefully will until I'm an old man. ;-)
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23185

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-21 20:32 +0100
Message-ID<XnsA2DBD0E11E61Eeejj99@194.109.133.133>
In reply to#23184
"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21
feb 2014 in comp.lang.javascript: 

> Evertjan. wrote:
> 
>>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21
>>feb 2014 in comp.lang.javascript:
>>
>>> I know how to detect a mouse click and I know how to return a key's
>>> value, but I can't figure out how to detect the combination of key +
>>> mouse. I need to detect when a user presses the ctrl key and clicks a
>>> mouse button. Can someone point the way?
>>
>>Indeed you are still learning, 10 seconds googling gave me an answer:
>>
>><http://www.javascripter.net/faq/ctrl_alt.htm>
> 
> Thanks. That's probably the only page I didn't find when searching
> Google. 

It was the top response searching for this string:

javascript ctrl mouseclick
 
> And, yes, I am still learning and hopefully will until I'm an old man.
> ;-) 

Anyway, that page is last century coding,
try this [chrome and IE11 tested]:

<script type='text/javascript'>
document.onmousedown = function(e) {
  var e = e || window.event;
  if (e.shiftKey || e.altKey || e.ctrlKey) 
    alert ( "shiftKey=" + e.shiftKey + "\naltKey=" 
    + e.altKey + "\nctrlKey=" + e.ctrlKey );
};
</script>



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23186

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-02-21 14:24 -0800
Message-ID<abkfg997e9cg59mfusq6fin02n3l7einb6@4ax.com>
In reply to#23185
Evertjan. wrote:

>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21
>feb 2014 in comp.lang.javascript: 
>
>> Evertjan. wrote:
>> 
>>>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21
>>>feb 2014 in comp.lang.javascript:
>>>
>>>> I know how to detect a mouse click and I know how to return a key's
>>>> value, but I can't figure out how to detect the combination of key +
>>>> mouse. I need to detect when a user presses the ctrl key and clicks a
>>>> mouse button. Can someone point the way?
>>>
>>>Indeed you are still learning, 10 seconds googling gave me an answer:
>>>
>>><http://www.javascripter.net/faq/ctrl_alt.htm>
>> 
>> Thanks. That's probably the only page I didn't find when searching
>> Google. 
>
>It was the top response searching for this string:
>
>javascript ctrl mouseclick

I searched for 'keypress with mouseclick'
> 
>> And, yes, I am still learning and hopefully will until I'm an old man.
>> ;-) 
>
>Anyway, that page is last century coding,
>try this [chrome and IE11 tested]:
>
><script type='text/javascript'>
>document.onmousedown = function(e) {
>  var e = e || window.event;
>  if (e.shiftKey || e.altKey || e.ctrlKey) 
>    alert ( "shiftKey=" + e.shiftKey + "\naltKey=" 
>    + e.altKey + "\nctrlKey=" + e.ctrlKey );
>};
></script>

Thanks, I ended up with (looking only for the shift key):

function keyDown(e) {
	var shiftPressed=0;
	var evt = e ? e:window.event;
	shiftPressed=evt.shiftKey;
	return shiftPressed;
}
Thanks again for your help.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23187

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-22 00:31 +0100
Message-ID<XnsA2DC563B7327eejj99@194.109.133.133>
In reply to#23186
"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21 feb 
2014 in comp.lang.javascript:

> function keyDown(e) {
>      var shiftPressed=0;

Why this setting to 0, 
as the return value will be either true or false?

>      var evt = e ? e:window.event;
>      shiftPressed=evt.shiftKey;
>      return shiftPressed;
>}

No need for all these intermediate variables.


document.onmousedown = function(e) {
   alert( (e || window.event).shiftKey );
};

================================
or

document.onmousedown = function(e) {
   if ( (e || window.event).shiftKey )
      alert('ShiftClick detected');
};



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23188

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-02-21 17:41 -0800
Message-ID<4vvfg9lhvefsl7qqjiavaenk7lbub40s2r@4ax.com>
In reply to#23187
Evertjan. wrote:

>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote on 21 feb 
>2014 in comp.lang.javascript:
>
>> function keyDown(e) {
>>      var shiftPressed=0;
>
>Why this setting to 0, 
>as the return value will be either true or false?

Yes. It's leftover from a test.
>
>>      var evt = e ? e:window.event;
>>      shiftPressed=evt.shiftKey;
>>      return shiftPressed;
>>}
>
>No need for all these intermediate variables.
>
>
>document.onmousedown = function(e) {
>   alert( (e || window.event).shiftKey );
>};
>
>================================
>or
>
>document.onmousedown = function(e) {
>   if ( (e || window.event).shiftKey )
>      alert('ShiftClick detected');
>};
Thanks, once more.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23189

FromAndrew Poulos <ap_prog@hotmail.com>
Date2014-02-22 12:48 +1100
Message-ID<LaidnWADIL12nJXOnZ2dnUVZ_i2dnZ2d@westnet.com.au>
In reply to#23185
On 22/02/2014 6:32 AM, Evertjan. wrote:

> document.onmousedown = function(e) {
>    var e = e || window.event;
>    if (e.shiftKey || e.altKey || e.ctrlKey)
>      alert ( "shiftKey=" + e.shiftKey + "\naltKey="
>      + e.altKey + "\nctrlKey=" + e.ctrlKey );
> };

I notice that you put a "var" before the "e". Is that really necessary?

With


var e = "1";

function foo(e) {
   window.alert(e);
}

foo("2");


wouldn't the alert display "2"?

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#23190

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-22 10:36 +0100
Message-ID<XnsA2DC6BF349CF0eejj99@194.109.133.133>
In reply to#23189
Andrew Poulos <ap_prog@hotmail.com> wrote on 22 feb 2014 in 
comp.lang.javascript:

>> document.onmousedown = function(e) {
>>    var e = e || window.event;
>>    if (e.shiftKey || e.altKey || e.ctrlKey)
>>      alert ( "shiftKey=" + e.shiftKey + "\naltKey="
>>      + e.altKey + "\nctrlKey=" + e.ctrlKey );
>> };
> 
> I notice that you put a "var" before the "e". Is that really necessary?

Yes.

Otherwise the statement could overwrite an "outside" variable e:

var e = 3;
alert(e); // 3
test2();
alert(e); // 3
test1();
alert(e); // 5

function test1() {
   e = 5;
};

function test2() {
   var e = 7;
};





-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23191

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-22 10:54 +0100
Message-ID<3506285.IpskKQNfjB@PointedEars.de>
In reply to#23189
Andrew Poulos wrote:

> On 22/02/2014 6:32 AM, Evertjan. wrote:
>> document.onmousedown = function(e) {
>>    var e = e || window.event;
>>    if (e.shiftKey || e.altKey || e.ctrlKey)
>>      alert ( "shiftKey=" + e.shiftKey + "\naltKey="
>>      + e.altKey + "\nctrlKey=" + e.ctrlKey );
>> };
> 
> I notice that you put a "var" before the "e". Is that really necessary?

No.  As “e” is in the formal parameter list, it is treated as as if it was a 
local variable.  Redeclaring a local variable has no effect.  Initializing 
it like that also has not.

A local variable can come in handy if the original value of the argument of 
the function call is needed later.  The local variable SHOULD have a 
different identifier then (otherwise it would prevent accesses to the 
argument value by parameter name).  Only insofar development tools (like 
Eclipse JSDT) that warn about parameter assignment are useful here, because 
ECMAScript implementations support only call-by-value (references to objects 
are values; properties and variables cannot be referenced in terms of their 
memory locations): Accidental assignments to the parameter itself will not 
change a value in the calling execution contexts.

> With
> 
> 
> var e = "1";
> 
> function foo(e) {
>    window.alert(e);
> }
> 
> foo("2");
> 
> 
> wouldn't the alert display "2"?

No, it would.  However, the whole operation is bogus.  It should be at least

  if (!e) e = window.event;

so that the assignment only takes place when necessary.  Further feature 
tests are recommended, see jsx.dom.createEventListener() [1].

[1] <http://PointedEars.de/wsvn/JSX/trunk/dom/events.js>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23192

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-22 11:19 +0100
Message-ID<XnsA2DC7335FFCD4eejj99@194.109.133.133>
In reply to#23191
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 22 feb 2014 in 
comp.lang.javascript:

>   if (!e) e = window.event;
> 
> so that the assignment only takes place when necessary.

e || window.event

will do fine.

I [most probably] all applications, 
window.event will only be called if e does convert to false.

====================
temp = e || alert('e == false');
var e = 3;
temp = e || alert('e == false');
====================


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23193

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-22 11:27 +0100
Message-ID<2476103.KEgCaNTNbC@PointedEars.de>
In reply to#23192
Evertjan. wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 22 feb 2014 in
> comp.lang.javascript:

It's attribution _line_, not attribution *novel*.
 
>>   if (!e) e = window.event;
>> 
>> so that the assignment only takes place when necessary.
> 
> e || window.event
> 
> will do fine.
> 
> I [most probably] all applications,
> window.event will only be called if e does convert to false.

1. Nothing will be *called* here.

2. That is beside the point; the *assignment* will take place this way
   even if not necessary.

3. If a less simple test is necessary (for example, in order to prevent
   conversion to boolean), the now simple expression will quickly become
   unreadable, and hard to maintain.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23194

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-02-22 11:39 +0100
Message-ID<XnsA2DC768E586D7eejj99@194.109.133.133>
In reply to#23193
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 22 feb 2014 in 
comp.lang.javascript:

>> e || window.event
>> 
>> will do fine.
>> 
>> I [most probably] all applications,
>> window.event will only be called if e does convert to false.
> 
> 1. Nothing will be *called* here.

Depending on definition, it will.
 
> 2. That is beside the point; the *assignment* will take place this way
>    even if not necessary.

Depending on definition, it will not.
 
> 3. If a less simple test is necessary 
> (for example, in order to prevent conversion to boolean),

Quod non.

> the now simple expression will quickly become
> unreadable, and hard to maintain.

That is beside the point here, 
because this is not about boolean.

e || window.event

should be so common that your mind will recognize it at once
for the near future.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23195

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-22 12:50 +0100
Message-ID<6801584.FWXuJcOkgI@PointedEars.de>
In reply to#23194
Evertjan. wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 22 feb 2014 in
> comp.lang.javascript:
> 
>>> e || window.event
>>> 
>>> will do fine.
>>> 
>>> I [most probably] all applications,
>>> window.event will only be called if e does convert to false.
>> 
>> 1. Nothing will be *called* here.
> 
> Depending on definition, it will.

You can make up definitions all you want.  That does not change the fact 
that “window.event” is not a call, but only a property access, by the 
definitions in the language specification (and programmer’s common sense).
  
>> 2. That is beside the point; the *assignment* will take place this way
>>    even if not necessary.
> 
> Depending on definition, it will not.

Nonsense.  The statement

  e = e || window.event;

is parsed like

  e = (e || window.event);

which reads in pseudo-VM code:

  1 BOOLEAN $e
  2 NUMBER $1
  3 JNZ $2, 6
  4 ASSIGN e, $window.event
  5 RETURN $window.event
  6 ASSIGN e, $e
  7 RETURN $e

That is, *regardless* of the values of “e” and “window.event”, the 
assignment always takes place.  This is unnecessary and error-prone because 
assigning the value of “e” to “e” does usually not change the value of “e”.  
As compared to

  if (!e) e = window.event;

which reads in pseudo-VM code:

  1 BOOLEAN $e
  2 NUMBER $1
  3 JNZ $2, 5
  4 RETURN
  5 ASSIGN e, $window.event
  6 RETURN $window.event

(See the ECMAScript Language Specification, 5.1 Edition, for the elaborate 
descriptions.)
 
>> 3. If a less simple test is necessary
>> (for example, in order to prevent conversion to boolean),
> 
> Quod non.

Si tacuisses, philosophus mansisses.
 
>> the now simple expression will quickly become
>> unreadable, and hard to maintain.
> 
> That is beside the point here,
> because this is not about boolean.

Yes, partially it is.  If you use the “||” operator, the first operand will 
be converted to boolean to see if shortcut evaluation suffices.
 
The other part is the unnecessary assignment if ”e” already has the proper 
value, and error-prone if it refers to a host object (which it does).

We have been over this ad nauseam.

> e || window.event
> 
> should be so common that your mind will recognize it at once
> for the near future.

And I presume your mind is too small to store the more efficient, more 
flexible, less error-prone

  if (!e) e = window.event;
 
or better

  if (typeof e == "undefined")
  {
    e = window.event;
  }

For it is too small to realize the difference between an attribution line 
and an attribution novel already.  And an e-mail address and something that 
only resembles one.  And …

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23196

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-22 12:56 +0100
Message-ID<5405627.TnZ6cknVVg@PointedEars.de>
In reply to#23195
Thomas 'PointedEars' Lahn wrote:

>   if (!e) e = window.event;
> 
> which reads in pseudo-VM code:
> 
>   1 BOOLEAN $e
>   2 NUMBER $1
>   3 JNZ $2, 5

JMPZ, of course.

>   4 RETURN
>   5 ASSIGN e, $window.event
>   6 RETURN $window.event

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23219

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2014-02-23 14:00 +0000
Message-ID<4ovjg9t78ok6lrpatcunc32poo4dbtlcvb@4ax.com>
In reply to#23195
On Sat, 22 Feb 2014 12:50:13 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Evertjan. wrote:

  <snip>
>For it is too small to realize the difference between an attribution line 
>and an attribution novel already.  

Thomas has gone round the twist. He thinks that a German convention is
mandatory for the rest of the world, even when that convention is
based on faulty logic. 

Evertan is obeying the UK convention, and why not. 

And he doesn't have a 1kB non-standard header line, unlike Thomas.


>And an e-mail address and something that 
>only resembles one.  

exxjxw.hannivoort@ ... works in one of my domains. Has anyone proved
that it isn't valid in the domain Evertan gives?


>And …
     ^

Thomas loves using characters that will be corrupted by a 7-bit
channel. Hey ho.

  John

[toc] | [prev] | [next] | [standalone]


#23255

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-23 23:48 +0100
Message-ID<2926855.hZBUqkaNbn@PointedEars.de>
In reply to#23191
Thomas 'Ingrid' Lahn wrote:

>     getFeature: function (obj, path) {
>       var realPath = path;
>       var start = 0;
> 
>       if (!_isArray(realPath))
>       {
>         realPath = arguments;
>         start = 1;
>       }
> 
>       for (var i = start, len = realPath.length; i < len; i++)
>       {
>         var component = realPath[i];
>         if (_isObject(obj)
>             && typeof obj[component] != "undefined" && obj[component])
>         {
>           obj = obj[component];
>         }
>         else
>         {
>           return void 0;
>         }
>       }
> 
>       return obj;
>     }
> 
> […]
> _______
> ¹ On a side note, I have also noticed that it already supports
>   false-values.  It does not support “undefined” and “null” values yet,
>   but I do not think this is a problem: I for one would consider
>   “undefined” and “null” to be an indication that the feature on that
>   property path is not available.

Ignore that.  It does not support “undefined” and “null”, which is fine with 
me so far, but it also does not support other false-values like “false” and 
“0”.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23257

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-02-23 20:04 -0300
Message-ID<ledupb$q41$1@speranza.aioe.org>
In reply to#23255
On 23-02-2014 19:48, Thomas 'PointedEars' Lahn wrote:
> Thomas 'Ingrid' Lahn wrote:
           ^^^^^^
Interesting nickname.

-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#23256

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-23 23:39 +0100
Message-ID<26292757.AuDnbMLnkA@PointedEars.de>
In reply to#23191
Thomas 'PointedEars' Lahn wrote:

> Andrew Poulos wrote:
>> On 22/02/2014 6:32 AM, Evertjan. wrote:
>>> document.onmousedown = function(e) {
>>>    var e = e || window.event;
>>>    if (e.shiftKey || e.altKey || e.ctrlKey)
>>>      alert ( "shiftKey=" + e.shiftKey + "\naltKey="
>>>      + e.altKey + "\nctrlKey=" + e.ctrlKey );
>>> };
>> 
>> I notice that you put a "var" before the "e". Is that really necessary?
> 
> No.  As “e” is in the formal parameter list, it is treated as as if it was
> a local variable.  Redeclaring a local variable has no effect. 
> Initializing it like that also has not.
> 
> A local variable can come in handy if the original value of the argument
> of the function call is needed later.  The local variable SHOULD have a
> different identifier then (otherwise it would prevent accesses to the
> argument value by parameter name).  Only insofar development tools (like
> Eclipse JSDT) that warn about parameter assignment are useful here,
> because ECMAScript implementations support only call-by-value (references
> to objects are values; properties and variables cannot be referenced in
> terms of their memory locations): Accidental assignments to the parameter
> itself will not change a value in the calling execution contexts.

Apparently nobody has objected to this yet, so I correct myself:

Assignment to a formal parameter changes the value of that parameter as 
copied from the argument of the call *for the local execution context*.  
This was probably obvious as one would implement optional named arguments 
this way.

What may not be obvious: When *not* in strict mode (this is the default), 
the corresponding element of the array-like “arguments” object is changed as 
well.

<http://ecma-international.org/ecma-262/5.1/#sec-C>
<http://ecma-international.org/ecma-262/5.1/#sec-10.6>

This can have unwanted side-effects.  Either initializing a local variable  
with the parameter value or enabling strict mode (where supported) prevents 
that.

It might be prudent then, even if the result may be distracting at times, to 
enable the “Parameter assignment” warning in the JSDT Validator.  (I know 
there are recommendations on the Web not to do that when using certain 
libraries; people might want to reconsider.)

As a current case in point, today I had modified my

    getFeature: function (obj) {
      for (var i = 1, len = arguments.length; i < len; i++)
      {
        var component = arguments[i];
        if (_isObject(obj)
            && typeof obj[component] != "undefined" && obj[component])
        {
          obj = obj[component];
        }
        else
        {
          return void 0;
        }
      }

      return obj;
    }

so that it also accepted a second Array argument instead of the remaining 
argument list (see also <news:2932926.BXQvjz4BgK@PointedEars.de>¹):

    getFeature: function (obj, path) {
      var start = 0;

      if (!_isArray(path))
      {
        path = arguments;
        start = 1;
      }

      for (var i = start, len = path.length; i < len; i++)
      {
        var component = path[i];
        if (_isObject(obj)
            && typeof obj[component] != "undefined" && obj[component])
        {
          obj = obj[component];
        }
        else
        {
          return void 0;
        }
      }

      return obj;
    }

This modification, although syntactically and semantically sound, proved to 
be a show-stopper: When the method was not passed an Array, the second 
element of the “arguments” object (“arguments[1]”) became a reference to the 
“arguments” object itself, causing the unit test to fail.

Since I cannot rely on strict mode support for now, I have modified it as 
follows:

    getFeature: function (obj, path) {
      var realPath = path;
      var start = 0;

      if (!_isArray(realPath))
      {
        realPath = arguments;
        start = 1;
      }

      for (var i = start, len = realPath.length; i < len; i++)
      {
        var component = realPath[i];
        if (_isObject(obj)
            && typeof obj[component] != "undefined" && obj[component])
        {
          obj = obj[component];
        }
        else
        {
          return void 0;
        }
      }

      return obj;
    }

Now it passes the unit test again.

IOW: Declaring a local “e” variable in the code on the top is not necessary 
and *here* it has none of the other side-effects that I mentioned; but it 
does have a noticeable positive effect.

_______
¹ On a side note, I have also noticed that it already supports false-values.
  It does not support “undefined” and “null” values yet, but I do not think
  this is a problem: I for one would consider “undefined” and “null” to be
  an indication that the feature on that property path is not available.
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web