英文:
Can not pass parameters to js.Func
问题
我有以下代码,其中函数正确地附加到按钮的onclick事件上:
func f(this js.Value, args []js.Value) interface{} {
    println("hello world")
    return nil
}
btn.Set("onclick", js.FuncOf(f))
我尝试将参数传递给这个函数:
btn.Set("onclick", js.FuncOf(f).Invoke("Hello World"))
并在js.Func中进行操作:
for x := range args {
    println(x)
}
但是我得到了错误:
wasm_exec.js:421 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'exports')
    at syscall/js.valueInvoke (wasm_exec.js:421)
    at syscall_js.valueInvoke (main.wasm:0x133521)
    at syscall_js.Value.Invoke (main.wasm:0x131300)
    at main.html (main.wasm:0x25b572)
    at main.main (main.wasm:0x25e591)
    at runtime.main (main.wasm:0x7f285)
    at wasm_pc_f_loop (main.wasm:0xddf81)
    at wasm_export_run (main.wasm:0xddf54)
    at global.Go.run (wasm_exec.js:577)
    at init (wasm.js:7)
有什么想法吗?
英文:
I've the below code where the function attached properly to the button onclick event:
func f(this js.Value, args []js.Value) interface{} {
	println("hello world")
	return nil
}
btn.Set("onclick", js.FuncOf(f))
I tried to pass args for this function as:
btn.Set("onclick", js.FuncOf(f).Invoke("Hello World"))
And manipulate it in the js.Func as:
	for x := range args {
		println(x)
	}
But I got the error:
wasm_exec.js:421 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'exports')
    at syscall/js.valueInvoke (wasm_exec.js:421)
    at syscall_js.valueInvoke (main.wasm:0x133521)
    at syscall_js.Value.Invoke (main.wasm:0x131300)
    at main.html (main.wasm:0x25b572)
    at main.main (main.wasm:0x25e591)
    at runtime.main (main.wasm:0x7f285)
    at wasm_pc_f_loop (main.wasm:0xddf81)
    at wasm_export_run (main.wasm:0xddf54)
    at global.Go.run (wasm_exec.js:577)
    at init (wasm.js:7)
Any thought?
答案1
得分: 1
我使用.bind解决了这个问题,代码如下所示:
element.Set("onclick", js.FuncOf(f).Call("bind", e, args...)) 
// bind(event, arguments...), 作为(arguments..., event)传递给函数f
所以,我的代码变成了:
btn.Set("onclick", js.FuncOf(f).Call("bind", btn, "hi", "hello"))
func f(this js.Value, args []js.Value) interface{} {
	for index, item := range args {
		if x < len(args)-1 { // len(args)-1 是事件本身
			println(index, item.String())
		}
	}
	self := args[len(args)-1].Get("target")
	// self = event.target == this
	self.Get("style").Call("setProperty", "background-color", "red")
    // 同样的效果:
	// this.Get("style").Call("setProperty", "background-color", "red")
	return nil
}
英文:
I solved it using .bind as below:
element.Set("onclick", js.FuncOf(f).Call("bind", e, args...)) 
// bind(event, arguments...), passed to function f as (arguments..., event)
So, my code become:
btn.Set("onclick", js.FuncOf(f).Call("bind", btn, "hi", "hello"))
func f(this js.Value, args []js.Value) interface{} {
	for index, item := range args {
		if x < len(args)-1 { // len(args)-1 is the event itself
			println(index, item.String())
		}
	}
	self := args[len(args)-1].Get("target")
	// self = event.target == this
	self.Get("style").Call("setProperty", "background-color", "red")
    // same as:
	// this.Get("style").Call("setProperty", "background-color", "red")
	return nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论