Both of these conditions are true:
'z-index' in getComputedStyle(document.body) // true
Reflect.has(getComputedStyle(document.body), 'z-index') // true
Additionally, the following statements also evaluate to true, indicating that 'z-index' is an own property of the CSSStyleDeclaration object:
getComputedStyle(document.body).hasOwnProperty('z-index') // true
Object.hasOwn(getComputedStyle(document.body), 'z-index') // true
However, this statement evaluates to false:
Reflect.ownKeys(getComputedStyle(document.body)).includes('z-index') // false
How can Object.hasOwn(x, y)
be true, while Reflect.ownKeys(x).includes(y)
is false? It doesn't seem logical.
It's worth noting that even though getComputedStyle(document.body) returns a read-only computed CSSStyleDeclaration object, the same issue arises with an HTMLElement's style property:
Object.hasOwn(document.body.style, 'z-index') // true
Reflect.ownKeys(document.body.style).includes('z-index') // false
According to MDN (https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle)
CSS property values may be accessed using the getPropertyValue(propName) API or by indexing directly into the object such as obj['z-index'] or obj.zIndex.
The convenience of accessing the value by obj['z-index']
is understood. The confusion lies in the discrepancy between Object.hasOwn(x,y) and Reflect.ownKeys(x).includes(y) under Ecmascript rules.
Edit: A related question on StackOverflow was found (how does it work element.style["background-color"]) but remained unanswered.