Path: csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Object values (continued) Date: Tue, 17 Mar 2020 22:57:17 +0100 Organization: PointedEars Software (PES) Lines: 108 Message-ID: <2017896.irdbgypaU6@PointedEars.de> References: <8f3v6ft5nntnuc7qi27q8if1crb40499hp@4ax.com> <1w7ujei4y9bez.k70pvfchqjrm$.dlg@40tude.net> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.albasani.net YXOul34lbvdt2bRop1Sk7GQJQi9PXt2nCbGT0wPZNYnEInzliNKzt07WsjMPnb0VfLBzClkFUZPn58zD24EsU8NY7a27skA0BUyJpdX7SSDdRJGDD6bazPNVGYp14fkI NNTP-Posting-Date: Tue, 17 Mar 2020 21:57:19 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="KHioqO2AomCwrxJpTYt31jaQvY8Da5rRb1h7xE3Hu3b0dnM3Pbt/n90n5UxwWz5NzRg3SZXxjGTHMcSX9B99GEzqv5vVp0FjO9FGwJZHrbsVyNM/4ygdOXxu4vCCqxTB"; mail-complaints-to="abuse@albasani.net" User-Agent: KNode/4.14.10 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== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On 3/17/20 3:09 PM, John G Harris wrote: >> On Tue, 17 Mar 2020 12:48:17 +0700, JJ wrote: >>> On Mon, 16 Mar 2020 14:34:50 +0000, John G Harris wrote: >>>> I really cannot understand why some people are fixated on references. >>> In JavaScript, everything are objects. >> There's a problem when *everything* is an object. You look at an >> object and see it holds a value, which is an object that holds a >> value, which is an object that holds a value, which is an object that >> holds a value, ... ... >> >> After descending through an infinite sequence of objects you still >> haven't reached "Hello World!". > > Expect that when you get to the string object that holds the value of > "Hello World!", that object IS the value you want to get to, so you stop. Actually, there are *primitive* String values, which are NOT objects; and there are String *instances*, which *are* objects/object values. In both cases, accessing the value in comparison or concatenation or property access is the same as operating on the primitive String value. Notably, the “typeof” operation is NOT one of those operations that do not distinguish between primitive String value and String instance: /* "string" */ typeof '42' /* "object" */ typeof new String('42') In general, it is a good idea to avoid creating String instances (and instances of other primitive types) explicitly, which is why the rule “no-new-wrappers“ is documented as a “Best Practice” rule in ESLint, and recommended as “to be enabled” by the Airbnb JavaScript Style Guide, currently per rule 22.2: Also, it is possible to "subclass" (for lack of a better word) the String type such that the "subclass" provides additional behavior and methods that operate on the primitive String value; for example: function MyString (s) { /* As a rule of thumb, always call the constructor of the parent */ String.call(this, s); } MyString.prototype = Object.create(String.prototype, { length: { get: function () { return this.getLength(); } }, getLength: { value: function () { return 42; } } }); [Notably, class MyString extends String { // … } only works partially: the “length” getter *cannot* be overwritten with a class declaration or Object.defineProperty(); the latter even throws an exception (in Google V8 JavaScript 7.3.492.22 in “Chromium 73.0.3683.75 (Developer Build) built on Debian 9.8, running on Debian 9 (64-bit)”).] [One such example of "subclassing" String is jsx.string.unicode.WideString which can be used as drop-in replacement for String in order to handle properly String values containing characters beyond the Basic Multilingual Plane (BMP)¹: ] It is therefore a good idea to use s instanceof String or (if that is not available or not feasible) typeof s.toString() == 'string' instead of just typeof s == 'string' to find out whether a value can be used as if it were a primitive String value, should that be required (often one can rely on implicit type conversion). ______ ¹ Notably, while ECMAScript Edition 7 introduced some support for those characters with the String.prototype.codePointAt() method, as of Edition 10 the “length” property will and implementations of it do *still* return the value 2 for a String value containing only *one* character beyond the BMP. JSX:string/unicode.js fixes this with [new] jsx.string.unicode.WideString(…).length which calls jsx.string.unicode.WideString.prototype.getLength(). -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.