JS中的数据类型检测
JavaScript的数据类型分为两类:原始类型(primitive type)和对象类型(object type)。原始类型有5种,分别是:数字(Number)、字符串(String)、布尔值(Boolean)、null和undefined。引用类 型包括:Object、Array、Date、Error、RegExp、Functions。
1.原始类型的检测
对于原始类型中的:数字、字符串、布尔值、undefined,最佳检测方法是使用typeof运算符,typeof检测后会返回一个表示值类型的字符串。typeof的语法是:
typeof <变量>
typeof(<变量>)
对以上4种原始值使用typeof运算符后:数字返回"number" 、 字符串返回"string" 、 布尔值返回"boolean" 、 undefined返回"undefined"。如下:
var str = 'a string';
console.log(typeof str); // -> string var num = 0;
console.log(typeof num); // -> string var bool = true;
console.log(typeof bool); // -> boolean var undefin;
console.log(typeof undefin); // -> undefined 未定义的变量和未初始化的变量都将返回undefined
原始值null,应当使用===和!==进行行比较,对null做typeof检测时,返回值为"object"。
2.引用类型的检测
引用值也称做对象(object),在JavaScript中除原始类型外都是引用类型。typeof运算符不能有效检测引用类型,所有引用类型都会返回“object”:
console.log(typeof []); // -> object
console.log(typeof new Date()); // -> object
console.log(typeof {}); // -> object
console.log(typeof new Error()); // -> object
console.log(typeof new RegExp()); // -> object
console.log(typeof Function); // -> function
引用类型检测推荐使用instanceof运算符,基本语法是:
value instanceof constructor
对引用类型做instanceof操作:
console.log([] instanceof Array); // -> true
console.log(new Date() instanceof Date); // -> true
console.log({} instanceof Object); // -> true
console.log(new Error() instanceof Error); // -> true
var reg = new RegExp();
console.log(reg instanceof RegExp); // -> true
function func (argument) {
}
console.log(func instanceof Function) // -> true
对于函数做typeof操作返回function,相比intanceof更推荐使用typeof。
对象属性检测
判断属性是否存在最好的方法是使用in运算符,如果属性存在或继承自原型链,都会返回true。如果只想检测对象实例是否存在某属性,而不是从原型继承的属性则使用hasOwnProperty()方法。如下:
function Person() {
this.name = 'liuht';
this.doSometing = function() {
return 'doSometing';
}
}
Person.prototype.area = 'China'; // 原型链属性
var man = new Person();
man.sex = 'man';
console.log('name' in man); // -> true
console.log('sex' in man); // -> true
console.log('area' in man); // -> true
console.log(man.hasOwnProperty('name')); // -> true
console.log(man.hasOwnProperty('sex')); // -> true
console.log(man.hasOwnProperty('area')); // -> false
JS中的数据类型检测的更多相关文章
- js中判断数据类型的四种方法总结
js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...
- js中的数据类型
JS中的数据类型: ——数字 (number)NaN ——字符串(string) ——布尔 (boolean)——函数 (function) 也是对象的一种 ——对象 (object) ...
- 如何判断js中的数据类型?
js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...
- 如何判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- [转]如何判断js中的数据类型
原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...
- 浅谈js中的数据类型,使用typeof获取js数据类型
JS中的数据类型 1):Undefined——值未定义 注:Undefined类型只有一个值,即特色的undefined.在使用var声明变量但未对其加以初始化时,这个变量的值就是undefined ...
- 如何判断js中的数据类型(转)
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- 判断js中的数据类型
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- js中的数据类型及其转换
Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...
随机推荐
- myeclipse10 优化设置
http://it.oyksoft.com/post/5898/ 一.Myeclipse10修改字体MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉 ...
- 什么是js和js的基本语法
时间:2016年12月15日 先讲讲基础语法: 大部分是来操作表单: js动态效果和数据交互(ajax?) js也有自己的API js大部分的DOM操作都是针对input的. 案例学习,对注册页面的简 ...
- 解决qt程序运行时的cannot create Qt for Embedded Linux data directory: /tmp/qtembedded-0出错情形
1.mkdir /tmp 2.挂载 mount -t tmpfs -o size=32m none /tmp
- Cocos2d-x lua 游戏中的文字和标签
游戏场景中的文字分为静态文字和动态文字,在图片中的文字为静态文字,不能通过程序访问,而且无法动态修改内容,但是表现力丰富.动态文字一般需要通过程序访问,需要动态修改内容可以通过标签(Label or ...
- LTE Manual ——Logging(翻译)
LTE Manual ——Logging(翻译) (本文为个人学习笔记,如有不当的地方,欢迎指正!) 9 Logging ns-3 日志功能可以用于监测或调试仿真程序的进展.日志输出可以通过 ma ...
- App开发(Android与php接口)之:短信验证码
最近和同学们一起开发一个自主项目,要用到短信验证码,在网上搜索了很久,看到一个推荐贴,提到了很多不错的短信服务商.经过测试,帖子中提到的服务商他们的短信到达率和到达速度也都不错.最后,由于经费问题,我 ...
- python 04
面向对象 __init__ 方法在类的一个对象被建立时, 马上运行. 这个方法可以用来对你的对象做一些你希望的初始化. python中所有的类成员(包括数据成员)都是公共的, 所有的方法都是有效的. ...
- php使用PDO连接mysql数据库
<?php $dsn='mysql:host=localhost;dbname=mssc'; $user='root'; $password=''; $status=1; try { $sql= ...
- 关于xml配置实现AOP的小知识
除了前面介绍的基于JDK1.5的注解方式来定义切面,切入点和增强处理外,Spring AOP也允许直接使用XML配置文件来管理它们.在JDK1.5之前,只能使用配置文件的方式来管理,在Spring2. ...
- web.xml listener和event
Listener接口 Event类 ServletContextListener ServletContextEvent ServletContextAttributeListener Servlet ...