Path: csiph.com!weretis.net!feeder4.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.7.225.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: What's wrong with using a global id? Date: Mon, 26 Feb 2018 14:30:38 +0100 Organization: PointedEars Software (PES) Lines: 210 Message-ID: <1653812.atdPhlSkOF@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="7.225.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.225.7"; logging-data="2301"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. ram@zedat.fu-berlin.de (Stefan Ram) writes: >>(I also showed them that given >>

>>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: .) 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); -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.