Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4258878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:42:29+00:00 2026-05-21T05:42:29+00:00

I have a custom Javascript class (created using John Resig’s Simple Javascript Inheritance ).

  • 0

I have a custom Javascript class (created using John Resig’s Simple Javascript Inheritance). I want to be able to compare two instances of this class, using the ==, <, >, >=, and <= symbols.

How do I override the comparators of my custom class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:42:30+00:00Added an answer on May 21, 2026 at 5:42 am

    this cannot be done in the way that you are implying it should be done (though that would be sweet). The best way I have seen this done is to implement on the prototype a set of methods to act like comparatives:

    gte : function( obj ){ // greater than or equal
      // return custom comparison with this as the object comparable on the left
    },
    gt : function( obj ){...}, // greater than but not equal
    eq : function( obj ){...}, // equal to
    // etc.
    

    I was thinking about this problem somemore at work today and there is an alternate way to take advantage of the standard comparison operators but have custom object comparisons. The trick would be to have a property (getter) on the object that represented the comparable state. This would require that all instances of the object evaluate to the same numeric value given the same comparable properties. As an example let’s talk vectors:

    function Vector(x,y,z){
      this.comp = function(){
        // assuming these are floats you may wish to create a comparable level of
        // precision. But this gets the point across.
        return x + (y * 10) + (z * 100);
      }
    }
    

    then when you set up vectors:

    var v1 = new Vector(1,1,1);
    var v2 = new Vector(1,0,1);
    v1.comp() > v2.comp() // true
    

    This only works of course if you are dealing with objects that can be broken down into simple numeric expression of value, but the upside is that the implementation code to get the basic effect is pretty low and you could even go so far as to make the object itself a function that returns the numeric expression of it’s component parts.

    function Vector(x,y,z){
      var v = function v(){
        return v.x + (v.y * 10) + (v.z * 100);
      }
      v.x = x;
      v.y = y;
      v.z = z;
      return v;
    }
    

    now you have all the benefits of the object with easy numeric comparisons and it’s even kinda terse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom UI control which has a JavaScript class written around the
Can you have custom client-side javascript Validation for standard ASP.NET Web Form Validators? For
I have a custom validation function in JavaScript in a user control on a
I have a class library that I have written in C#.net. I want to
I have created a web custom control and used within an aspx page. I
I have a little trouble understanding scope and lifetime using javaScript and jQuery. I
I'm working on some custom Javascript for a CMS template at work. I want
I have custom coded several enterprise applications for mid to large organizations to use
I have custom errors configured in my web.config, but IIS 6.0 is returning the
I have a few models that need to have custom find conditions placed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.