javascript - Getter property not returning updated value in Console.log -
i bit stuck below scenario. reading javascript: definitive guide , came below code under accessor properties.
function inherit(p) { if (p == null) throw typeerror(); if (object.create) { return object.create(p); } var t = typeof p; if (t !== 'object' && t !== 'function') throw typeerror(); function f() {}; f.prototype = p; return new f(); };
the above simple code sets prototype of newly created object. below simple code, in when next
called, supposed return value greater 55, i.e. 56
var serialnum = { $n: 0, next() { return this['$n']++; }, set next(n) { if (n >= this.$n) { this.$n = n; } else { throw 'serial number can set larger value'; } } }; var genserialnum = inherit(serialnum); genserialnum.$n = 55; console.log(genserialnum.next);
so when set $n
of genserialnum
object creates $n
property of genserialnum
, on calling getter property next
, increments 55 56(as can see scope variable of chrome dev tools), on printing in console displays 55. why?
Comments
Post a Comment