Skip to content

Commit 7a00133

Browse files
committed
docs: drop return global.__native(this) from Android subclass examples
With TypeScript 2.0 and newer the compiled constructor already returns the native instance from `_super.call(this)`, so `global.__native` is a no-op. Remove it from the examples, drop constructors that only called `super()`, and add a short note that existing calls are harmless.
1 parent fe3d252 commit 7a00133

3 files changed

Lines changed: 6 additions & 37 deletions

File tree

‎content/guide/android-marshalling.md‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ let MyVersatileCopyWriter = java.lang.Object.extend({
628628
```ts
629629
@Interfaces([com.a.b.Printer, com.a.b.Copier, com.a.b.Writer]) /* the interfaces that will be inherited by the resulting MyVersatileCopyWriter class */
630630
class MyVersatileCopyWriter extends java.lang.Object {
631-
constructor() {
632-
super();
633-
return global.__native(this);
634-
}
635-
636631
print() { ... }
637632
copy() { ... }
638633
write() { ... }
@@ -653,10 +648,6 @@ Java/Kotlin method overloads are handled by the developer by explicitly checking
653648

654649
```ts
655650
class MyVersatileCopyWriter extends ... {
656-
constructor() {
657-
super();
658-
return global.__native(this);
659-
}
660651
...
661652
print() {
662653
let content = "";

‎content/guide/extending-classes-and-implementing-interfaces-android.md‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ class MyButton extends android.widget.Button {
5353
constructor() {
5454
super()
5555
constructorCalled = true
56-
57-
// necessary when extending TypeScript constructors
58-
return global.__native(this)
5956
}
6057

6158
setEnabled(enabled: boolean): void {
@@ -88,19 +85,17 @@ const button = new MyButton(context)
8885
In the above example, the `setEnabled` function's use of `this` keyword points to the JavaScript object that proxies the extended native instance. The `this.super` usage provides access to the base class method implementation.
8986
:::
9087

88+
:::tip Note
89+
Older examples end the constructor with `return global.__native(this)`. This is not needed with TypeScript 2.0 and newer. The runtime keeps `global.__native` only for backward compatibility, so existing calls are harmless and can be removed.
90+
:::
91+
9192
### Creating an anonymous Java class which extends from the base Java `java.lang.Object` class
9293

9394
::: code-group
9495

9596
```ts [via TypeScript]
9697
@NativeClass
9798
class MyClass extends java.lang.Object {
98-
constructor() {
99-
super()
100-
// necessary when extending TypeScript constructors
101-
return global.__native(this)
102-
}
103-
10499
toString(): string {
105100
// override Object's toString
106101
}
@@ -135,13 +130,6 @@ To create a named Java class which extends from the `java.lang.Object` class:
135130
@NativeClass
136131
@JavaProxy('org.nativescript.example.MyClass')
137132
class MyClass extends java.lang.Object {
138-
// constructor
139-
constructor() {
140-
super()
141-
// necessary when extending TypeScript constructors
142-
return global.__native(this)
143-
}
144-
145133
toString(): string {
146134
// override Object's toString
147135
}
@@ -545,13 +533,6 @@ class ClickListener
545533
extends java.lang.Object
546534
implements android.view.View.OnClickListener
547535
{
548-
constructor() {
549-
super()
550-
551-
// necessary when extending TypeScript constructors
552-
return global.__native(this)
553-
}
554-
555536
onClick(view: android.view.View): void {
556537
// Perform action on click
557538
}

‎content/public/assets/agentic/NATIVESCRIPT.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,14 @@ Rogue timers cause memory leaks and unexpected behavior.
167167
```typescript
168168
@NativeClass()
169169
class MyButton extends android.widget.Button {
170-
constructor() {
171-
super()
172-
return global.__native(this)
173-
}
174-
175170
setEnabled(enabled: boolean): void {
176171
this.super.setEnabled(enabled)
177172
}
178173
}
179174
```
180175

176+
Do not add `return global.__native(this)` to constructors. It is a legacy pattern that is not needed with TypeScript 2.0 and newer; a constructor only needs `super(...)`, and can be omitted if it does nothing else.
177+
181178
### iOS (using @NativeClass decorator)
182179

183180
```typescript

0 commit comments

Comments
 (0)