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


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

hasOwnProperty and property className

Started byCezary Tomczyk <cezary.tomczyk@gmail.com>
First post2017-04-01 11:20 +0200
Last post2017-04-01 13:53 +0200
Articles 6 — 3 participants

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


Contents

  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

#32893 — hasOwnProperty and property className

FromCezary Tomczyk <cezary.tomczyk@gmail.com>
Date2017-04-01 11:20 +0200
SubjecthasOwnProperty and property className
Message-ID<obnrdm$158t$1@csiph.com>
Just out of curiosity.

Why document.body.hasOwnProperty('className') returns false?

Property className does exists for object document.body object.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/
https://www.aslint.org/ - Accessibility linting tool

[toc] | [next] | [standalone]


#32894

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-04-01 12:17 +0200
Message-ID<XnsA74A7CF5916ADeejj99@194.109.6.166>
In reply to#32893
Cezary Tomczyk <cezary.tomczyk@gmail.com> wrote on 01 Apr 2017 in 
comp.lang.javascript:

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

hasOwnProperty()
is about Javascript-objects,
not about DOM-objects, 
methinks.

var a = []; // an array
document.write( a.hasOwnProperty('length') ); // true
document.write( '<br>' );

document.write( document.body.hasOwnProperty('style') ); // false
document.write( '<br>' );



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#32896

FromCezary Tomczyk <cezary.tomczyk@gmail.com>
Date2017-04-01 15:59 +0200
Message-ID<obobnk$1hpt$1@csiph.com>
In reply to#32894
On 01/04/2017 12:17, Evertjan. wrote:
> Cezary Tomczyk <cezary.tomczyk@gmail.com> wrote on 01 Apr 2017 in
> comp.lang.javascript:
>
>> Just out of curiosity.
>>
>> Why document.body.hasOwnProperty('className') returns false?
>>
>> Property className does exists for object document.body object.
>
> hasOwnProperty()
> is about Javascript-objects,
> not about DOM-objects,
> methinks.
>
> var a = []; // an array
> document.write( a.hasOwnProperty('length') ); // true
> document.write( '<br>' );
>
> document.write( document.body.hasOwnProperty('style') ); // false
> document.write( '<br>' );

Thanks.

On MDN 
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty 
documentation doesn't mentioned that specifically. Just:

"The hasOwnProperty() method returns a boolean indicating whether the 
object has the specified property."

That's why I was asking.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

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


#32897

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-04-01 16:40 +0200
Message-ID<XnsA74AA9B4C3DE8eejj99@194.109.6.166>
In reply to#32896
Cezary Tomczyk <cezary.tomczyk@gmail.com> wrote on 01 Apr 2017 in
comp.lang.javascript: 

> On 01/04/2017 12:17, Evertjan. wrote:
>> Cezary Tomczyk <cezary.tomczyk@gmail.com> wrote on 01 Apr 2017 in
>> comp.lang.javascript:
>>
>>> Just out of curiosity.
>>>
>>> Why document.body.hasOwnProperty('className') returns false?
>>>
>>> Property className does exists for object document.body object.
>>
>> hasOwnProperty()
>> is about Javascript-objects,
>> not about DOM-objects,
>> methinks.
>>
>> var a = []; // an array
>> document.write( a.hasOwnProperty('length') ); // true
>> document.write( '<br>' );
>>
>> document.write( document.body.hasOwnProperty('style') ); // false
>> document.write( '<br>' );
> 
> Thanks.
> 
> On MDN 
> https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Obj
> ects/Object/hasOwnProperty documentation doesn't mentioned that
> specifically. Just: 
> 
> "The hasOwnProperty() method returns a boolean indicating whether the 
> object has the specified property."

Yes, however in sensu strictior a dom-object is not an object in the 
Javascript sense.

Sometimes the DOM is not part of Javascript, 
like in serverside javascript, in wscript/jscript, etc.


> That's why I was asking.
> 



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#32901

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-04-02 00:39 +0200
Message-ID<1898070.irdbgypaU6@PointedEars.de>
In reply to#32896
Cezary Tomczyk wrote:

> […] Evertjan. wrote:
>> Cezary Tomczyk […] wrote […]:
>>> Just out of curiosity.
>>>
>>> Why document.body.hasOwnProperty('className') returns false?
>>>
>>> Property className does exists for object document.body object.
>>
>> hasOwnProperty()
>> is about Javascript-objects,
>> not about DOM-objects,
>> methinks.
>> […]
> 
> Thanks.

For misinformation based on a jump to conclusion based on cluelessness?
 
> On MDN
> 
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
> documentation doesn't mentioned that specifically.

And there is no reason why it should.

> Just:
> 
> "The hasOwnProperty() method returns a boolean indicating whether the
> object has the specified property."

That wording is not entirely correct or, IOW, it is ambiguous; a matter of 
definition.

In ECMAScript wording, an object has a property if it has the property 
itself (its *own* property) *or* inherits the property from another object 
through the prototype chain.  (The inheritance is implicit; if you attempt 
to access a property of an object that does not have it as its own, lookup 
proceeds along the prototype chain until an object that has that property as 
its own or the end of the prototype chain is reached.)

And that is definitely _not_ what the has*Own*Property() method’s return 
value indicates.  I have clarified the wording in the article accordingly.

> That's why I was asking.

Too bad then that you have not read carefully the “Description” section of 
the article (which clarified that even before my edit), and my explanation 
in my other follow-up.
 
-- 
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]


#32895

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-04-01 13:53 +0200
Message-ID<1640137.tdWV9SEqCh@PointedEars.de>
In reply to#32893
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.

[toc] | [prev] | [standalone]


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


csiph-web