Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20300
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.datemas.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <2664592.4ckUSR1F5s@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Sat, 15 Jun 2013 11:41:29 +0200 |
| User-Agent | KNode/4.10.2 |
| 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: Alignment of three tables |
| Newsgroups | comp.lang.javascript |
| References | <b1jivmF20mgU1@mid.individual.net> <kp2n16$ror$1@dont-email.me> <9415734.fqrm2ecmqt@PointedEars.de> <b1m8g6Fjnc5U1@mid.individual.net> <b1mfv8Flc2aU1@mid.individual.net> <kp60kh$nrp$1@dont-email.me> <2068082.pen2y8QiGc@PointedEars.de> <kpf6dg$gpb$1@news.albasani.net> <m21u84tuoo.fsf@nntp.bednarz.nl> |
| MIME-Version | 1.0 |
| Lines | 133 |
| NNTP-Posting-Date | 15 Jun 2013 11:41:32 CEST |
| NNTP-Posting-Host | 00362337.newsspool2.arcor-online.net |
| X-Trace | DXC=^mMk0G]>g8?kUFX=Y?aLP;A9EHlD;3Yc24Fo<]lROoR18kF<OcfhCO;4LhWYJTITM<DZm8W4\YJN<b@mF9jNikd57a5\<`>]gf>RdK5H45i3F> |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:20300 |
Show key headers only | View raw
Eric Bednarz wrote:
> Stefan Weiss <krewecherl@gmail.com> writes:
>> On 2013-06-11 05:09, Thomas 'PointedEars' Lahn wrote:
>>> <http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1>
>>
>> There's one thing I don't understand about that algorithm:
>>
>> {} + ""
>
> That's a block statement followed by an unary + operator with the empty
> string as its operand.
Very good. I had not thought of that. To prove that ES 5.1, “11.6.1 The
Addition operator” is properly implemented as concatenation operator:
/* "[object Object]", as Stefan expected */
var x = {} + "";
or
/* "[object Object]" */
({}) + ""
or
/* "[object Object]" */
new Object() + ""
The curious – maybe not obvious – thing here is, though, that it is _not_
the "" that causes the "[object Object]" result, but the conversion of the
Object instance to a primitive value.
The ToPrimitive() algorithm (§9.1) in step 5 –
| 5. Let lprim be ToPrimitive(lval).
– calls the [[DefaultValue]] internal method, passing the PreferredType on
as argument for the hint. As there is no PreferredType, there is no hint.
Object instances are native objects. Their [[DefaultValue]] method is
specified in ECMAScript (§8.12.8). Calling it without hint works as if it
was called with hint Number. Thus, it attempts to converts its object
argument to a primitive value by attempting to call its valueOf() method
first. Object instances inherit from Object.prototype a valueOf() method
through the prototype chain; it returns a reference to the same object.
That is *not* a primitive value.
If the valueOf() method of the object cannot be called or does not return a
primitive value, [[DefaultValue]] attempts to call the object's toString()
method. Object instances inherit from Object.prototype a toString() method
through the prototype chain which always returns a primitive string value
that is the equivalent of "[object " + [[Class]] + "]" (we can use it to
tell the [[Class]] of an object, for example for Array.isArray() emulation,
as implemented by jsx.object.getClass()). Because that return value
satisfies the condition in §8.12.8, hint Number, step 4b, that is the return
value of [[DefaultValue]] and of ToPrimitive().
That in turn satisfies the condition in §11.6.1, step 7, where it says:
| 7. If Type(lprim) is String or Type(rprim) is String, then […]
It follows from logical shortcut evaluation that it does not matter that
Type("") is String, because Type(({}).toString()) already is String. As a
result, we observe in conforming implementations:
/* "[object Object]23" */
({}) + 23
And because of
| 8. Return the result of applying the addition operation to
| ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.
/* 42 */
({valueOf: function () { return 19; }}) + 23
[A possible application of the latter is:
function Complex (re, im)
{
/* or throw exceptions */
this.re = (re ? +re : 0)
this.im = (im ? +im : 0);
}
/**
* @overrides Object.prototype.valueOf()
*/
Complex.prototype.valueOf = function () {
if (!this.im)
{
return this.re;
}
return this;
};
/* 42 */
new Complex(19) + 23
/* 42 */
new Complex(19) + new Complex(23)
Unfortunately, without the possibility of operator overloading the following
does not work:
/* _not_ new Complex(42, 42) */
new Complex(19, 42) + new Complex(23)
But you can work around it:
Complex.prototype.add = function (o) {
/* or “Complex” if you do not care for polymorphism */
var C = this.constructor;
if (o instanceof C)
{
return new C(this.re + o.re, this.im + o.im);
}
return new C(this.re + (+o), this.im);
};
/* new Complex(42, 42) */
(new Complex(19, 42)).add(new Complex(23));
]
--
PointedEars
Twitter: @PointedEars2
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
Alignment of three tables "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-09 08:49 -0600
Re: Alignment of three tables Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-09 08:18 -0700
Re: Alignment of three tables Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-09 20:02 +0000
Re: Alignment of three tables "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-09 15:01 -0600
Re: Alignment of three tables Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-09 22:50 +0000
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-10 00:46 +0200
Re: Alignment of three tables John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-10 14:41 +0100
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-10 17:35 +0200
Re: Alignment of three tables "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-10 09:08 -0600
Re: Alignment of three tables "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-10 11:16 -0600
Re: Alignment of three tables Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-11 02:05 +0000
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-11 05:09 +0200
Re: Alignment of three tables "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-06-11 10:59 +0200
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-11 16:51 +0200
Re: Alignment of three tables Stefan Weiss <krewecherl@gmail.com> - 2013-06-14 15:38 +0200
Re: Alignment of three tables Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-06-14 20:26 +0200
Re: Alignment of three tables Stefan Weiss <krewecherl@gmail.com> - 2013-06-14 21:22 +0200
Re: Alignment of three tables Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-06-15 00:36 +0200
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-15 12:04 +0200
Re: Alignment of three tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-15 11:41 +0200
Re: Alignment of three tables Stefan Weiss <krewecherl@gmail.com> - 2013-06-15 13:53 +0200
Re: Alignment of three tables Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-17 04:28 -0700
Re: Alignment of three tables "Andy" <andy@NOSPAMmanyplay.com> - 2013-06-10 09:19 +0100
Re: Alignment of three tables Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-12 18:56 +0100
csiph-web