Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20300
| Message-ID | <2664592.4ckUSR1F5s@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-06-15 11:41 +0200 |
| Subject | Re: Alignment of three tables |
| Newsgroups | comp.lang.javascript |
| References | (4 earlier) <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> |
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