Getter and Setters lookup #730
Replies: 1 comment
-
Looking at the sentence before, all it means is "Setting a property to an object creates an own property" will happen if no setter is defined on that object. Otherwise, the setter will do whatever it is written to do. And "Is there a 'c' own property on o? No, check its prototype" will happen if no getter is defined on that object. Otherwise, the getter will do what it is written to do. It seems to work as you described. It is mentioned in passing here. The way I would explain it is... (defined means set to any value or by
I'm not sure if there's a clearly stated explanation like that on MDN or not. I hope this helps anyway 👍 |
Beta Was this translation helpful? Give feedback.
-
On the Inheritance and the prototype chain page, there's a line -
The only exception to the getting and setting behaviour rules is when it's intercepted by a getter or setter.
I am looking for a page which discusses this exception. I believe this exception refers to the getter and setter inheritance(I could be wrong though) which I am trying to understand. I noticed this behaviour in the below example -
Even though
fullName
which is an accessor property is in the[[Prototype]]
oftempUser
which is nothing butuser
, it'sset
method is called during assignmenttempUser.fullName = "Jack Roe"
. In contrast, in the case of data property,fullName
property would be created directly ontempUser
. For example -Note that the
user.fullName
is stillJohn Doe
and did not change toJack Roe
Further, if I remove the setter from the
user
object, the assignmenttempUser.fullName = "Jack Roe"
fails silently in non-strict mode and throws TypeError in strict mode -Can anyone please explain the above behaviour and refer me to a page on MDN Docs which explains it? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions