1

I'd like to put a getter / setter on every html elements title property for example.

I'll be honnest, I don't really know what I'm doing but I've tried redefining the title property but it's not writable so I tried to reassign the Element.property like this :

var o = {};
Object.defineProperty(o, 'innerText', {
  configurable: true, enumerable: true,
  get: function() { return this.__innerText__; },
  set: function(y) {  console.log('setting title'); this.__innerText__  = y; }
});
Object.defineProperty(o, '__innerText__', {
  configurable: true, writable: true, enumerable: true, value :""
});

Element.prototype = Object.assign(o, Element.prototype);

let div = document.createElement('div')
document.body.appendChild(div);
div.innerText = "hey"

// Uncaught TypeError: Illegal invocation
// at Function.assign (<anonymous>)
Ced
  • 15,847
  • 14
  • 87
  • 146
  • 3
    You generally shouldn't be changing `Element.prototype`. See http://stackoverflow.com/questions/3653818/modify-prototypes-of-every-possible-dom-element – Barmar Feb 24 '17 at 23:54
  • @Barmar it's just for development purpose – Ced Feb 25 '17 at 00:07
  • 1
    The browsers don't care why you're doing it. If they don't support it, it won't work. – Barmar Feb 25 '17 at 00:11

0 Answers0