Override the Equivalence Comparison in Javascript

后端 未结 5 469
一整个雨季
一整个雨季 2020-11-28 15:55

Is it possible to override the equivalence comparison in Javascript?

The closest I have gotten to a solution is by defining the valueOf function and invoking valueOf

5条回答
  •  自闭症患者
    2020-11-28 16:02

    That is because the == operator doesn't compare only primitives, therefore doesn't call the valueOf() function. Other operators you used do work with primitives only. I'm afraid you cannot achieve such thing in Javascript. See http://www.2ality.com/2011/12/fake-operator-overloading.html for some more details.

提交回复
热议问题