javascript中 typeof和instanceof的区别
<一> js中typeof的用法进行了详细的汇总介绍
(1)返回一个变量的基本类型
回顾基本类型(number,string,boolean,null,undefined,object)
console.log(typeof 1); // number
console.log(typeof 'abc'); // string
console.log(typeof true); // boolean
let o = {a:'23'};
console.log(typeof o); // object
console.log(typeof null); // object
console.log(typeof undefined); // undefined
备注1: null表示 “没有对象”,即该处不应该有值;undefined表示“缺少值”,就是此处应该有值,但是没有定义。
备注2:判断一个值既不是null,也不是undefeated,
问题:如果你想检测一个值是否被定义过(值不是undefined也不是null),那么你就遇到了typeof最有名的一个怪异表现(被认为是一个bug):typeof null返回了"object":
function isDefined(x) {
return x!== null && x!== undefined;
}
备注2:typeof null 是object,是不是bug,从另一个角度看,如果null是一个真正意义上对象的话,它应该放在与array,function同级别的位置,而不是跟undefined,string同级别,但是typeof null返回object,矛盾。
链接:http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html
(2)typeof 检查一个变量是否存在,是否有值
if(typeof o === 'object'){
console.log("o是一个对象")
}else {
console.log("o不是一个对象")
}
备注:同理,别的基本类型也可以采用相同的办法。
<二> js中的instanceof运算符
主要功能:检测应用类型,是数组,正则等
(1)instanceof的普通用法, obj instanceof Object 检测Object.prototype是否存在于参数obj的原型链上
Person的原型在p原型链中
function Person(){};
var p =new Person();
console.log(p instanceof Person);//true
(2)继承中判断实例是否属于它的父类
Student和Person都在s的原型链中
function Person(){};
function Student(){};
var p =new Person();
Student.prototype=p;//继承原型
var s=new Student();
console.log(s instanceof Student);//true
console.log(s instanceof Person);//true
备注:instanceof只能用来判断对象和函数,不能用来判断字符串和数字
(3)复杂用法
function Person() {}
console.log(Object instanceof Object); //true
//第一个Object的原型链:Object=>
//Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Object的原型:Object=> Object.prototype
console.log(Function instanceof Function); //true
//第一个Function的原型链:Function=>Function.__proto__ => Function.prototype
//第二个Function的原型:Function=>Function.prototype
console.log(Function instanceof Object); //true
//Function=>
//Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//Object => Object.prototype
console.log(Person instanceof Function); //true
//Person=>Person.__proto__=>Function.prototype
//Function=>Function.prototype
console.log(String instanceof String); //false
//第一个String的原型链:String=>
//String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个String的原型链:String=>String.prototype
console.log(Boolean instanceof Boolean); //false
//第一个Boolean的原型链:Boolean=>
//Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Boolean的原型链:Boolean=>Boolean.prototype
console.log(Person instanceof Person); //false
//第一个Person的原型链:Person=>
//Person.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Person的原型链:Person=>Person.prototype
(4)总结
function _instanceof(A, B) {
var O = B.prototype;// 取B的显示原型
A = A.__proto__;// 取A的隐式原型
while (true) {
//Object.prototype.__proto__ === null
if (A === null)
return false;
if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true
return true;
A = A.__proto__;
}
}
<三> typeof 与instanceof区别
typeof和instanceof的目的都是检测变量的类型,两个的区别在于typeof一般是检测的是基本数据类型,instanceof主要检测的是引用类型!
javascript中 typeof和instanceof的区别的更多相关文章
- javascript中typeof与instanceof的区别
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...
- javascript 中 typeof 和 instanceof 的区别
在 javascript 中经常会用到 typeof 和 instanceof 来判断一个对象的类型,可能 typeof 用得多些,那来看看这两个之间的区别吧. typeof : typeof 是一个 ...
- 浅谈JavaScript中typeof与instanceof的区别
首先,我们从其常规定义入手: instanceof 运算符可以用来判断某个构造函数的 prototype 属性是否存在另外一个要检测对象的原型链上.(需要注意的一点是:prototyp ...
- JavaScript中typeof,instanceof,hasOwnProperty,in的用法和区别
一. typeof操作符 typeof操作符用于返回正在使用值的类型. // 使用原始值 let mNull = null; let mUndefined = undefined; let mStri ...
- JS中typeof与instanceof的区别
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: Typeof typeof 是一个一元运算,放在一个运算数之前 ...
- JS中typeof与instanceof的区别 (2010-05-26 10:47:40
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...
- javascript中typeof和instanceof用法的总结
今天在看相应的javascript书籍时,遇到了typeof和instanceof的问题,一直不太懂,特地查资料总结如下: JavaScript 中 typeof 和 instanceof 常用来判断 ...
- JavaScript 中typeof、instanceof 与 constructor 的区别?
typeof.instanceof 与 constructor 详解 typeof 一元运算符 返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,st ...
- js中 typeof 和 instanceof 的区别
typeof 和 instanceof 都能判断数据类型,但是它们之间有什么区别呢,浅谈如下 typeof 用于判断数据类型,返回值为以下6种类型 1.string 2.boolean 3.numbe ...
随机推荐
- iOS - 截取数组前几个元素放入新的数组,剩余的放入另外一个数组
NSArray *array = [NSArray arrayWithObjects:@"Crystal",@"Maisie",@"Lukas&quo ...
- LeetCode_463. Island Perimeter
463. Island Perimeter Easy You are given a map in form of a two-dimensional integer grid where 1 rep ...
- [PHP] 浅谈 Laravel Authentication 的 auth:api
auth:api 在 Laravel 的 Routing , Middleware , API Authentication 主题中都有出现. 一. 在 Routing 部分可以知道 auth:api ...
- 第1部分 Elasticsearch基础
一.安装 es端口:9200 kibana端口:5601 brew install elasticsearch brew install elasticsearch brew services sta ...
- jdk安装配置(不适合新人,个人随笔)
jdk下载:https://www.oracle.com/technetwork/java/javase/downloads/index.html 按个人喜好下载相应版本,一般比最新的低一个版本 下载 ...
- canvas实现饼状图
效果图如下: html: <canvas id="myCanvas" width="500" height="500">< ...
- MySQL ALTER TABLE语法
先看一下定义(密密麻麻) ALTER TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_optio ...
- Forbidden (CSRF token missing or incorrect.):
CSRF令牌失效或丢失,Ajax请求页面报错(403 Forbidden ) csrftoken存在 页面响应为CSRF验证失败请求被中断,经过测试,该错误并非是没有在表单中加入{% csrf_tok ...
- day56——http协议、MVC和MTV框架模式、django下载安装、url路由分发
day56 昨日复习 今日内容 HTTP协议 网页:https://www.cnblogs.com/clschao/articles/9230431.html 老师整理的重点 老师整理的重点 请求信息 ...
- C++ Primer第五版(中文带书签)
本想发github的(链接更稳定),但是大小超出限制了. 本文件为扫描件,还是在我找了大半天之后的结果.能找到的免费的貌似都是扫描件,在看了一百多页之后(我不喜欢文字不能选中的感觉),我果断买了纸质书 ...