javascript - Modifying an object's prototype to made a log to the console every time a variable is requested -


i'm sure i've seen before somewhere. i'm beginning looking @ prototypes don't know much.

what's correct prototype declaration catches requests varibles? myobject being object altered prototype.

"myobject.x" caught function, i'd log there's been call attempting access 'x' , return x depending on called (not overly important).

(x valid variable name) edit: 'myobject.foo' give console output of 'foo' , 'myobject.bar' return 'bar'

i've seen used in script variables changed before returned, can't remember enough find again.

you make x method (instead of property), override in subclass (adding logging call) , use prototype.x.call call super method.

var foo = function () {     this.x = function () {         console.log('ancestor x');     } }  var bar = function () {     this.x = function () {         console.log('inherited x');         bar.prototype.x.call(this);     } }  bar.prototype = new foo;  new bar().x(); 

otherwise, there vendor extensions , es5/6 getter methods, you'll have contend erratic browser support either of options.

fiddle


Comments