我试图利用自定义运算符对我的应用程序中的自定义类进行算术运算,该类与ClearScript接口。以下是我的示例自定义类的摘要:
public class Vector3 {
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
public Vector3(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3 operator +(Vector3 a, Vector3 b) {
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
}
我的ClearScript引擎已正确初始化,我可以Vector3
通过Javascript正确初始化对象,并相应地修改属性。
但是,如果我Vector3
在Javascript环境中初始化2个对象,并尝试使用Javascript加法运算符,则最终会将加法运算符评估为字符串连接,而不是我的自定义运算符。
例:
var a = new Vector3(1, 1, 1);
var b = new Vector3(0, 2, -1);
var c = a + b;
print(typeof a); //returns "function" (which is correct)
print(typeof b); //returns "function" (which is also correct)
print(typeof c); //returns "string" (should return function)
该变量c
仅包含string
([object HostObject][object HostObject]
),而不包含Vector3
对象。
如何让Javascript引擎知道调用我的自定义运算符,而不是通过ClearScript使用默认Javascript运算符?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句