Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33290
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Overriding function and adding a bit |
| Date | 2017-05-12 03:13 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <4301878.31r3eYUQgx@PointedEars.de> (permalink) |
| References | (1 earlier) <oevbsf$61c$1@dont-email.me> <5178377.MhkbZ0Pkbq@PointedEars.de> <of05sh$85g$1@dont-email.me> <20479369.6Emhk5qWAg@PointedEars.de> <3160292.R56niFO833@PointedEars.de> |
Thomas 'PointedEars' Lahn wrote:
> ,-------------------------. :
> | global : object[Object] | :
This should be
> | : object[global] |
at least for V8 in Node.js.
However, in Chromium the global object appears to have "Window" as the value
of its @@toStringTag internal property.
Since ECMAScript 2015, the concept of the @@toStringTag property of an
object replaces the concept of the internal [[Class]] property for object
types that are not explicitly recognized by the changed
Object.prototype.toString() algorithm.
For objects referred by “obj” that have properties and those whose type is
explictly recognized by the algorithm, obj[Symbol.toStringTag] is the
“undefined” value. So obj[Symbol.toStringTag] could provide a more
efficient to retrieve the equivalent of the former internal [[Class]]
property than parsing the return value of ({}).toString.call(obj), provided
that the @@toStringTag property value has not been modified.
However, the Specification explicitly mentions the possibility that programs
can modify the @@toStringTag property value which makes “legacy type tests”
unreliable.
Example:
var o = {
label: "crook",
exec: function () {
/* there’s no there there */
return null;
}
};
/* I’m not a crook^W^W^W a RegExp, _believe me_[tm]! */
o[Symbol.toStringTag] = "RegExp";
/* [object RegExp] */
({}).toString.call(o);
Such lies can be thwarted:
function dontLieToMe (obj)
{
var desc = Object.getOwnPropertyDescriptor(o, Symbol.toStringTag);
return Object.defineProperty(o, Symbol.toStringTag, {
/* “writable: false, configurable: false” is implicit */
value: desc ? desc.value : void 0
});
}
var o = {
label: "crook",
exec: function () {
/* there’s no there there */
return null;
}
};
dontLieToMe(o);
/* I’m not a crook^W^W^W a RegExp, _believe me_[tm]! */
o[Symbol.toStringTag] = "RegExp";
/* [object Object] */
({}).toString.call(o);
/* “Uncaught TypeError: Cannot redefine property:
Symbol(Symbol.toStringTag)” :-b */
Object.defineProperty(o, Symbol.toStringTag, {
value: desc ? desc.value : void 0,
writable: true
});
But in the end, duck typing may always be the best approach ;-)
<http://www.ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring>
<http://www.ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
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
Overriding function and adding a bit Andrew Poulos <ap_prog@hotmail.com> - 2017-05-10 11:59 +1000
Re: Overriding function and adding a bit Doc O'Leary <droleary@2017usenet1.subsume.com> - 2017-05-10 15:33 +0000
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-10 19:14 +0200
Re: Overriding function and adding a bit Doc O'Leary <droleary@2017usenet1.subsume.com> - 2017-05-10 22:57 +0000
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-11 01:49 +0200
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-11 17:03 +0200
Re: Overriding function and adding a bit Andrew Poulos <ap_prog@hotmail.com> - 2017-05-12 08:32 +1000
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-12 22:12 +0200
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-12 03:13 +0200
Re: Overriding function and adding a bit John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-11 16:33 +0100
Re: Overriding function and adding a bit Tim Streater <timstreater@greenbee.net> - 2017-05-11 18:54 +0100
Re: Overriding function and adding a bit Doc O'Leary <droleary@2017usenet1.subsume.com> - 2017-05-11 22:02 +0000
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-12 01:53 +0200
Re: Overriding function and adding a bit John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-12 10:05 +0100
Re: Overriding function and adding a bit Doc O'Leary <droleary@2017usenet1.subsume.com> - 2017-05-12 21:43 +0000
Re: Overriding function and adding a bit Tim Streater <timstreater@greenbee.net> - 2017-05-12 23:03 +0100
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-12 17:28 -0700
Re: Overriding function and adding a bit John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-13 14:39 +0100
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-13 07:17 -0700
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 16:25 +0100
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-13 08:30 -0700
Re: Overriding function and adding a bit Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-13 17:56 +0200
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-13 09:04 -0700
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 20:55 +0100
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-14 00:00 +0100
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-14 03:07 +0100
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-13 18:39 -0700
Re: Overriding function and adding a bit John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-14 11:23 +0100
Re: Overriding function and adding a bit John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-14 11:36 +0100
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-14 12:34 +0100
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-14 14:11 +0200
Re: Overriding function and adding a bit Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-14 15:37 +0100
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-14 17:16 +0200
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-14 15:40 +0200
Re: Overriding function and adding a bit Gene Wirchenko <genew@telus.net> - 2017-05-11 15:39 -0700
Re: Overriding function and adding a bit Tim Streater <timstreater@greenbee.net> - 2017-05-11 10:39 +0100
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-11 10:05 -0700
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-10 19:13 +0200
Re: Overriding function and adding a bit Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-10 19:22 +0200
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-11 10:08 -0700
Re: Overriding function and adding a bit Julio Di Egidio <julio@diegidio.name> - 2017-05-11 09:56 -0700
csiph-web