1

I want to modify the HTMLElement prototype to have a custom function in an instance of HTMLELement. I need for that function to be able to access the HTMLElement Instance's properties like the tagName. The problem is that inside the custom prototype function this points to the Window object instead of the HTMLElement instance. How can I achieve this?

HTMLElement.prototype.doSomething = () => {
  console.log(this)
  // Here I need to do something like console.log(this.tagName)
} 

let elem = document.createElement('div')
elem.doSomething()
// -> Window Object, instead of HTMLElement Instance object.

Thank you

0 Answers0