一、javascript 中有几种类型的值

  1.基本数据类型 : 包括 Undefined、Null、Boolean、Number、String、Symbol (ES6 新增,表示独一无二的值)

    特点: a. 值不可变

        b. 存放在栈中

        c.双等和全等的区分

  2.引用数据类型: 包括 Object、Array、Function

    特点: a.值可变

        b.同时保存再栈内存和堆内存

        c.比较是引用的比较

二、javascript 数据类型的检测

  1.typeof : 返回一个表示数据类型的字符串(number boolean string symbol object undefined function), 缺点: 不能判断 null array 时间对象 正则对象

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

  null 和 array 都会返回 object

  2. indestanceof :  判断 A 是否是 B 的实例 (A instanceof B),返回布尔值,便是一个对象在其原型链中是否存在一个构造函数的prototype属性。

    缺点: a.字面量创建和实例方式创建有区别,只有实例创建的结果才是标准的。

        

console.log(1 instanceof Number)//false
console.log(new Number(1) instanceof Number)//true

        b.只要再当前的实例原型链上,检测结果都为true

var arr = [1, 2, 3];
console.log(arr instanceof Array) // true
console.log(arr instanceof Object); // true
function fn(){}
console.log(fn instanceof Function)// true
console.log(fn instanceof Object)// true

       c.不能检测 null 和 undefined: 对于特殊的数据类型 null 和 undefined,他们的所属类是 Null 和 Undefined,但是浏览器把这两个类保护起来了,不允许我们在外面访问使用。

      

  使用instanceof检测 数组、对象、时间对象、正则对象

[] instanceof Array; //true
{} instanceof Object;//true
new Date() instanceof Date;//true
new RegExp() instanceof RegExp//true

  

  4.constructor: constructor 检测 Object 与 instanceof 不一样,还可以处理基本数据类型的检测。

var aa=[1,2];
console.log(aa.constructor===Array);//true
console.log(aa.constructor===RegExp);//false
console.log((1).constructor===Number);//true
var reg=/^$/;
console.log(reg.constructor===RegExp);//true
console.log(reg.constructor===Object);//false

    缺点: a. null 和 undefined 是无效的对象,因此是不会有 constructor 存在的

        b.函数可以把类的原型进行重写,所以检测不稳定

  5.Object.prototype.toString.call()  : Object.prototype.toString.call() 最准确最常用的方式。首先获取 Object 原型上的 toString 方法,让方法执行,让 toString 方法中的 this 指向第一个参数的值。

  

  Object对象和它的原型链上各自有一个toString()方法,第一个返回的是一个函数,第二个返回的是值类型。

  

  一般情况下,js中对象的toString(),返回字符串,内容与函数声明语法有关,例如[1,2,3].toString()//"1,2,3"

  大多数都返回函数的完整源码,Array.toString()//"function Array() { [native code] }"

  内置函数往往返回一个类似"[native code]"的函数体,需要配合call方法,比如Object.prototype.toString.call([1,2,3])//"[object Array]"

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]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window是全局对象global的引用

  

三、面试题: 封装一个函数,输入任意,输出他的类型

function type(target) {
var ret = typeof(target);
var template = {
"[object Array]": "array",
"[object Object]":"object",
"[object Number]":"number - object",
"[object Boolean]":"boolean - object",
"[object String]":'string-object'
} if(target === null) {
return 'null';
}else if(ret == "object"){
var str = Object.prototype.toString.call(target);
return template[str];
}else{
return ret;
}
}

  

参考资料:

  • 【文章】[ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用(推荐)

  • JS 判断数据类型的三种方法

  • JS 中的数据类型及判断

  • Javascript 判断变量类型的陷阱 与 正确的处理方式

  • 判断 JS 数据类型的四种方法

  • JavaScript 的数据类型及其检测

    

js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型的更多相关文章

  1. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  2. js 数据类型及检测

    js中基本数据类型有6种number.string.undefined.null.boolean,Symbol (ES6 新增,表示独一无二的值),还有一种数据类型为引用数据类型统称为Object对象 ...

  3. js数据类型检测小结

    在js中,有四种用于检测数据类型的方式,分别是: typeof 用来检测数据类型的运算符 instanceof 检测一个实例是否属于某个类 constructor 构造函数 Object.protot ...

  4. js 数据类型检测

    提起数据类型检测  大多数人首先想起的应该是  typeof 'xxx' == '数据类型' 坦然这种方法对于基本数据类型的检测还是非常方便的,但是当遇到引用数据类型 Object时却爱莫能助,下面就 ...

  5. JS数据类型和堆栈+变量比较和值的复制+参数传递和类型检测

    变量命名 变量名:字母 数字 下划线 美元符$ jquery:  $     $.each()   $ === jQuery underscore( js的一个函数库)  :   _     _.ea ...

  6. 总结的JS数据类型判定(非常全面)

    用typeof 来检测数据类型 Javascript自带两套类型:基本数据类型(undefined,string,null,boolean,function,object)和对象类型. 但是如果尝试用 ...

  7. JS数据类型的理解(猜测)

    Js 数据类型 对于这个主题,首先来看几个问题,如果你对这几个问题很清楚的话,那就请直接跳过吧,不用接着往下看了,如果不清楚,建议你还是看看. 1)如果判断函数?function 和object的联系 ...

  8. 判断JS数据类型的四种方法

    在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefined.N ...

  9. 1. js数据类型_对象_函数_内存

    1. js数据类型有哪些? 基本(值)类型 Number ---- 任意数值 String ---- 任意字符串 Boolean ---- true/false undefined ---- unde ...

随机推荐

  1. python基本数据类型,int,bool,str

    一丶python基本数据类型 1.int 整数,主要用来进行数学运算. 2.str 字符串,可以保存少量数据并进行相应的操作 3.bool 判断真假.True.False 4.list 存储大量数据, ...

  2. vscode 常用插件安装

    设置中文语言使用快捷键[Ctrl+Shift+P],弹出的搜索框中输入[configure language],然后选择搜索出来的[Configure Display Language],locale ...

  3. ORACLE 数据库的级联查询 一句sql搞定(部门多级)

    在ORACLE 数据库中有一种方法可以实现级联查询   select  *                //要查询的字段 from table              //具有子接点ID与父接点I ...

  4. linux 下 `dirname $0`(转)

    在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".".这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目 ...

  5. 【硬盘整理】使用UltimateDefrag将常用文件放置在磁盘最外圈

    使用方法未知.软件截图如下: 官方网站(英文):http://www.disktrix.com/ 汉化破解版V3.0下载地址:http://page2.dfpan.com/fs/7com9monca3 ...

  6. SAP云平台运行环境Cloud Foundry和Neo的区别

    SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...

  7. nginx搭建流媒体服务器

    1.安装PCRE库 到www.pcre.org 下载pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gz cd pcre-8.37 ./configure make ...

  8. IOS给图片增加水印(图片、文字)

    在网上发现很多人使用 CGContextDrawImage(context,CGRectMake(0,0,self.width,self.height),[image CGImage]); //原图  ...

  9. POJ-3080 Blue Jeans---字符串+暴力

    题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...

  10. bzoj2600 [Ioi2011]ricehub

    Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...