如何重载JavaScript对象的运算符相等 [英] How to overload operator equality for JavaScript objects

查看:59
本文介绍了如何重载JavaScript对象的运算符相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Dojo.declare创建了新对象。如何为对象重载运算符==?

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

推荐答案

您不能重载 == ,但 == 有一个隐式 .toString()调用,因此无论 .toString()返回将使您可以有效地重载 == (kinda):

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

关于如何在Dojo中做到这一点,我不使用Dojo,对不起,但要点是,您可以引用创建的任何对象,并像我的示例一样添加 thatObject.prototype.toString

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.

这篇关于如何重载JavaScript对象的运算符相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆