如何判断js的变量的数据类型
文章首发: http://www.cnblogs.com/sprying/p/4349426.html
本文罗列了一般的Js中类型检测的方法,实际上是每个新手在构建Js知识体系时,都要知晓的,而我只是翻出刚学Js时的笔记。
一、Js中有5种基本数据类型
二、类型判断
|
1
2
3
4
5
6
|
var obtainType = function(o){ var t; if(o === null ) return “null”; else if(o !== o) return “NaN”; else if( (t = typeof o) !== ‘object’) return t;} |
|
1
2
3
4
5
6
7
8
9
10
|
function obtainType(type) { return function (obj) { return Object.prototype.toString.call(obj) === "[object " + type + "]" }}var isObject = isType("Object")var isString = isType("String")var isArray = Array.isArray || isType("Array")var isFunction = isType("Function") |
|
1
2
3
4
5
6
7
|
/** * 返回函数的名字,可能为空串;不是函数,返回null */Function.prototype.getName = function () { if ("name" in this) return this.name; return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];}; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/** * 返回:null NaN undefined string number boolean * function Array String Object(包括一些自定义类型) 自定义类型 */var obtainType =function(o){ /** * 获取参数类型 * 对象直接量、Object.create、自定义构造函数的类属性皆为Object; * 识别出原生类型 (内置构造函数和宿主对象) */ function classOf(obj){ return Object.prototype.toString.call(obj).slice(8, -1); } /** * 返回函数的名字,可能为空串;不是函数,返回null */ Function.prototype.getName = function () { if ("name" in this) return this.name; return this.name = this.toString().match(/function\s*([^(]*)\(/)[1]; }; var t, c, n; // 处理null值特殊情形 if (o === null) return "null"; // NaN:和自身值不相等 if (o !== o) return "NaN"; // 识别出原生值类型和函数、undefined if ((t = typeof o) !== "object") return t; // 识别出原生类型 if ((c = classOf(o)) !== "Object") return c; // 返回自定义类型构造函数名字 if (o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n; return "Object";}; |
5.
|
1
2
3
4
5
|
var strObj = new String('abc');typeof strObj // "object"obtainType(strObj) // "String" |
三、 其它
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// badif (name !== '') { // ...stuff...}// goodif (name) { // ...stuff...}// badif (collection.length > 0) { // ...stuff...}// goodif (collection.length) { // ...stuff...} |
如何判断js的变量的数据类型的更多相关文章
- JS 01 变量_数据类型_分支循环_数组
点击直通车↓↓↓ 数据类型及数据类型的手动转换 数组 一.概念 JavaScript(JS)是一种基于对象和事件驱动.且可以与HTML标记语言混合使用的脚本语言,其编写的程序可以直接在浏览器中解释执 ...
- js之变量与数据类型
变量 声明 一个变量被重新复赋值后,它原有的值就会被覆盖,变量值将以最后一次赋的值为准. var age = 18; age = 81; // 最后的结果就是81因为18 被覆盖掉了 同时声明多个变量 ...
- 如何判断js中的数据类型?
js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...
- javascript重修之书(一):如何判断变量的数据类型
javascript重修之书(一):如何判断变量的数据类型 一:检测值类型 基本类型:(Undefined.Null.Boolean.Number和String) javascript之所以被称为一门 ...
- 判断JS数据类型的四种方法
在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefined.N ...
- 判断JS的数据类型
typeof.instanceof. constructor. prototype方法比较 (摘自如何判断JS中的数据类型) 1. 使用typeof操作符. 对一个值使用 typeof 操作符可能返回 ...
- 判断JS数据类型的几种方法
原文转自http://www.cnblogs.com/onepixel/p/5126046.html! 说到数据类型,我们先说一下JavaScript 中常见的几种数据类型: 基本类型:string, ...
- 正确判断js数据类型 总结记录
正确判断js数据类型 总结记录 判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. 三方库. js六大数据类型 number: 数 ...
- js课程 1-5 js如何测试变量的数据类型
js课程 1-5 js如何测试变量的数据类型 一.总结 一句话总结:用typeof()方法. 1.js如何判断变量的数据类型? 用typeof()方法. 13 v=10; 14 15 if(typeo ...
随机推荐
- 字符串系列——KMP模板整理
KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...
- python lambda简易使用
基本格式 lambda 变量名:函数表达式 ①直接使用 f=lambda x:x**2 f(3) ②设置函数列表 l=[lambda x:x**2, lambda x:x**3, lambda x:x ...
- freopen的各种错误姿势
本弱鸡已经触发的错误姿势: freoprn("railway.in","r",stdin); freopen("railway.cpp",& ...
- buf.writeDoubleBE()函数详解
buf.writeDoubleBE(value, offset[, noAssert]) buf.writeDoubleLE(value, offset[, noAssert]) value {Num ...
- Centos下Yum安装PHP5.5,5.6,7.0及扩展
默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 ...
- 洛谷 1501 [国家集训队]Tree II BZOJ 2631 Tree
[题解] 维护乘法标记和加法标记的LCT #include<cstdio> #include<algorithm> #define Mod (51061) #define N ...
- 武大OJ 612. Catch the sheep
Description Old Sama is a great and powerful magician in the word. One day, a little girl, Anny, tou ...
- 几道splay
hdu 1890 题意:每次将第i位到第i小数字所在的位置之间的位置翻转,每次输出第i小数字所在的位置 分析: 简单的splay处理区间翻转问题 有三点需要注意: 1.区间是1~n+2 2.此题里的查 ...
- jq仿ps颜色拾取功能-js颜色拾取
1.效果展示 2.html代码:index.html <!DOCTYPE html> <html lang="en"> <head> <m ...
- [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component
Components with slots can expose their data by passing it into the slot and exposing the data using ...