Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23256
| Message-ID | <26292757.AuDnbMLnkA@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2014-02-23 23:39 +0100 |
| Subject | Re: How to detect key pressed plus mouse click, e.g., ctrl + left click |
| Newsgroups | comp.lang.javascript |
| References | (1 earlier) <XnsA2DBCC37E59CCeejj99@194.109.133.133> <tn9fg95v13j6c51bjs89qidr75s5kttk3g@4ax.com> <XnsA2DBD0E11E61Eeejj99@194.109.133.133> <LaidnWADIL12nJXOnZ2dnUVZ_i2dnZ2d@westnet.com.au> <3506285.IpskKQNfjB@PointedEars.de> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web