Pārlūkot izejas kodu

[js] add Lib.nativeThis (closes #3934)

Simon Krajewski 10 gadi atpakaļ
vecāks
revīzija
ffc290bfa0
2 mainītis faili ar 19 papildinājumiem un 2 dzēšanām
  1. 2 2
      std/js/JQuery.hx
  2. 17 0
      std/js/Lib.hx

+ 2 - 2
std/js/JQuery.hx

@@ -74,7 +74,7 @@ extern class JQueryHelper {
 	public static var JTHIS(get, null) : JQuery;
 
 	static inline function get_JTHIS() : JQuery {
-		return untyped __js__("$(this)");
+		return new JQuery(js.Lib.nativeThis);
 	}
 
 }
@@ -391,7 +391,7 @@ extern class JQuery implements ArrayAccess<Element> {
 	//static function is*, makeArray, map, merge, noop, now, param, proxy, sub, trim, type, unique
 
 	private static inline function get_cur() : JQuery {
-		return untyped js.JQuery(__js__("this"));
+		return new js.JQuery(js.Lib.nativeThis);
 	}
 
 	private static function __init__() : Void untyped {

+ 17 - 0
std/js/Lib.hx

@@ -65,4 +65,21 @@ class Lib {
 	static inline function get_undefined() : Dynamic {
 		return untyped __js__("undefined");
 	}
+
+	/**
+		`nativeThis` is the JavaScript `this`, which is semantically different
+		from the Haxe `this`. Use `nativeThis` only when working with external
+		JavaScript code.
+
+		In Haxe, `this` is always bound to a class instance.
+		In JavaScript, `this` in a function can be bound to an arbitrary
+		variable when the function is called using `func.call(thisObj, ...)` or
+		`func.apply(thisObj, [...])`.
+
+		Read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
+	**/
+	public static var nativeThis(get,never) : Dynamic;
+	@:extern static inline function get_nativeThis() : Dynamic {
+		return untyped __js__("this");
+	}
 }