JS--数据类型与判断/数据类型的转换

Js--数据类型:

   分为:原始数据类型(基本数据类型),引用数据类型

原始数据类型:String    Number   Null  undefined   Boolean    Symbol.....

引用数据类型:Object Array   Function ......

基本数据类型和引用数据类型存储在内存中的位置不同:

  • 基本数据类型存储在栈中

  • 引用类型的对象存储于堆中

Js--数据类型的判断

var str = 'abc'
var num = 1
var fun = function () { console.log(1); }
var be = true

    var obj = { a: 0, b: 1 }
    var arr = [1, 2, 3]
    var nu = null
  1.typeof

    // console.log(typeof str); //string
    // console.log(typeof num); //number

    // console.log(typeof fun); //function
    // console.log(typeof be); //boolean

    // console.log(typeof obj); //object
    // console.log(typeof nu); //object
    // console.log(typeof arr); //object
 
    // 其中typeof返回的类型都是字符串形式,需注意,例如:
    // console.log(typeof str == 'string');//true
    // console.log(typeof num == "number");//true
  2.判断已知对象类型的方法: instanceof
// console.log(arr instanceof Array);//true
    // console.log(str instanceof String);//false
    // console.log(obj instanceof Object);//true
    // console.log(fun instanceof Function);//true
    // 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支

 3、根据对象的constructor判断: constructor(构造器/构造函数)

  // console.log(arr.constructor === Array);//true
    // console.log(str.constructor == String);//true
    // console.log(obj.constructor == Object);//true
    // console.log(num.constructor == Number);//true
    // console.log(fun.constructor == Function);//true
    // console.log(be.constructor == Boolean);//true
    // 注意: constructor 在类继承时会出错
    // function A() { };
    // function B() { };
    // A.prototype = new B(); //A继承自B
    // var aObj = new A();
    // console.log(aObj.constructor === B)//true;
    // console.log(aObj.constructor === A)//false;
    // 而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
    // console.log(aObj instanceof B);//true
    // aObj.constructor = A; //将自己的类赋值给对象的constructor属性
    // console.log(aObj.constructor === A)// true;
    // console.log(aObj.constructor === B) //false; //基类不会报true了;

4、通用但很繁琐的方法: prototype

  // console.log(Object.prototype.toString.call(arr) === '[object Array]')//true
    // console.log(Object.prototype.toString.call(str) === '[object String]')//true
    // console.log(Object.prototype.toString.call(num) === '[object Number]')//true
    // console.log(Object.prototype.toString.call(obj) === '[object Object]')//true
    // console.log(Object.prototype.toString.call(fun) === '[object Function]')//true
    // console.log(Object.prototype.toString.call(be) === '[object Boolean]')//true
    // console.log(Object.prototype.toString.call(nu) === '[object Null]')//true

常见的类型转换有:

  • 强制转换(显示转换)
  • 自动转换(隐式转换)

1.显示转换,即我们很清楚可以看到这里发生了类型的转变,常见的方法有:

  • Number()
  • parseInt()
  • String()
  • Boolean()

2.隐式转换

我们这里可以归纳为两种情况发生隐式转换的场景:

  • 比较运算(==!=><)、ifwhile需要布尔值地方
  • 算术运算(+-*/%

除了上面的场景,还要求运算符两边的操作数不是同一类型

自动转换成字符串

'5' + 1 // '51'
'5' + true // "5true"
'5' + false // "5false"
'5' + {} // "5[object Object]"
'5' + [] // "5"
'5' + function (){} // "5function (){}"
'5' + undefined // "5undefined"
'5' + null // "5null"

自动转换成数值

除了+有可能把运算子转为字符串,其他运算符都会把运算子自动转成数值

'5' - '2' // 3
'5' * '2' // 10
true - 1  // 0
false - 1 // -1
'1' - 1   // 0
'5' * []    // 0
false / '5' // 0
'abc' - 1   // NaN
null + 1 // 1
undefined + 1 // NaN

 

🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡🧡

posted @   长安·念  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示