JavaScript对象是否有总订单? [英] Is there a total order on JavaScript objects?

查看:47
本文介绍了JavaScript对象是否有总订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎说明了一切.使用< > < = > = 运算符?

The title pretty much says it all. Does JavaScript guarantee a total order on objects when using the <, >, <= and >= operators?

我写了一些代码只是为了检查某些对象的总排序.结果与总排序一致,但这并不能证明任何事情:

I wrote some code just to check total ordering on some objects. The result is consistent with total ordering, but that doesn't prove anything:

function thereIsTotalOrder(items){

    var one, other, theThird;

    // warning: n^3 complexity follows

    // If a <= b and b <= a then a = b (antisymmetry);
    for(var i=0; i<items.length; i++){
        for(var j=0; j<items.length; j++){
            one = items[i];
            other = items[j];
            if((one <= other) && (other <= one) && (one != other)){
                return false;
            }
        }
    }

    // If a <= b and b <= c then a <= c (transitivity)
    for(var i=0; i<items.length; i++){
        for(var j=0; j<items.length; j++){
            for(var k=0; k<items.length; k++){
                one = items[i];
                other = items[j];
                theThird = items[k];
                if((one <= other) && (other <= theThird) && !(one <= theThird)) {
                    return false;
                }
            }
        }
    }

    // a <= b or b <= a (totality). 
    for(var i=0; i<items.length; i++){
        for(var j=0; j<items.length; j++){
            one = items[i];
            other = items[j];
            if(!((one <= other) || (other <= one))) {
                return false;
            }
        }
    }

    return true;
}

function a(){};
function b(){};
var c = "foo";
var d = "bar";
var e = "bar";
var f = function(){};
var g = {name : "bananas"};

console.log(thereIsTotalOrder([a, b, c, d, e, f, g])); // prints "true"

推荐答案

取决于我们正在考虑的对象.如果我们将注意力集中在数字,那么是的,订单将是总计.如您的示例所示,(至少有些)

Depends on what objects we're considering. If we confine our attention to the class of numbers, then yes, the order will be total. As your example demonstrates, the same can be said of (at least some) strings. But totality doesn't seem to hold generally.

例如,如果在声明中添加 var h = 5; ,然后在 thereIsTotalOrder 调用中添加 h false .那是因为在 h = 5 c ='foo'的状态下,( h c c h )为假(这意味着总计不满意).

For instance, if you add var h = 5; to your declarations and then add h to your thereIsTotalOrder call, you will get a false. That's because in a state where h = 5 and c = 'foo', (hcch) is false (which means that totality is not satisfied).

正如您正确地指出的那样,尽管 thereIsTotalOrder 返回的错误值不存在并不证明所有对象都是完全有序的,但false 确实证明所有对象之间的顺序(如果已定义)不是总计.

As you rightly noted, while the absence of a false value returned by thereIsTotalOrder doesn't prove that all objects are totally ordered, the presence of a false value does prove that the order (if defined) between all objects is not total.

这篇关于JavaScript对象是否有总订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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