• 你真得懂Javascript中的==等于运算符吗?


    var i = 2;
    Number.prototype.valueOf = function() {
    return i++;
    };
    var a = new Number( 42 );
    if (a == 2 && a == 3) {
    console.log( "Yep, this happened." );

    }

    ============================


    "0" == null; // false
    "0" == undefined; // false
    "0" == false; // true -- UH OH!
    "0" == NaN; // false
    "0" == 0; // true
    "0" == ""; // false


    false == null; // false
    false == undefined; // false
    false == NaN; // false
    false == 0; // true -- UH OH!
    false == ""; // true -- UH OH!
    false == []; // true -- UH OH!
    false == {}; // false


    "" == null; // false
    "" == undefined; // false
    "" == NaN; // false
    "" == 0; // true -- UH OH!
    "" == []; // true -- UH OH!
    "" == {}; // false


    0 == null; // false
    0 == undefined; // false
    0 == NaN; // false
    0 == []; // true -- UH OH!
    0 == {}; // false



    [] == ![]; // true

    2 == [2]; // true
    "" == [null]; // true

    0 == " "; // true


    var a = null;
    var b = Object( a ); // same as `Object()`
    a == b; // false
    var c = undefined;
    var d = Object( c ); // same as `Object()`
    c == d; // false
    var e = NaN;
    var f = Object( e ); // same as `new Number( e )`
    e == f; // false


    ======================================================

    ES5规范中的解释:

    Comparing: objects to nonobjects
    If an object/function/array is compared to a simple scalar primitive
    (string, number, or boolean), the ES5 spec says in clauses
    11.9.3.8-9:
    1. If Type(x) is either String or Number and Type(y) is Object,
    return the result of the comparison x == ToPrimitive(y).
    2. If Type(x) is Object and Type(y) is either String or Number,
    return the result of the comparison ToPrimitive(x) == y.



    Comparing: nulls to undefineds
    Another example of implicit coercion can be seen with == loose
    equality between null and undefined values. Yet again quoting the
    ES5 spec, clauses 11.9.3.2-3:
    1. If x is null and y is undefined, return true.
    2. If x is undefined and y is null, return true.



    Comparing: anything to boolean
    1. If Type(x) is Boolean, return the result of the comparison
    ToNumber(x) == y.
    2. If Type(y) is Boolean, return the result of the comparison x ==
    ToNumber(y).


    Comparing: strings to numbers

    1. If Type(x) is Number and Type(y) is String, return the result of
    the comparison x == ToNumber(y).
    2. If Type(x) is String and Type(y) is Number, return the result of
    the comparison ToNumber(x) == y.


    ==============================直观的比較图

    http://dorey.github.io/JavaScript-Equality-Table/

  • 相关阅读:
    async/await
    js继承
    js处理上下文代码的2个阶段
    js相关地址
    vue自定义指令钩子函数
    vue 组件
    vue methods和computed,v-show和v-if
    vue 生命周期
    DOM操作——怎样添加、移除、移动、复制、创建和查找节点
    js 传递参数
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7106603.html
Copyright © 2020-2023  润新知