js基础-1. 数据类型

190 阅读3分钟

1. 数据类型

1.1 数据类型种类

  1. 原始类型: 字符串,数字型,布尔型, undefined, null,Symbol(es6)

  2. 对象类型: 对象、数组、函数

<JavaScript权威指南>


//new出来的,typeof 均为object

Number(); // 0, 数字0

new Number(); // Number {0} ,__proto__: Number

//Symbol: 无字面形式

1.2 数据类型检测

1.2.1 typeof
  1. 一元运算符,优先级较高,后面()可写可不写

  2. 返回值:

  3. 原始数据类型:number, string, boolean, undefined, symbol(es6),

  4. 引用数据类型:object, function, 【注意:无数组】

  5. 缺点:

  6. null 识别为 object

  7. NaN 识别为 number

  8. new出来的,都是object

  9. 没有单独的数组类型,为object


let firstName = Symbol();

typeof firstName; // 'symbol'

typeof NaN; //number

typeof null; //object

typeof []; //object

//new 出来的都是object

typeof new Boolean(); //object

typeof new String(); //object

typeof new Number(); //object

typeof false; //boolean

typeof Boolean(); //boolean

typeof Number(); //number

typeof Array(); //object,typeof无array类型

//没有单独的数组类型

typeof Array(); //object

1.2.2 instanceof

运算符,用于检测构造函数的prototype是否出现在检测对象的原型链上


[] instanceof Array; //true

new Array(1,2,3) instanceof Array

{} instanceof Object; //true

true instanceof Boolean; //false

new Boolean(true) instanceof Boolean; //true

function C(){}

var d = new C()

d instanceof C; //true

1.2.3 检测数组

Array.isArray() ES5


let arr = [];

Array.isArray(arr); //true

//类数组

function fn(arguments){

  Array.isArray(arguments); //false

}

fn();

Array .isArray ()优于 instanceof,因为可以检测iframes传入的数组


var iframe = document.createElement('iframe');

document.body.appendChild(iframe);

document.body.appendChild(iframe);

var xArray = window.frames[1].Array;

var arr = new xArray(1,2,3); //[1,2,3]

arr instanceof Array //false

Array.isArray(arr) //true

Array.isArray 不存在时,使用如下方法创建


if(!Array.isArray){

  Array.isArray = function(obj){

    return Object.prototype.toString.call(obj) === '[Object Array]'

  }

}

1.2.4 Object.prototype.toString.call(arg) 类识别

返回值:[object + 类型(首字母大写)]

  1. 原始数据类型:String, Number, Boolean, Undefined, Null, Symbol*(es6)*

  2. 引用数据类型:Function, Array, Object, RegExp, Window, Set*(es6), Map(es6)*


/*原始数据类型*/

//字符串

{

  Object.prototype.toString.call('dd'); //"[object String]"

  Object.prototype.toString.call(new String('dd')); //"[object String]"

}

//数字型

{

  Object.prototype.toString.call(3); //"[object Number]"

  Object.prototype.toString.call(new Number(3)); //"[object Number]"

  Object.prototype.toString.call(NaN); //"[object Number]"

}

//布尔型

{

  Object.prototype.toString.call(true); //"[object Boolean]"

  Object.prototype.toString.call(new Boolean(2)); //"[object Boolean]"

}

//undefined

{

  Object.prototype.toString.call(undefined); //"[object Undefined]"

}

//null

{

  Object.prototype.toString.call(null); //"[object Null]"

}

//symbol

{

  Object.prototype.toString.call(Symbol()); //"[object Symbol]"

}

/*引用数据类型*/

//函数

{

  Object.prototype.toString.call(function(){}); //"[object Function]"

  Object.prototype.toString.call(new Function()); //"[object Function]"

}

//数组

{

  Object.prototype.toString.call(new Array()); //"[object Array]"

  Object.prototype.toString.call([]); //"[object Array]"

}

//对象

{

  Object.prototype.toString.call({}); //"[object Object]"

  Object.prototype.toString.call(new Object()); //"[object Object]"

}

//set

{

  Object.prototype.toString.call(new Set()); // "[object Set]"

}

//map

{

  Object.prototype.toString.call(new Map()); //"[object Map]"

}

1.2.5 数字检测,排除NaN影响
  • 利用只有NaN不等于自身,排除NaN

  • es6中 Object.is(),可弥补有些浏览器下,全等缺陷

  • isNaN 检测是否为非数字。判断Number()的结果是否为NaN

  • Number.isNaN() [es6]检测是否为NaN。 isNaN增强版


//利用只有NaN不等于自身,排除NaN

{

  if(typeof arg === 'number' && arg === arg){}

}

//es6中 Object.is(),可弥补有些浏览器下,全等缺陷

{

  +0 === -0; //某些浏览器下,false

  NaN === NaN; //false

  Object.is(+0, -0); //true

  Object.is(NaN, NaN); //true

}

//ES5中,isNaN() 函数检测是否为非数字。

//等价于:检测Number()结果是否为NaN

{

  isNaN(1); //false

  isNaN('1'); //false,会进行数据类型转换

  isNaN(''); // false , Number('') === 0

  isNaN(null); //false,Number(null) === 0

  isNaN(false); //false,Number(false) === 0

  isNaN(NaN); //true

  isNaN(undefined); //true,Number(undefined) => NaN

  isNaN('a'); //true,Number('a') => NaN

  isNaN({}); //true

  isNaN(function(){}); //true

}

//Number.isNaN() [es6]检测是否为NaN, isNaN增强版

{

  Number.isNaN(NaN); //true

  //其他都是false

  Number.isNaN(true);  //false

  Number.isNaN(undefined) //false

  Number.isNaN('') //false

}

Number.isNaN() 的 polyfill

  • 利用isNaN为true时,只有NaN的typeof 结果为number

  • 利用只有 NaN 不跟自己相等的特性


//在isNaN() 的基础上增加了一个 type 的判断

{

  if(!Number.isNaN){

    Number.isNaN = function(n){

      return typeof n === 'number' && window.isNaN(n);

    }

  }

}

//只有 NaN 不跟自己相等的特性。

{

  if(!Number.isNaN){

    Number.isNaN = function(n){

      return n !== n;

    }

  }

}