this.textsize不是函数
我有这个错误:
uck offult(在承诺中)typeerror:this.textsize不是函数
如果我只是放置textsize
,它告诉我它没有定义。
当它们位于另一个文件中的同一文件中时
textSize(text, classname="textDefault") {
/* Get the size of the given text rendered in pixels.
text : string
output : dict
*/
if (!d3) return;
let container = d3.select("body")
.append("svg");
container.append("text")
.attr("class", classname)
.attr("x", -99999)
.attr("y", -99999)
.text(text);
let size = container.node().getBBox();
container.remove();
return { "width": size.width, "height": size.height };
},
getYTickNumberWidth(dataValues, tickLabelFormat) {
/* Get width for y tick number labels.
dataValues : list/float
tickLabelFormat : function
output : float
*/
return d3.max(dataValues.map(
function(d) {
console.log(this.textSize())
let size = this.textSize(tickLabelFormat(d));
return size.width;
}
));
},
I have this error :
Uncaught (in promise) TypeError: this.textSize is not a function
for this.textSize
if I just put textSize
it tells me that it is not defined.
while they are in the same file one below the other
textSize(text, classname="textDefault") {
/* Get the size of the given text rendered in pixels.
text : string
output : dict
*/
if (!d3) return;
let container = d3.select("body")
.append("svg");
container.append("text")
.attr("class", classname)
.attr("x", -99999)
.attr("y", -99999)
.text(text);
let size = container.node().getBBox();
container.remove();
return { "width": size.width, "height": size.height };
},
getYTickNumberWidth(dataValues, tickLabelFormat) {
/* Get width for y tick number labels.
dataValues : list/float
tickLabelFormat : function
output : float
*/
return d3.max(dataValues.map(
function(d) {
console.log(this.textSize())
let size = this.textSize(tickLabelFormat(d));
return size.width;
}
));
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于您使用的是功能而不是箭头功能。
尝试以下操作:
I think the problem is that you are using a function instead of an arrow function.
try this:
这里的问题是
此
的范围。当您使用常规函数Map 包含其自己的并尝试访问textsize
的映射。因此,您必须使用箭头函数,其中
的值此
在函数的整个生命周期中保持不变,并且始终绑定到this
的值 - 箭头父函数。演示:
Here issue is with the scope of
this
. As you are using regular function insidemap
which contains it's ownthis
and trying to accesstextSize
in it's own scope.Hence, You have to use arrow function in which the value of
this
remains the same throughout the lifecycle of the function and is always bound to the value ofthis
in the closest non-arrow parent function.Demo :