Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38259

Re: Object values (continued)

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Object values (continued)
Date 2020-03-17 22:57 +0100
Organization PointedEars Software (PES)
Message-ID <2017896.irdbgypaU6@PointedEars.de> (permalink)
References <8f3v6ft5nntnuc7qi27q8if1crb40499hp@4ax.com> <1w7ujei4y9bez.k70pvfchqjrm$.dlg@40tude.net> <f0827fd6opgb22lbnbd0rghvhcaqdg0i3k@4ax.com> <WiacG.120366$UC6.67584@fx09.iad>

Show all headers | View raw


Richard Damon wrote:

> On 3/17/20 3:09 PM, John G Harris wrote:
>> On Tue, 17 Mar 2020 12:48:17 +0700, JJ <jj4public@vfemail.net> 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:

<https://eslint.org/docs/rules/>
<https://github.com/airbnb/javascript#type-casting--coercion>

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)¹:

     <https://github.com/PointedEars/JSX/blob/master/string/unicode.js>]

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: <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


Thread

Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-16 14:34 +0000
  Re: Object values  (continued) Jon Ribbens <jon+usenet@unequivocal.eu> - 2020-03-16 14:52 +0000
    Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-16 18:19 +0000
      Re: Object values  (continued) Jon Ribbens <jon+usenet@unequivocal.eu> - 2020-03-16 20:06 +0000
        Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-17 19:01 +0000
          Re: Object values  (continued) Jon Ribbens <jon+usenet@unequivocal.eu> - 2020-03-17 19:55 +0000
            Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-18 17:29 +0000
  Re: Object values  (continued) JJ <jj4public@vfemail.net> - 2020-03-17 12:48 +0700
    Re: Object values  (continued) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-03-17 17:51 +0100
      Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-17 19:03 +0000
        Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-18 07:20 +1100
          Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-18 17:29 +0000
            Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-19 14:21 +1100
              Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-19 11:12 +0000
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-20 14:33 +1100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-20 09:21 +0000
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-21 09:56 +1100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-21 11:18 +0000
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-22 08:45 +1100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-22 10:10 +0000
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-23 07:44 +1100
    Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-17 19:09 +0000
      Re: Object values (continued) Richard Damon <Richard@Damon-Family.org> - 2020-03-17 16:21 -0400
        Re: Object values (continued) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-03-17 22:57 +0100
  Re: Object values  (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-19 05:32 -0700
    Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-19 15:26 +0100
      Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-19 18:16 +0000
    Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-19 18:15 +0000
      Re: Object values  (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-19 11:54 -0700
        Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-20 09:11 +0000
          Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-27 15:35 +0000
            Re: Object values  (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-27 09:59 -0700
              Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-28 14:09 +0000
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-29 14:18 +1100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-29 10:40 +0100
                Re: Object values (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-29 04:41 -0700
    Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-21 14:12 +0000
      Re: Object values  (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-21 19:45 +0100
        Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-22 10:30 +0000
          Re: Object values  (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-22 23:30 +0100
            Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-23 09:44 +0000
              Re: Object values  (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-23 11:19 +0100
                Re: Object values  (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-23 05:00 -0700
                Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-23 18:05 +0000
                Re: Object values  (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-23 11:50 -0700
                Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-23 19:34 +0000
                Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-23 18:14 +0000
                Re: Object values  (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-23 22:21 +0100
                Re: Object values  (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-24 14:09 +0000
                Re: Object values  (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-25 00:01 +0100
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-24 01:18 +0100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-24 14:24 +0000
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-24 15:43 +0100
                Re: Object values (continued) Julio Di Egidio <julio@diegidio.name> - 2020-03-24 11:03 -0700
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-24 18:23 +0000
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-24 22:19 +0100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-25 16:34 +0000
                Re: Object values (continued) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-03-25 23:17 +0100
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-26 01:27 +0100
                Re: Object values (continued) Andrew Poulos <ap_prog@hotmail.com> - 2020-03-26 18:58 +1100
                Re: Object values (continued) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-26 10:43 +0000
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-26 12:22 +0100
                Re: Object values (continued) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-03-26 10:08 -0700
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-26 18:29 +0100
                Re: Object values (continued) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-03-26 14:17 -0700
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-26 23:37 +0100
                Re: Object values (continued) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-03-26 16:40 -0700
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-27 11:38 +0100
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-26 18:11 +0000
                Re: Object values (continued) Silvio <silvio@internet.com> - 2020-03-26 19:41 +0100
                Re: Object values (continued) "Mark-" <nospam@mapson.xyz> - 2020-03-25 00:00 +0000
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-25 15:05 +0000
                Re: Object values (continued) "Mark-" <nospam@mapson.xyz> - 2020-03-25 20:12 +0000
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-26 18:09 +0000
                Re: Object values (continued) John G Harris <niam@jghnorth.org.uk.invalid> - 2020-03-25 19:39 +0000
            Re: Object values  (continued) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-03-23 18:49 -0700

csiph-web