What are __defineGetter__() and __defineSetter__() functions?

后端 未结 3 904
深忆病人
深忆病人 2020-12-28 14:06

What are __defineGetter__() and __defineSetter__() functions in prototype of every Object?

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 14:14

    See the MDN docs here for a description and example code:

    A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties.

    As noted in the docs (and by @ cwallenpoole), __define[GS]etter__() functions are now deprecated. There's a lot more detail in this article. I believe the defineProperty() function is now the preferred syntax.

提交回复
热议问题