JS noob here: If a getter/setter with a particular name is used that is the same as a property's name that takes on a value, and in code that is in strict mode, will an error occur in ES5? Ex (where yes, I know I am not properly using the getter and setter below in regards to their purpose):
"use strict"; //assume is first line in script or function
let obj = {
p: 5,
get p() {
console.log("in getter");
},
set p(v) {
console.log("in setter: ", v);
}
};
I ask here for a few reasons:
-
- I've had trouble finding a way to run pre-ES6 (namely ES5) JavaScript. (I checked here, for example, and even something like a browser may have a mix and match of ECMAScript standards.)
-
- As of ES6 duplicate property names is not an error (see here).
-
- I've played around with the order of a property and a getter and setter of the same name in ES6, and their order appears to matter. (I know that subsequently-defined properties will now be those used if of the same name, but putting a property after a getter of the same name but before a setter of the same name causes accessing that property to always evaluate to
undefined
, (even when yes, the getter I use is coded to return a non-undefined
value, such as6
).) Ex's:
- I've played around with the order of a property and a getter and setter of the same name in ES6, and their order appears to matter. (I know that subsequently-defined properties will now be those used if of the same name, but putting a property after a getter of the same name but before a setter of the same name causes accessing that property to always evaluate to
//Ex 1:
let obj = {
get p() {
console.log("in getter");
return 6;
},
p: 5,
};
console.log(obj.p); //prints 5
//Ex 2:
let obj2 = {
get p() {
console.log("in getter");
return 6;
},
p: 5,
set p(v) {
console.log("in setter: ", v);
}
};
console.log(obj2.p); //prints undefined