zoukankan      html  css  js  c++  java
  • 绝对相等与弱相等


    console.log([] == ![]); //true //弱相等"=="会进行类型转换,转换成统一类型进行比较 // !符号优于==,[]boolean值为TRUE,所以![]就是FALSE, // 根据转换规则,右边转换成number类型为0,左边[]转换成number类型为0 // 所以两者相等。 console.log([] === ![]); //false //2.绝对相等不会进行类型转换 const a = { value: [3, 2, 1], valueOf: function () { //valueOf是固定方法,返回原始类型的值 return this.value.pop(); }, }
    实例:
    console.log(a
    == 1 && a == 2 && a == 3); //true //js解析会将a转换成number类型,所以结果会是true //首先你需要知道object.prototype.valueOf() //对象a调用valueOf返回了ToObject(value),因为 //value是number类型,所以返回了一个number的对象, //值为参数,最终js引擎将其转换成原始类型,所以最终的结果才会true. var value = 0; Object.defineProperty(window, 'a', { get: function () { return this.value += 1; } }); console.log(a === 1 && a === 2 && a === 3); //true //主要是利用了property这个函数
  • 相关阅读:
    LeetCode Subsets II
    LeetCode Rotate Image
    LeetCode Palidrome Number
    LeetCode Generate Parentheses
    LeetCode Maximum Subarray
    LeetCode Set Matrix Zeroes
    LeetCode Remove Nth Node From End of List
    Linux Loop设备 使用
    Linux 文件系统大小调整
    LeetCode N-Queens II
  • 原文地址:https://www.cnblogs.com/angle-xiu/p/11283742.html
Copyright © 2011-2022 走看看