js 数据类型转换与判断

553 阅读4分钟

常用的变量类型

有11种

Number,String,Boolean,Object,Array,

Json,Function,undefined,Null,Date,

RegExp,Error

数据类型

值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。

引用数据类型:对象(Object)、数组(Array)、函数(Function)。

注:Symbol 是 ES6 引入了一种新的原始数据类型,表示独一无二的值。 七大类型:包括基本类型和object混合类型

判断数据类型方法

*** 1.typeof ***

typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。

typeof 返回结果:string,number,boolean,object,undefined,function,symbol(其余类型均为Object) typeof返回的类型都是字符串形式,全部小写

typeof ''; // string 有效
typeof 1; // number 有效 
typeof true; //boolean 有效
typeof undefined; //undefined 有效
typeof Symbol(); // symbol 有效
typeof null; //object 无效
typeof [] ; //object 无效
typeof new Function(); // function 有效
typeof new Date(); //object 无效
typeof new RegExp(); //object 无效

//
typeof 1 === 'number' //true

对于基本类型,除 null 以外,均可以返回正确的结果。 对于引用类型,除 function 以外,一律返回 object 类型。 对于 null ,返回 object 类型。 对于 function 返回 function 类型。

其中,null 有属于自己的数据类型 Null , 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型,没有错,但不是我们想要的结果。

*** typeof 能验证*** string 字符串 number 数字 boolean 布尔 undefined Symbol function

*** 2.instanceof ***

instanceof 是用来判断 A 是否为 B 的实例对,表达式为:A instanceof B,如果A是B的实例,则返回true,否则返回false。

instanceof检测的是原型,

[] instanceof Array; //true
[] instanceof Object; //true
{} instanceof Object;//true
new Date() instanceof Date;//true
new Date() instanceof Object;//true
'd' instanceof String;//false
String('d') instanceof String//false

instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。 Array.isArray() //ES5新增,判断是否是数组

*** 3.constructor ***

alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true

注意: constructor 在类继承时会出错 null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。 函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的constructor 引用会丢失,constructor 会默认为 Object

*** 4.prototype.toString() ***

toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型,更严格的讲,是 toString运行时this指向的对象类型, 返回的类型格式为[object,xxx],xxx是具体的数据类型,其中包括:

String,Number,Boolean,Undefined,Null,Function,Date,Array,RegExp,Error,HTMLDocument,... 基本上所有对象的类型都可以通过这个方法获取到。

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]

*** 5.jquery.type() ***

jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"

数据类型转换

JS是一种弱类型语言,变量没有类型限制,可以随意赋值。

虽然变量没有类型限制,但运算符要求进行运算的变量的类型和运算符期望的类型一致,所以,当不同类型的变量进行加减运算时,就会进行类型转换(如,1+'a'、'3'-'1')

强制(显性)转换

*** 转换为字符串***

console.log(String(123));//"123"
console.log(String(true));//"true"
console.log(String(false));//"false"
console.log(String(null));//"null"
console.log(String(undefined));//"undefined"
console.log(String(new Date()))//Thu Nov 01 2018 14:24:09 GMT+0800 (中国标准时间)
console.log(String(function(){}))//function(){}

*** 转换为数字***


console.log(Number(123));//123
console.log(Number('123'));//123
console.log(Number('123a'));//NaN
console.log(Number(''));//0
console.log(Number(true));//1
console.log(Number(false));//0
console.log(Number(undefined));//NaN
console.log(Number(null));//0
console.log(Number(' \t\n 3.23322\t '));//Number可以自动去掉两头空白符,输出3.23322
console.log(Number(function(){}))//NaN
console.log(Number(new Date()))//1541053632320 一串数字

Number构造函数将字符串转换为数字时,字符串必须完全被为数字,即只要有一个字符无法转为数值, 则整个字符串都会被转为NaN。如果想不精确转换,可以使用parseInt()函数, parseInt()函数将最大限度的将字符串转换为数字。如:


console.log(parseInt('123abc456'));//123
console.log(Number('123abc456'));//NaN

*** 转换为布尔***

使用Boolean构造函数转换其他类型的数值时,除了下面几种情况返回false外,其他的值都返回true。 1、null

2、0、-0或者+0

3、NaN

4、'' 空字符串

5、undefined

console.log(Boolean(null));//false
console.log(Boolean(0));//false
console.log(Boolean(-0));//false
console.log(Boolean(+0));//false
console.log(Boolean(''));//false
console.log(Boolean(NaN));//false
console.log(Boolean(undefined));//false
console.log(Boolean({}));//true
console.log(Boolean([]));//true
console.log(Boolean("蝈蝈"));//true
console.log(Boolean(new Boolean(false)));//true

自动(隐性)转换

自动类型转换就是不需要人为强制的进行转换,js会自动将类型转换为需要的类型,所以该转换操作用户是感觉不到的,因此又称为隐性类型转换。自动类型转换实际上和强制类型转换一样,也是通过String()、Number()、Boolean()等构造函数进行转换,只是该操作是JS自己自动完成的而已。