数据类型判断,及NaN,null,undefined的区别应用

146 阅读3分钟

js基本类型

  • boolean
  • number
  • string
  • undefined
  • null

内置对象

  • bigint
  • symbol

js复杂类型

  • object
  • function
  • array

类型判断方法

1.  typeof // typeof arr // object
1.  instanceof // arr instanceof Array true
1.  Object.prototype.toString.call(var) === '[object Array]' //true
1.  isArray // Array.prototype.isPrototypeOf(arr) //true

简单变量类型判断 typeof

var a = 1; typeof a; // 'number'
var b = 'a'; typeof b; // 'string'
var c = true; typeof c: // 'boolean'
var d = null; typeof d; // 'object'
var e = undefined; typeof e; // 'undefined'
var aa = {}; typeof aa;// 'object'
var bb = []; typeof bb; // 'object'
var cc = function(){}; typeof cc; // 'function'

简单变量类型判断 instanceof

  • var a = new Array();
  • var b = new Object();
  • function cc() { console.log('hello'); }
  • a instanceof Array // true
  • b instanceof Object // true
  • cc instanceof Object // true

instanceof 原理 ** 有机会再写??? **

简单变量类型判断 Object.prototype.toString.call

var a = 1; // Object.prototype.toString.call(a); // '[object Number]'
var b = 'a'; // Object.prototype.toString.call(b); // '[object String]'
var c = true; // Object.prototype.toString.call(c); // '[object Boolean]'
var d = undefined; // Object.prototype.toString.call(d); // '[object Undefined]'
var e = null; // Object.prototype.toString.call(e); // '[object Null]' **为什么是Null**
var f = []; // Object.prototype.toString.call(f); // '[object Array]'
var g = {}; // Object.prototype.toString.call(g); // '[object Object]'
var h = function(){}; // Object.prototype.toString.call(h); // '[object Function]'

检查数据类型的方式

基本类型:string,number,boolean
特殊类型:undefined,null
引用类型:Object,Function,Function,Array,Date,...

typeof 可以准确判断基本类型但是当其对引用类型进行检测时,会返回object 这样就无法进行精准的判断,这样也不足为奇,因为所有的对象其原型链最终都指向object

typeof 1; //'number'
typeof ''; //'string'
typeof true;//'boolean'
typeof undefined;//'undefined'
function param(){console.log('param');}
typeof param;// ‘function’
typeof new Function(); // 'function'
typeof null; //'object' 这就无法区分null和object
typeof [];//'object'这就无法区分数组和object
typeof {};//'object'这就无法区分其它应用类型和object

instanceof 是用来判断 A 是否为 B 的实例对象,当 A 的 proto 指向 B 的 prototype 时,就认为A就是B的实例。instanceof只能用来检测两个对象是否在一条原型链上,并不能检测出对象的具体类型。

function Person(name){ console.log(name); }
var p = new Person('daren');
p instanceof Person; // true;
p instanceof Object; // true;
null instanceof Object; // false;

Object.prototype.toString 这种方式来区分数组和对象及其它引用类型 Array.isArray() 也可以用这个方法判断数组

var b = {}
Object.prototype.toString.call(b) === "[object Object]";
var bb = [];
Object.prototype.toString.call(b) === "[object Array]";

Object.is()

let a = null;
Object.is(a, null);// true;
let aa = undefined;
Object.is(aa, undefined); // true;
let aaa = NaN;
Object.is(aaa, NaN); // true;


Object.is('foo', 'foo');     // true
Object.is(window, window);   // true

Object.is('foo', 'bar');     // false
Object.is([], []);           // false

var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo);         // true
Object.is(foo, bar);         // false

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

// 特例
Object.is(0, -0);            // false
Object.is(0, +0);            // true
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true

NaN,null,undefined

NaN

var x = 0/0;
(isNaN(x)) ===true;
typeof x ; // 'number'

undefined `undefined 的字面意思就是:未定义的值 。这个值的语义是,希望表示一个变量最原始的状态,而非人为操作的结果。

1、声明一个变量,但是没有赋值

2、访问对象上不存在的属性或者未定义的变量

3、函数定义了形参,但没有传递实参 `

null null 的字面意思是:空值。这个值的语义是,希望表示一个对象被人为的重置为空对象,而非一个变量最原始的状态。在内存里的表示就是,栈中的变量没有指向堆中的内存对象。

1、如果定义的变量在将来用于保存对象,那么最好将该变量初始化为null

2、当一个数据不再需要使用时,我们最好通过将其值设置为null来释放其引用