Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34750
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: What's wrong with using a global id? |
| Date | 2018-02-26 14:30 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1653812.atdPhlSkOF@PointedEars.de> (permalink) |
| References | <global-ids-20180225051010@ram.dialup.fu-berlin.de> <id-20180225194506@ram.dialup.fu-berlin.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.
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread
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
csiph-web