资讯专栏INFORMATION COLUMN

es5标准-9.12 The SameValue Algorithm-实现

whataa / 2293人阅读

摘要:链接描述类型不同类型相同都为有三种同为同为处理都为时虽都为但不同或或有三种不考虑其他情况剩余暂时未实现第条的以及相等的解析未做。。

9.12 The SameValue Algorithm链接描述

9.12 The SameValue Algorithm
The internal comparison abstract operation SameValue(x, y), where x and y are ECMAScript language values, produces true or false. Such a comparison is performed as follows:

1.If Type(x) is different from Type(y), return false.
2.If Type(x) is Undefined, return true.
3.If Type(x) is Null, return true.
4.If Type(x) is Number, then.

a.If x is NaN and y is NaN, return true.
b.If x is +0 and y is -0, return false.
c.If x is -0 and y is +0, return false.
d.If x is the same Number value as y, return true.
e.Return false.

5.If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
6.If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
7.Return true if x and y refer to the same object. Otherwise, return false.

function SameValue (a,b){
  // Number.isNaN=Number.isNaN||function (a){return typeof a==="number"&&isNaN(a);}; 
   if (typeof a !== typeof b) {
       //类型不同
        return false;
   } else if (typeof a === "undefined") {
       //类型相同都为undfined
        return true;
   } else if (typeof a === "object"){
       //object有三种Array,{},null
       if (a === null && b === null) {
           //同为null
            return true;
       } else if (Array.isArray(a) && Array.isArray(b)){
           //同为array
           var alength = a.length,
                blength = b.length;
            if (alength === blength){
                for (var i = 0; i < alength; i++) {
                    if (!SameValue(a[i], b[i])) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
       }else if (a !== null && !Array.isArray(a) && b !==null && !Array.isArray(b)){
            //处理都为object时
            return true;
       }else{
           //虽都为object,但不同null或array或{}
            return false;
       }
   } else if (typeof a === "number") {
       //number有三种NaN,0,+-infinity不考虑
        if (isNaN(a) && isNaN(b)){
            return true;
        }else if (a === b){
            return true;
        }else{
            return false;
        }
   }else if (a.toString() === b.toString()){
       //其他情况剩余function
        return true;
   }else{
        return false;
   }
}

暂时未实现第4条的b,c..以及{}相等的解析未做。。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/81308.html

相关文章

  • 重读Javascript之Object

    摘要:对象是中最常的内置对象之一。为了节省内存,使用一个共享的构造器使用更安全的引用如果不是或,抛出一个异常。使创建的一个新的对象为,就和通过表达式创建一个新对象一样,是标准内置的构造器名设置的内部属性为。方法返回一个该对象的字符串表示。 Object 对象是Javascript中最常的内置对象之一。除了null 和 undefined,其他的所有的都可以转换为对象。可以把对象看成含有键值一...

    Alex 评论0 收藏0
  • JS中的关系比较与相等比较运算

    摘要:在中的关系比较运算,指的是像这种大小值的关系比较。而相等比较,可区分为标准相等比较与严格相等比较两大种类。 在JS中的关系比较(Relational Comparison)运算,指的是像x < y这种大小值的关系比较。 而相等比较,可区分为标准相等(standard equality)比较x == y与严格相等(strict equality)比较x === y两大种类。严格相等比较会...

    paraller 评论0 收藏0
  • JavaScript "相等" 的二三事

    摘要:还规定了无穷及其它的相应规范,有兴趣可自行查找相关资料。其它相同数值相等。类型中,引用同一对象,相等。不同点对的判断上各有不同。以为代表的相等和相等以为代表的不相等和相等以为代表的相等和不相等相同类型采用严格比较。 相等不相等? 先来随便举几个?吧~ 0 == true //? [1] == [1] //? [1] == 1 ...

    wanghui 评论0 收藏0
  • 【译】ECMAScript文档---序言及1-6章(下)

    摘要:除非在本规范中其它指定的文法产生式可选部分隐式地包含一个叫做的接收一个值为包含相关产生式的文法的终结符或者非终结符的参数静态语义规则的定义。 5.2 算法约定(Algorithm Conventions)   规范常常使用一个带编号的列表来显示算法中的步骤。这个小算法被用作准确地表达ECMAScript语言构造需要的语义。这些算法并不是打算暗示任何具体实现的使用。事实上,这里也许有更高...

    xiaokai 评论0 收藏0
  • 你真的理解==和===的区别吗?

    摘要:上面的理解是错的,和返回就可以推翻。解释不清楚和是相等的。和的规则类似,唯一少了转换的一步。三高级阶段参考规范真正理解真的如高设所说的那样吗其实不然。来分析一个经典的例子,看完彻底理解的强制转换。 showImg(https://segmentfault.com/img/remote/1460000011658462?w=512&h=321); 用中文怎么叫合适?相等?全等?其实并不合...

    TwIStOy 评论0 收藏0

发表评论

0条评论

whataa

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<