I’m trying to attach a method mytest
to a function prototype:
(function() { window.onload = function() { var a = (e) => document.querySelector(e); a.prototype.mytest = function(e){ ... }; //example div a("div").mytest("d"); }(); })();
But I get:
Uncaught TypeError: Cannot set property ‘mytest’ of undefined”
Advertisement
Answer
Even if you fix the fact that the arrow function has no prototype by using a regular function, you cant assign to the prototype of the value returned by that function. So you’ll never be able to call a(...).mytest(...)
.
Maybe this is along the lines of what you were trying to achieve
(function() { window.onload = function() { const a = (e) => ({ elem: document.querySelector(e), mytest: function(e) { console.log(this.elem, e) } }) //example div a("div").mytest("d"); }(); })();
<div></div>