Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23560 > unrolled thread
| Started by | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| First post | 2014-03-12 03:52 +0100 |
| Last post | 2014-03-12 03:52 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.javascript
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: first-class functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 03:52 +0100
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 03:52 +0100 |
| Subject | Re: first-class functions? |
| Message-ID | <1781123.YuqQMODZ1I@PointedEars.de> |
Stefan Ram wrote:
> I start with the following code in a web page:
There are no web pages.
> document.writeln( 4 );
You have been told before not to use document.writeln(). Also, that code
style is at least uncommon. You would be well-advised to stick to common
code styles.
> . It prints »4«. Ok. Next, I enclose the function expression
> in parentheses:
>
> ( document.writeln )( 4 );
A function expression is something entirely different:
function … (…) { … }
You have enclosed a property access instead.
> Still prints »4«. Fine. But now, I try to get closer to what
> I actually wanted to write:
>
> ( true ? document.writeln : document.writeln )( 4 );
That is a pointless statement.
> And this gives an error message here!
“Gives an error message” is not a problem description. But I can guess what
you get: “function called on incompatible object” or something like that.
> Where are my first-class "functions as values", when I need them?
You attempted to call the method as method of another object, here the
global object, which is not allowed. The same is true for many other host
object’s methods (short: host methods), and a common beginner’s
problem/error.
In general, if you want to use host methods in expressions, you need to wrap
them in user-defined functions instead, so that the “this” value is
preserved. Quick hack (script kids, don't try that at home):
var myWrite = (function () {
if (jsx.object.isHostMethod(document, "body", "appendChild"))
{
return function (s) {
document.body.appendChild(document.createTextNode(s));
};
}
return function (s) {
/* CAVEAT: Not equivalent to the above */
document.write(s);
};
}());
myWrite(42);
I think the FAQ has an example written by someone else a long time ago.
(It probably needs revision, but should give the general idea.)
I strongly suggest you read the latest Edition of the ECMAScript Language
Specification before further questions of that kind.
--
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 top | Article view | comp.lang.javascript
csiph-web