Sfoglia il codice sorgente

[js] use Object.create for Type.createEmptyInstance

Dan Korostelev 8 anni fa
parent
commit
7cf09570b1
2 ha cambiato i file con 7 aggiunte e 1 eliminazioni
  1. 1 1
      std/js/Object.hx
  2. 6 0
      std/js/_std/Type.hx

+ 1 - 1
std/js/Object.hx

@@ -27,7 +27,7 @@ import haxe.DynamicAccess;
 @:native("Object")
 @:native("Object")
 extern class Object {
 extern class Object {
 	static function assign<T:{}>(target:T, sources:Rest<{}>):T;
 	static function assign<T:{}>(target:T, sources:Rest<{}>):T;
-	@:pure static function create<T:{}>(proto:{}, ?propertiesObject:DynamicAccess<ObjectPropertyDescriptor>):T;
+	@:pure static function create<T>(proto:{}, ?propertiesObject:DynamicAccess<ObjectPropertyDescriptor>):T;
 	static function defineProperties<T:{}>(obj:T, props:DynamicAccess<ObjectPropertyDescriptor>):T;
 	static function defineProperties<T:{}>(obj:T, props:DynamicAccess<ObjectPropertyDescriptor>):T;
 	static function defineProperty<T:{}>(obj:T, prop:String, descriptor:ObjectPropertyDescriptor):T;
 	static function defineProperty<T:{}>(obj:T, prop:String, descriptor:ObjectPropertyDescriptor):T;
 	static function freeze<T:{}>(obj:T):T;
 	static function freeze<T:{}>(obj:T):T;

+ 6 - 0
std/js/_std/Type.hx

@@ -114,10 +114,16 @@ enum ValueType {
 		}
 		}
 	}
 	}
 
 
+	#if (js_es < 5)
 	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
 	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
 		__js__("function empty() {}; empty.prototype = cl.prototype");
 		__js__("function empty() {}; empty.prototype = cl.prototype");
 		return __js__("new empty()");
 		return __js__("new empty()");
 	}
 	}
+	#else
+	public static inline function createEmptyInstance<T>( cl : Class<T> ) : T {
+		return js.Object.create((cast cl).prototype);
+	}
+	#end
 
 
 	public static function createEnum<T>( e : Enum<T>, constr : String, ?params : Array<Dynamic> ) : T {
 	public static function createEnum<T>( e : Enum<T>, constr : String, ?params : Array<Dynamic> ) : T {
 		var f:Dynamic = Reflect.field(e,constr);
 		var f:Dynamic = Reflect.field(e,constr);