Skip to content

Commit 3b1819e

Browse files
authored
Class field of type function not supported (#33337)
1 parent b4d2ba3 commit 3b1819e

File tree

1 file changed

+34
-0
lines changed
  • aspnetcore/blazor/javascript-interoperability

1 file changed

+34
-0
lines changed

aspnetcore/blazor/javascript-interoperability/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,40 @@ In a few documentation examples, JS interop is used to mutate an element *purely
6262

6363
For more information, see <xref:blazor/js-interop/call-javascript-from-dotnet#capture-references-to-elements>.
6464

65+
## JavaScript class with a field of type function
66+
67+
A JavaScript class with a field of type function is ***not*** supported by Blazor JS interop. Use Javascript functions in classes.
68+
69+
<span aria-hidden="true">❌</span><span class="visually-hidden">Unsupported:</span> `GreetingHelpers.sayHello` in the following class as a field of type function isn't discovered by Blazor's JS interop and can't be executed from C# code:
70+
71+
```javascript
72+
export class GreetingHelpers {
73+
sayHello = function() {
74+
...
75+
}
76+
}
77+
```
78+
79+
<span aria-hidden="true">✔️</span><span class="visually-hidden">Supported:</span> `GreetingHelpers.sayHello` in the following class as a function is supported:
80+
81+
```javascript
82+
export class GreetingHelpers {
83+
sayHello() {
84+
...
85+
}
86+
}
87+
```
88+
89+
Arrow functions are also supported:
90+
91+
```javascript
92+
export class GreetingHelpers {
93+
sayHello = () => {
94+
...
95+
}
96+
}
97+
```
98+
6599
## Avoid inline event handlers
66100

67101
A JavaScript function can be invoked directly from an inline event handler. In the following example, `alertUser` is a JavaScript function called when the button is selected by the user:

0 commit comments

Comments
 (0)