How do you call member functions on existing JavaScript objects? #88517
-
Using https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/import-export-interop?view=aspnetcore-7.0, I am not sure how to call member functions on existing for (var [name,node] of context.program.sourceFiles){
console.log(name);
} I would like to do the above in C#. Here is a failed attempt var program = context.GetPropertyAsJSObject("program");
var sourceFiles = program.GetPropertyAsJSObject("sourceFiles");
// How do I call 'entries'?
// This does not work.
var entries = MapPrototype.entries(sourceFiles); Where I was trying to define public partial class MapPrototype {
[JSImport("entries")]
public static partial JSObject entries(JSObject self);
} cc @pavelsavara |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
OOP in not supported in Net7. You need to create another method in JS, wrap it as |
Beta Was this translation helpful? Give feedback.
-
#[wasm_bindgen(method)] lets Rust call JavaScript object methods while correctly handling the this context. |
Beta Was this translation helpful? Give feedback.
-
The comment is pointing out that even though using #[wasm_bindgen(method)] has drawbacks, it’s useful when you need Rust to call methods on JS objects. |
Beta Was this translation helpful? Give feedback.
-
The comment is pointing out that even though using #[wasm_bindgen(method)] has drawbacks, it’s useful when you need Rust to call methods on JS objects. |
Beta Was this translation helpful? Give feedback.
-
sourceFiles.InvokeVoid("forEach", (JSObject node, string name) => |
Beta Was this translation helpful? Give feedback.
Not yet, maybe Net9 but that's open discussion. OOP over interop boundary is not good idea in my opinion. It leads to chatty interactions with lots of overhead, GC pressure and other problems. I advise to create higher level functionality methods and interop with that instead.