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


Groups > comp.lang.javascript > #34750 > unrolled thread

Re: What's wrong with using a global id?

Started byThomas 'PointedEars' Lahn <PointedEars@web.de>
First post2018-02-26 14:30 +0100
Last post2018-02-26 20:11 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What's wrong with using a global id? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-02-26 14:30 +0100
    Re: What's wrong with using a global id? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-02-26 14:35 +0100
      Re: What's wrong with using a global id? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-03-02 15:05 +0100
    Re: What's wrong with using a global id? John G Harris <niam@jghnorth.org.uk.invalid> - 2018-02-26 20:11 +0000

#34750 — Re: What's wrong with using a global id?

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2018-02-26 14:30 +0100
SubjectRe: What's wrong with using a global id?
Message-ID<1653812.atdPhlSkOF@PointedEars.de>
Stefan Ram wrote:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>(I also showed them that given
>><p id="null">
>>this.null
>>did not refer to the element while
>>document.getElementById( "null" )
>>did.
> 
>   Now, I observe here
> 
> |> document.getElementById( "null" )== this.null
> |< true
> 
>   , but
> 
> |> document.getElementById( "Array" )== this.Array
> |< false
> 
> |> document.getElementById( "Array" ).innerHTML
> |< "Kundenformular"
> 
> |> this.Array.innerHTML
> |< undefined
> 
>   .

(Your punctuation is still atrocious.)

You should have told the host environment in which you tested; I presume it 
is some version of Mozilla Firefox using Mozilla JavaScript (13 %) as 
previously you appeared to be blissfully unaware of the much larger (global) 
market share of Chromium/Google Chrome/Opera and its Google V8 JavaScript 
implementation of ECMAScript (61 %). (Web browsers only; source: 
<https://netmarketshare.com/>.)

In any case, it works as designed.  The ECMAScript global object does not 
have a built-in “null” property, but it does have a built-in “Array” 
property.  The value of the built-in “Array” property is (a reference to) 
the built-in ECMAScript Array constructor.

By having an element with ID “null” inserted and parsed in the HTML 
document, the global object provides access to the object representing that 
element in the document tree (an “element object” implementing the Element 
interface of W3C DOM 4, through an element-type specific interface – here, 
the HTMLParagraphElement interface) via property access to a property named 
“null”.

Note that this wording, although more complicated, is important: it does 
_not_ imply that the global object actually has a property with that name.  
And indeed, using Object.getOwnPropertyNames(this).indexOf("null") in 
Firefox 52 (navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64; 
rv:52.0) Gecko/20100101 Firefox/52.0") one can show that it does not, and 
neither has one of the objects in its prototype chain 
(“Object.getPrototypeOf(this)” aso.)

A built-in property will never be modified *implicitly* by the host 
environment, which is a Good Thing – if it were different, code could be 
*broken* by parsing an element in the document whose ID happens to coincide 
with one of the names of the built-in properties of the global object, such 
as “Array”, because the global object is in the scope chain of those 
execution contexts:

  /* in global context */
  this.Array = {};
  
  try
  {
    /* throws “TypeError: Array is not a constructor” */
    var a = new Array();
  }
  catch (e)
  {
    console.error(e);
  }

  function foo ()
  {
    /* throws “TypeError: Array is not a constructor” */
    var a = new Array();
  }

  try
  {
    foo();
  }
  catch (e)
  {
    console.error(e);
  }

  function fooStrict ()
  {
    "use strict";

    /* throws “TypeError: Array is not a constructor” */
    var a = new Array();
  }

  try
  {
    fooStrict();
  }
  catch (e)
  {
    console.error(e);
  }
 
    [Note that, unless extreme backwards compatibility is
     required, you should write “[]” instead of “new Array()”
     regardless.  See the ECMAScript Support Matrix for details.]

In Mozilla JavaScript in a Mozilla Gecko HTML environment:

                               ,----------------------------.
                               : Object.prototype : Object  :
                               :     (built-in, native)     :
                               :----------------------------:
                               : +__defineGetter__()        :
                               : +__defineSetter__()        :
                               : +__lookupGetter__()        :
                               : +__lookupSetter__()        :
                               : +hasOwnProperty()          :
                               : …                          :
                               :----------------------------:
                               : +__proto__ : Null          :
                               : …                          :
                               : [_not_ null]               :
                               `----------------------------'
                                              ^
                                              : (__proto__)
                                              :
                                 ,-------------------------.
                                 : : EventTargetPrototype  :
                                 :         (host)          :
                                 :-------------------------:
                                 : +addEventListener()     :
                                 : +dispatchEvent()        :
                                 : +removeEventListener()  :
                                 :-------------------------:
                                 : +constructor : Function :
                                 : +__proto__ : Object     :
                                 : [_not_ null]            :
                                 `-------------------------'
                                              ^
                                              : (__proto__)
                                              :
                                 ,-------------------------.
                                 :    : WindowProperties   :
                                 :         (host)          :
                                 :-------------------------:
                                 : +__proto__ : Object     :
                                 : [_not_ null]            :
                                 `-------------------------'
                                              ^
                                              : (__proto__)
                                              :
                                 ,-------------------------.
                                 :    : WindowPrototype    :
                                 :         (host)          :
                                 :-------------------------:
                                 : +constructor : Function :
                                 :-------------------------:
                                 : +__proto__ : Object     :
                                 : [_not_ null]            :
                                 `-------------------------'
                                              ^
                                              : (__proto__)
                                              :
                      ,------------------------------------------------.
                      :             global object : Window             :
                      :           (built-in, native; + host)           :
                      :------------------- built-in -------------------:
                      : +Array() : Array                               :
                      :  …                                             :
  this -------------> : +RegExp() : RegExp                             :
  (global or          :------------------------------------------------:
  or non-strict       : +NaN : number                                  :
  execution context)  : …                                              :
                      :----------------- host-defined -----------------:
                      : +__proto__ : Object                            :
                      : +null : HTMLParagraphElement (by proxy getter) :                      
                      `------------------------------------------------'

In Google V8 JavaScript in Chromium 57 (navigator.userAgent === 
""Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/57.0.2987.98 Safari/537.36") it is slightly different:

  this : Window → : WindowProperties → : EventTarget → : Object

Find out for yourself with

  var
    proto = this,
    prototypeChain = [proto];

  while ((proto = Object.getPrototypeOf(proto)))
  {
    prototypeChain.push(proto);
  }

  console.log(prototypeChain);

<http://www.ecma-international.org/ecma-262/8.0/#sec-lexical-environments>

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

[toc] | [next] | [standalone]


#34751

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2018-02-26 14:35 +0100
Message-ID<2186336.jE0xQCEvom@PointedEars.de>
In reply to#34750
Thomas 'PointedEars' Lahn wrote:

> By having an element with ID “null” inserted and parsed in the HTML
> document, the global object provides access to the object representing
> that element in the document tree (an “element object” implementing the
> Element interface of W3C DOM 4, through an element-type specific interface
> – here, the HTMLParagraphElement interface) via property access to a
> property named “null”.
> 
> Note that this wording, although more complicated, is important: it does
> _not_ imply that the global object actually has a property with that name.
> And indeed, using Object.getOwnPropertyNames(this).indexOf("null") in
> Firefox 52 (navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64;
> rv:52.0) Gecko/20100101 Firefox/52.0") one can show that it does not, and
> neither has one of the objects in its prototype chain
> (“Object.getPrototypeOf(this)” aso.)

One can now (ES 2015+) construct such an object using Proxy:

  var proxy = new Proxy({
    Array: Array
  }, {
    get: function (target, name) {
      return (name in target)
        ? target[name]
        : document.getElementById(name);
    }
  });

  /* “[object Array]” (in all cases) */
  console.log(proxy.Array);

  /* “[object HTMLParagraphElement]” (in this case) */
  console.log(proxy.null);

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

[toc] | [prev] | [next] | [standalone]


#34755

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2018-03-02 15:05 +0100
Message-ID<1776626.9Ho59r5IoZ@PointedEars.de>
In reply to#34751
Thomas 'Ingrid' Lahn wrote:

>   var proxy = new Proxy({
>     Array: Array
>   }, {
>     get: function (target, name) {
>       return (name in target)
>         ? target[name]
>         : document.getElementById(name);
>     }
>   });
> 
>   /* “[object Array]” (in all cases) */
>   console.log(proxy.Array);

Actually, more like “function Array() { [native code] }”, because
the referred object is a Function instance, _not_ an Array instance.

> […]

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

[toc] | [prev] | [next] | [standalone]


#34753

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2018-02-26 20:11 +0000
Message-ID<g8q89ddq0c62anbooap578d0h1471c8q6m@4ax.com>
In reply to#34750
On Mon, 26 Feb 2018 14:30:38 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Stefan Ram wrote:

  <snip>
>(Your punctuation is still atrocious.)

Thomas doesn't understand Netiquette.


  <snip>
>The value of the built-in “Array” property is (a reference to) 
  <snip>

No it is not. Thomas should read the Standard.

  John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web