Coder Social home page Coder Social logo

前端: JavaScript: === & ==, + about blog HOT 5 OPEN

xvno avatar xvno commented on August 16, 2024
前端: JavaScript: === & ==, +

from blog.

Comments (5)

xvno avatar xvno commented on August 16, 2024

简单说明

== 相等运算符(The Equals Operator),
=== 严格相等运算符(The Strict Equals Operator)

为了方便说明, 在此定义以下简称以便于区分:

{
    "===": "全等",
    "==": "双等"
} 

两种操作最为明显的区别在于 全等要求两个操作数类型相同(typeof),
而且在==规则中, 如果俩操作数的typeof结果相同, 则使用===规则比较(规则第1条)

from blog.

xvno avatar xvno commented on August 16, 2024

全等规则: x === y 的比较逻辑

  1. 判断类型是否相同

    • 不同 -> false
    • 相同 继续进行以下步骤
  2. x和y

    • 同为undefined -> true
    • -> 继续
  3. x和y

    • 同为null -> true
    • -> 继续

    3.1. x,y同为number类型, 则:

    • x或y任意一方为NaN -> false
    • x和y的数值相等 -> true
    • 0与-0 -> true
    • 两数值不想等 -> false

    3.2. 同为string, 则相同位置上的字符对应的16bit作比较

    • 所有位置全相同 -> true
    • -> false

    3.3. 同为boolean,

    • true:true -> true
    • false:false -> true
    • (true, false) -> false

    3.4. 同为引用类型,

    • 指向同一个对象 -> true
    • -> false

from blog.

xvno avatar xvno commented on August 16, 2024

双等规则: x == y 的比较逻辑

x, y 位置可互换, 即在此表示 x == y 与 y == x

  1. typeof类型相等, 则直接用全等规则,
  2. 如果(x, y) 是(undefined, null)的组合关系 - -> true
    即 x:y undefined:null 或 null:undefined,
  3. (number, string), string转换为number, 再使用 全等规则 比较,
  4. x 是boolean, 先把它转换成number, 再递归本 双等规则toNumber(x) == y
    x: true -> 1, false -> 0
  5. x 是 boolean/number/string, y 是 object, 递归比较 x == y.toPrimitive(), toPrimitive = y.valueOf() || y.toString(),
  6. -> false

from blog.

xvno avatar xvno commented on August 16, 2024

特别说明

关于双等规则 第5条, object的.valueOf优先级比.toString要高, 所以 toPrimitive = y.valueOf || y.toString的描述是有效的.

image

from blog.

xvno avatar xvno commented on August 16, 2024

plus: +

two number

  1. Infinity + Infinity = Infinity
  2. -Infinity + -Infinity = Infinity
  3. Infinity +/- Infinity = NaN
  4. NaN + X = NaN (undefined + undefined = NaN)
  5. 1 + 2 = 3

str + x, x + str

  1. x is a number(N, NaN, Infinity, -Infinity): ${str}${x}, ${x}${str}
  2. x is a string: ${str}${x}, ${x}${str}
  3. x is a boolean: x => 'true' or 'false': ${str}${x}, ${x}${str}
  4. x is null or undefined: null => 'null', undefined => 'undefined', then ${str}${x}, ${x}${str}
  5. x is a object: ${str}${object.toString()}, ${object.toString()}${str}, 对象默认 toString() 来自于 Object.prototype.toString()

Object

  1. undefined + undefined = NaN
  2. NaN + undefined = NaN
  3. NaN + NaN = NaN
  4. a.toString() + b.toString()

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.