How to overload operator equality for JavaScript objects

£可爱£侵袭症+ 提交于 2019-12-04 21:33:18

问题


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


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/5618763/how-to-overload-operator-equality-for-javascript-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!