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


Groups > comp.lang.javascript > #32895

Re: hasOwnProperty and property className

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: hasOwnProperty and property className
Date 2017-04-01 13:53 +0200
Organization PointedEars Software (PES)
Message-ID <1640137.tdWV9SEqCh@PointedEars.de> (permalink)
References <obnrdm$158t$1@csiph.com>

Show all headers | View raw


Cezary Tomczyk wrote:

> Why document.body.hasOwnProperty('className') returns false?
> 
> Property className does exists for object document.body object.

Note the “Own” in the method name; see the ECMAScript 2016 Language
Specification, § 19.1.3.2, for details:

<http://www.ecma-international.org/ecma-262/7.0/#sec-object.prototype.hasownproperty>

It depends on the DOM implementation of course, but in the Blink DOM
(here: in Chromium 57.0.2987.98 built for Debian GNU/Linux 8.7, running
on Debian stretch/sid and Devuan GNU+Linux [64-bit]), the property
is *inherited* from an object implementing the Element interface of
W3C DOM4:

| > navigator.userAgent
| <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36"
| > document.body.hasOwnProperty('className')
| <- false
| > Object.getPrototypeOf(document.body).hasOwnProperty('className')
| <- false
| > Object.getPrototypeOf(Object.getPrototypeOf(document.body)).hasOwnProperty('className')
| <- false
| > Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(document.body))).hasOwnProperty('className')
| <- true
| > Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(document.body)))
| <- ▾ Element {…} [i]
|      […]
|      className: (...)

<https://www.w3.org/TR/2015/REC-dom-20151119/#interface-element>

Note that the “className” interface attribute is (wisely) implemented as
an accessor property there, as you can see e.g. when you expand its value by
clicking the ellipsis:

|      className: [Exception: TypeError: Illegal invocation at Element.remoteFunction (<anonymous>:2:14)]

More information is provided by the Object.getOwnPropertyDescriptor()
method introduced with ECMAScript Edition 5:

| > Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(document.body))), "className")
| <- ▾ Object {enumerable: true, configurable: true, get: function, set: function} [i]
|        configurable: true
|        enumerable: true
|      ▸ get: function ()
|      ▸ set: function ()
|      ▸ __proto__: Object

<http://PointedEars.de/es-matrix/?filter=%5EObject%5C.>

To determine whether an object has a property, i.e. has an own property
or inherits one of that name, you can use the “in” operator, introduced
with ECMAScript Edition 3, and available since

  - Mozilla JavaScript 1.5 (Firefox 1.0.8 for Windows),
  - Microsoft JScript 5.5 (Microsoft Internet Explorer 5.50.4807.2300),
  - Opera ECMAScript 7.02 (Opera 7.02.2668 for Windows XP 32-bit),
  - at least KDE JavaScript 4.6.5 (Konqueror 4.6.5 for Linux),
  - at least Apple JavaScriptCore 531.22.7 (Apple Safari 4.0.5 for Windows),
  - at least Google V8 JavaScript 3.19.17 (Chrome 29.0.1535.2 for Mac OS X):

| > "className" in document.body
| <- true

<http://PointedEars.de/es-matrix/?filter=%5Cbin%5Cb>

But note that “document.body” refers to a host object, so all bets are off.
However, for host object’s properties the “typeof” operation so far has been
the most reliable and backwards-compatible test:

| > typeof document.body.className
| <- "string"
| > typeof document.body.className != "undefined"
| <- true

Note that this would not work for properties whose getter returns
the “undefined” value.  But I do not know a host object’s property
that has such a getter.

-- 
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 | Find similar | Unroll thread


Thread

hasOwnProperty and property className Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-04-01 11:20 +0200
  Re: hasOwnProperty and property className "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-04-01 12:17 +0200
    Re: hasOwnProperty and property className Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-04-01 15:59 +0200
      Re: hasOwnProperty and property className "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-04-01 16:40 +0200
      Re: hasOwnProperty and property className Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-02 00:39 +0200
  Re: hasOwnProperty and property className Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-01 13:53 +0200

csiph-web