In the following code, both uses of console.log(ox)
print 1
. What happens to the assignment ox = 2
? Is it just ignored?
var o = {
get x() {
return 1;
}
}
console.log(o.x); // 1
o.x = 2
console.log(o.x); // 1
In sloppy mode, yes, it'll just be ignored - the value "assigned" will be discarded. But in strict mode (which is recommended), the following error will be thrown:
Uncaught TypeError: Cannot set property x of
#<Object>
which has only a getter
'use strict'; var o = { get x() { return 1; } } console.log(ox); // 1 ox = 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.