Overriding assignment operator in JS

前端 未结 2 660
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 00:07
var myObject = {\"myKey\" : \"myValue\"}
typeof(myObject.myKey) returns `string`

myObject.myKey = \"newValue\"
console.log(myObject.myKey) prints newValue
2条回答
  •  梦毁少年i
    2021-02-09 00:29

    Browser-supplied host objects behave in ways that are not constrained by the semantics of the language. That is, document looks like a JavaScript object, but it's not. It's part of the runtime environment.

    The JavaScript spec is written in terms of various internal "method" descriptions. Host objects like window and document have special versions of those internal methods. Thus, the runtime follows the spec as to how the = assignment process works, but the internal method [[Put]] is simply special.

提交回复
热议问题