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


Groups > comp.lang.javascript > #23565

Re: first-class functions?

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <15678947.80IZ92ngzi@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Wed, 12 Mar 2014 11:06:24 +0100
User-Agent KNode/4.11.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: first-class functions?
Newsgroups comp.lang.javascript
References <first-class-functions-20140312030629@ram.dialup.fu-berlin.de> <0.4b944fce00eef4685de9.20140312030725GMT.874n34ktci.fsf@bsb.me.uk>
MIME-Version 1.0
Lines 68
NNTP-Posting-Date 12 Mar 2014 11:06:28 CET
NNTP-Posting-Host 62e2429b.newsspool1.arcor-online.net
X-Trace DXC=SDT4kYdcOL6i6K;>iZ]763ic==]BZ:af>4Fo<]lROoR1nkgeX?EC@@01CEWAg0_HN5DZm8W4\YJN<YUVHKH;GI^7<_e=dZX<SR7>b_T4YOS@=;
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:23565

Show key headers only | View raw


Ben Bacarisse wrote:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>   I start with the following code in a web page:
>>
>> document.writeln( 4 );
>>
>>   . It prints »4«. Ok. Next, I enclose the function expression
>>   in parentheses:
>>
>> ( document.writeln )( 4 );
>>
>>   Still prints »4«. Fine. But now, I try to get closer to what
>>   I actually wanted to write:
>>
>> ( true ? document.writeln : document.writeln )( 4 );
>>
>>   And this gives an error message here! Where are my
>>   first-class "functions as values", when I need them?
> 
> document.writeln is a function but, being a method, it will refer to
> properties in the document object (it does so using "this" of course).

“Of course”?  No.  It is a method of a host object.  All bets are off.
 
> A simpler example:
> 
>   var wl = document.writeln;

This could already be an error, referring to a host objects method not in a 
CallExpression.  BTST.  The host object referred to by “document” does not 
have to implement [[Get]] in the way of native objects. [1]

>   wl(4);  // This is an error -- to what does 'this' refer in wl?

We cannot know that, if there is such a value, and if it would be relevant.  
(Assuming native object-like behavior, “this” would either refer to the 
global object, or in strict mode would be the “undefined” value.)

> You can get your first-class function back by binding the member
> function to the object:
> 
>   var wl = document.writeln.bind(document);
>   wl(4);

Unreliable at best: a host object’s method does not have to refer to a 
Function instance, it does not even have to have Function.prototype in its 
prototype chain which provides .bind() [2].  It just needs to be callable, 
i.e. implement [[Call]] (not: .call()) in a useful way. [3, 4]

Certainly incompatible as Function.prototype.bind() was not introduced 
before ECMAScript Edition 5. [5]
 
> Something similar will fix your conditional expression, too.

You simply cannot know that.

_________
[1] <http://ecma-international.org/ecma-262/5.1/#sec-8.6.2>
[2] <http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.5>
[3] <http://ecma-international.org/ecma-262/5.1/#sec-11.2.3>
[4] <http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.4>
[5] <http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf>
-- 
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 | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: first-class functions? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-12 03:07 +0000
  Re: first-class functions? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 11:06 +0100

csiph-web