Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23562 > unrolled thread
| Started by | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| First post | 2014-03-12 03:07 +0000 |
| Last post | 2014-03-12 11:06 +0100 |
| Articles | 2 — 2 participants |
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? 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
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-03-12 03:07 +0000 |
| Subject | Re: first-class functions? |
| Message-ID | <0.4b944fce00eef4685de9.20140312030725GMT.874n34ktci.fsf@bsb.me.uk> |
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). A simpler example: var wl = document.writeln; wl(4); // This is an error -- to what does 'this' refer in wl? You can get your first-class function back by binding the member function to the object: var wl = document.writeln.bind(document); wl(4); Something similar will fix your conditional expression, too. -- Ben.
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-12 11:06 +0100 |
| Message-ID | <15678947.80IZ92ngzi@PointedEars.de> |
| In reply to | #23562 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web