Javascript 为什么window.getComputedStyle()返回属性名为“0”的对象;背景色“;但使用的是属性名称“;背景色;也有用吗?

Javascript 为什么window.getComputedStyle()返回属性名为“0”的对象;背景色“;但使用的是属性名称“;背景色;也有用吗?,javascript,ecmascript-6,getcomputedstyle,Javascript,Ecmascript 6,Getcomputedstyle,至少在Chrome和Firefox中是这样: Object.keys(getComputedStyle(document.body)).includes("backgroundColor") // => true Object.keys(getComputedStyle(document.body)).includes("background-color") // => false 然而 所以,如果背景色不是一个键,怎么能 getComputedStyle(document.bo

至少在Chrome和Firefox中是这样:

Object.keys(getComputedStyle(document.body)).includes("backgroundColor")  // => true
Object.keys(getComputedStyle(document.body)).includes("background-color") // => false
然而

所以,如果背景色不是一个键,怎么能

getComputedStyle(document.body)["background-color"]

展示什么?我知道在jQuery中,
fontSize
font-size
等相同,但如果是属性值访问,则违反了如何访问对象属性值的规则。任何JS对象都可以这样做吗?

没有必要单独看到这种行为:
对象。键只返回自己的、可枚举的、字符串命名的属性。任何JS对象都可以通过使用
“背景色”

  • 原型链上的较高位置,或
  • 不可枚举
  • 前者适用于当前Firefox上的
    “background color”
    ,它是计算样式原型上的getter/setter对:

    console.log(“getComputedStyle(document.body)中的背景色”);
    console.log(
    
    getOwnPropertyDescriptor(CSS2Properties.prototype,“背景色”)
    如果我做了
    obj=getComputedStyle(document.body)
    并继续显示
    obj.\uuu-proto\uuuu
    obj.\uu-proto\uu.\uu-proto\uu
    我没有看到名为
    背景色的属性,但是
    对象.getOwnPropertyDescriptor(window.getComputedStyle(document.body),“背景色”)
    会导致
    {value:“rgb(255,255,255)”,可写:真,可枚举:真,可配置:真}
    窗口.getComputedStyle(document.body).hasOwnProperty('background-color')
    ->
    。要么我缺少什么,要么浏览器不可用lying@Ry-
    Object.keys(obj.\uuuuu proto\uuuuuu)。包括(“背景色”)//=>false
    @CertainPerformance:Hm,这可能在Chrome上很神奇。在Firefox上则不然。(只有FF按照规格提供)
    getComputedStyle(document.body)["background-color"]