gpt4 book ai didi

javascript - 在回调中访问父对象和调用对象?

转载 作者:行者123 更新时间:2023-11-29 17:28:52 25 4
gpt4 key购买 nike

我有一个库提供的函数,它在一些处理后运行回调。

在该回调中,我想访问其方法启动该回调的父对象 - 见下文。

class MaskMaker
addMaskedImage: (imagefile, texturefile, canvasid) ->
$('<img src="'+imagefile+'">').load ->
console.log('Id like to call another MaskMaker method with @width as a parameter')

显然 => 将允许我访问父对象,如 this/@,-> 将允许我访问触发回调的元素,如 this/@。但是,什么是同时做到这两点的最巧妙的方法,例如,这样我就可以使用图像宽度作为参数调用 MaskMaker 的直接方法?我需要 that = this hack 还是有更好的东西?

谢谢!

最佳答案

But what's the neatest way to do both, eg, so I could call a direct method of MaskMaker with the images width as a parameter?

您不能同时选择两者(显然),因此必须选择 => + event.target

$('<img src="'+imagefile+'">').load (ev) => @method ev.target.width

还是不错的

that = this
$('<img src="'+imagefile+'">').load -> that.method @width

另请参阅:https://github.com/jashkenas/coffee-script/issues/1230

关于javascript - 在回调中访问父对象和调用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6310178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com