Reputation: 605
I am trying to create a simple extension for SVGTextElement, here it is:
interface SVGTextElement {
setX(value: string): SVGTextElement;
setY(value: string): SVGTextElement;
}
SVGTextElement.prototype.setX = (value: string): SVGTextElement => {
var el: SVGTextElement = this;
el.setAttribute("x", value);
return el;
}
SVGTextElement.prototype.setY = (value: string): SVGTextElement => {
var el: SVGTextElement = this;
el.setAttribute("y", value);
return el;
}
I am using this extension like this:
const e = document.createElementNS("http://www.w3.org/2000/svg", "text");
e.setX("0");
But I am getting error:
SvgTextElementExtensions.ts:18 Uncaught TypeError: el.setAttribute is not a function
What am I doing wrong?
Upvotes: 2
Views: 148
Reputation: 14503
You should not use the fat arrow syntax here, that will bind this
to window, and window does not have setAttribute
. Do this:
SVGTextElement.prototype.setX = function (value: string): SVGTextElement {
var el: SVGTextElement = this;
el.setAttribute("x", value);
return el;
}
SVGTextElement.prototype.setY = function (value: string): SVGTextElement {
var el: SVGTextElement = this;
el.setAttribute("y", value);
return el;
}
Upvotes: 1