How to overload operator equality for JavaScript o

2019-03-27 16:00发布

I have created new objects with Dojo.declare. How to overload operator == for objects ?

3条回答
欢心
2楼-- · 2019-03-27 16:27

You can't. JavaScript doesn't support operator overloading.

查看更多
三岁会撩人
3楼-- · 2019-03-27 16:30

You can't overload ==, but == has an implicit .toString() call, so whatever .toString() returns will allow you to effectively overload == (kinda):

function foo(){}
foo.prototype.toString = function(){ return 42; }

var x = new foo();
x == 42; // true

As for how to do this in Dojo, I don't use Dojo, sorry, but the gist is that you get a reference to whatever object is creates and add thatObject.prototype.toString as in my example.

查看更多
Evening l夕情丶
4楼-- · 2019-03-27 16:47

You can't in Java/ECMAscript. You can only overload operators using ExtendScript from Adobe. See this example. Also see this blog entry (pro), or this (contra).

查看更多
登录 后发表回答