zoukankan      html  css  js  c++  java
  • js比较,隐式转换

    隐性类型转换步骤

    一、首先看双等号前后有没有NaN,如果存在NaN,一律返回false。

    二、再看双等号前后有没有布尔,有布尔就将布尔转换为数字。(false是0,true是1)

    三、接着看双等号前后有没有字符串, 有三种情况:

    1、对方是对象,对象使用toString进行转换;

    2、对方是数字,字符串转数字;(前面已经举例)

    3、对方是字符串,直接比较;

    4、其他返回false

    四、如果是数字,对方是对象,对象取valueOf进行比较, 其他一律返回false

    五、null, undefined不会进行类型转换, 但它们俩相等

    上面的转换顺序一定要牢记,面试的时候,经常会出现类型的问题。

    举例巩固提高

    下面我们一起来做做下面的题目吧!

    var a;
    console.dir(0 == false);//true
    
    console.dir(1 == true);//true
    
    
    console.dir(2 == {valueOf: function(){return 2}});//true
    
    
    console.dir(a == NaN);//false
    console.dir(NaN == NaN);//false
    
     console.dir(8 == undefined);//false
    
    console.dir(1 == undefined);//false
    
     console.dir(2 == {toString: function(){return 2}});//true
    
     console.dir(undefined == null);//true
    
     console.dir(null == 1);//false
    
      console.dir({ toString:function(){ return 1 } , valueOf:function(){ return [] }} == 1);//true
    
      console.dir(1=="1");//true
      console.dir(1==="1");//false

    做完上面的题目,不知道你对js隐性类型转换步骤了解了不?

  • 相关阅读:
    python 编码格式
    mysql 允许特定IP访问
    mysql “Too many connections” 解决办法
    python 微信支付
    python RSA 加密与签名
    给列表里添加字典时被最后一个覆盖
    设置MySQL允许外网访问
    Python中print/format字符串格式化实例
    ssh 将22端口换为其它 防火墙设置
    linux ubuntu nethogs安装与介绍
  • 原文地址:https://www.cnblogs.com/Midaoi/p/5121416.html
Copyright © 2011-2022 走看看