首页 技术 正文
技术 2022年11月15日
0 收藏 853 点赞 3,845 浏览 2875 个字

<一> 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.prototypeconsole.log(Function instanceof Function); //true
//第一个Function的原型链:Function=>Function.__proto__ => Function.prototype
//第二个Function的原型:Function=>Function.prototypeconsole.log(Function instanceof Object); //true
//Function=>
//Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//Object => Object.prototypeconsole.log(Person instanceof Function); //true
//Person=>Person.__proto__=>Function.prototype
//Function=>Function.prototypeconsole.log(String instanceof String); //false
//第一个String的原型链:String=>
//String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个String的原型链:String=>String.prototypeconsole.log(Boolean instanceof Boolean); //false
//第一个Boolean的原型链:Boolean=>
//Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Boolean的原型链:Boolean=>Boolean.prototypeconsole.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主要检测的是引用类型!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,959
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,483
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,112
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,778