/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceof: 1.左操作数是一个对象,右操作数是标识对象的类,如果左侧的对象是右侧类的实例,则表达式返回true,否则返回false 2.如果左边的操作数不是对象,返回false 3.如果右边的操作数不是函数,抛出类型错误异常 4.在计算obj instanceof f 时, 会先计算f.prototype…
数据类型 js 基本类型包括:Undefined  symbol null string boolean number js 引用类型包括:object array Date RegExp typeof 我们一般用typeof来判断数据的类型的 接下来我们试试 console.log(typeof undefined) //undefined console.log(typeof Undefined) //undefined console.log(typeof Null) //undefine…
Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof  得到某个对象的类型 差别: 举个样例: var arr = new Array(); typeof(arr); //object Object.prototype.toString.call(arr); //[Object Array] 推断某个对象值属于哪种内置类型 Object.prototype.toString ECMAS…
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true // "boolean" typeof {} // "object" typeof [] // "object" typeof function(){} // "function" typeof undefined // &quo…
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.log(typeof true); //boolean 4 console.log(typeof null); //object 5 console.log(typeof undefined); //undefined 6 console.log(typeof []); //object 7 console.lo…
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.prototype.toString无论基本数据类型还是引用类型返回其对应类型. 对应测试结果如下:   typeof test instanceof Object.prototype.toString.call(test) var test = 'xuriliang'; string test instan…
本文由segementfalt上的一道instanceof题引出: var str = new String("hello world"); console.log(str instanceof String);//true console.log(String instanceof Function);//true console.log(str instanceof Function);//false 先抛开这道题,我们都知道在JS中typeof可以检测变量的基本数据类型. let…
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同数据类型的Object.prototype.toString方法返回值如下. 数值:返回[object Number]. 字符串:返回[object String]. 布尔值:返回[object Boolean]. undefined:返回[object Undefined]. null:返回[ob…
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型.但当除了 Object 类型的对象外,其他类型直接使用 toString 方法时,会直接返回都是内容的字符串,所以我们需要使用call或者apply方法来改变toString方法的执行上下文. const an = ['Hello','An'];an.toS…
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype.toString.call 使用,可以区分7种 console.log(Object.prototype.toString.call(123)) //[object Number]console.log(Object.prototype.toString.call('123')) //[objec…