Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30301
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Ctrl + onload |
| Date | 2016-04-19 19:46 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <7166847.g7ly2KG8ZP@PointedEars.de> (permalink) |
| References | <nepuha$19br$1@gioia.aioe.org> <nf447d$qv0$1@gioia.aioe.org> |
emf wrote:
> document.getElementById("start").onclick = function (e) {
> var i;
> requestFullScreen();
> i = (e.ctrlKey)
> ? 1
> : 0;
The variable “i” is unnecessary:
> init(i);
init(+e.ctrlKey);
works because the value of “e.ctrlKey” is of type Boolean, and the unary “+”
operator converts “true” to the Number value 1, and “false” to 0.
However, the type conversion, explicitly (here) or implicitly (with e.g. the
“if” statement) is not strictly necessary; you could test for the Boolean
value in init() instead.
> };
> };
>
> A couple of comments. Instead of
>
> textFile(0);
> textFile(1);
>
> initially I had tried to use an for loop, which worked but JSlint found
> the for unexpected, so I deduced that I shouldn't use a for loop in this
> context.
You should learn to understand *why* linters give you errors/warnings, and
you should not blindly follow their advice. JSLint is especially notorious
in giving bad/misleading advice, thanks to Douglas Crockford’s presumptions
about the programming languages and the DOM that he does not understand,
too. You should avoid it in favor of more sophisticated tools, like JSHint.
Still, a “for” loop for two iterations resulting in two simple function
calls appears to be overkill, indeed. One *can* *over*-DRY things.
> Also I still have difficulty understanding why the e is now necessary in
> the line:
>
> onclick = function (e) { ...
Learn how to *ask* *questions* the *smart* way; see the FAQ. The above is
_not_ a question at all; it is a statement to which my first idea was
“That’s too bad.”. So such a statement is not going to help you solve your
problems. I am making an exception now and answer your non-question:
There is “e.ctrlKey” in the function code. “e” cannot be resolved if it is
not in the scope chain, so the property access fails then. The error
console tells you that: a ReferenceError exception should be thrown. See
the FAQ.
More, the first argument to an event listener, per the W3C DOM Level 2+
Events and Web API Specifications, is a reference to an event object, an
object implementing the Event interface. It is that object that provides
information as to what were the conditions when the event was fired (i.e.
created and propagated to its target), including the status of the Ctrl key.
Finally and *again*, your *real* name belongs in the From header field of
your postings. You should not be surprised that, in addition to your
posting non-questions, there are few responses if you do not show people
this simple courtesy. Few people want to communicate with a three-letter
entity. Internet is the thing with cables; Usenet is the thing with
*people*.
Again, I am making an exception here in the hope that a more direct approach
works better with you, since so far you did not appear to understand the
hints that you have been given in that regard.
--
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
Ctrl + onload emf <emfril@gmail.com> - 2016-04-15 01:34 -0400
Re: Ctrl + onload emf <emfril@gmail.com> - 2016-04-18 22:13 -0400
Re: Ctrl + onload Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-19 19:46 +0200
Re: Ctrl + onload Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-26 02:55 +0200
csiph-web