Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: hasOwnProperty and property className Date: Sat, 01 Apr 2017 13:53:56 +0200 Organization: PointedEars Software (PES) Lines: 87 Message-ID: <1640137.tdWV9SEqCh@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1491047637 4593 eJwNwgkRADEIBDBL5V2QA4X6l3A3iYmTX6ibq72fzM3Ts6iNPG97qFMhha7CvkVMCLOAWZU+Nq4RZw== (1 Apr 2017 11:53:57 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 1 Apr 2017 11:53:57 +0000 (UTC) User-Agent: KNode/4.14.2 X-NNTP-Posting-Host: eJwFwYEBgDAIA7CXhNEq57AC/59ggkOj3iAYWOzTHjvllyWIupVp1ZszbvYZpNCoQdOerB87RxJF Cancel-Lock: sha1:rjvMU4Z0vS371jJYiIgBvTRJNyU= X-User-ID: eJwNyckBwDAIA7CVSMCGjsNR9h+h1VdQHrYbQcNi74a09HtAmb8Sd14XZ9VEbixHz41gg2h93Kq4ltqVun4/T6IVlA== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. 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: 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: (...) 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 (: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 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 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.