JavaScript中类型检测
文章首发: http://www.cnblogs.com/sprying/p/4349426.html
本文罗列了一般Js类型检测的方法,是构建Js知识体系的一小块,这篇文章是我很早之前总结的。
一、Js中有5种基本数据类型
二、类型判断
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;
}
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")
/**
* 返回函数的名字,可能为空串;不是函数,返回null
*/
Function.prototype.getName = function () {
if ("name" in this) return this.name;
return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
};
/**
* 返回: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.
var strObj = new String('abc');
typeof strObj // "object"
obtainType(strObj) // "String"
三、 其它
// bad
if (name !== '') {
// ...stuff...
} // good
if (name) {
// ...stuff...
} // bad
if (collection.length > 0) {
// ...stuff...
} // good
if (collection.length) {
// ...stuff...
}
JavaScript中类型检测的更多相关文章
- JavaScript中如何检测一个变量是一个String类型?
typeof x === "string" typeof(x) === "string' // 小写 x.constructor === String // 大写类型 同 ...
- Javascript之类型检测
一.检测原始(基本数据:字符串.数字.布尔.null.undefined)类型. 用typeof检测原始类型:1.对于字符串,typeof返回"string"; 2.对于数字,ty ...
- Javascript中类型的判断
数据类型的判断有这么几种方式 1.一元运算符 typeOf 2.关系运算符 instanceof 3.constructor 属性 4.prototype属性 一.typeof typeof的返回值有 ...
- Javascript之类型检测(一)
js中有7种内置类型,这7种类型又分为2大类:基本数据类型和对象(object) 一.检测原始(基本数据:字符串.数字.布尔.null.undefined.symbol)类型. 用typeof检测原始 ...
- JavaScript变量类型检测总结
JavaScript中的变量类型: 基本类型值:Undefined,Null,Boolean,Number和String. 按值访问(可直接操作保存在变量中的变量值): 复制规则:当复制基本类型值时: ...
- JS中类型检测方式
在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...
- Javascript中类型: undefined, number ,string ,object ,boolean
var a1; var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var ...
- JavaScript中如何检测一个变量是一个String类型?请写出函数实现
方法1. function isString(obj){ return typeof(obj) === "string"? true: false; // returntypeof ...
- Javascript 常用类型检测
1.判断变量是否为数组的数据类型? 方法一 :判断其是否具有"数组性质",如slice()方法.可自己给该变量定义slice方法,故有时会失效. 方法二 :obj instance ...
随机推荐
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- Transaction And Lock--锁相关基础
--=======================================================--锁提示--holdlock :将共享锁保留到事务完成,而不是在相应的表.行或数据页 ...
- 基于node.js+socket.io+html5实现的斗地主游戏(1)概述
一.游戏描述 说是斗地主游戏,其实是寝室自创的"捉双A",跟很多地方的捉红10.打红A差不多,大概规则是: 1.基础牌型和斗地主一样,但没有大小王,共52张牌,每人13张,这也是为 ...
- 数独·唯一性技巧(Uniqueness)-2
Hidden Rectangle(隐藏矩形) 在由候选数(AB)组成.可能形成UR结构的4格中,有2-3格存在额外的候选数,此时若以不存在额外候选数的一格为起点,检查其对角格所在的行和列,若该行和列其 ...
- table.insert(tableName, v)
self.teammateList = {} for i=1,3 do local teammate = UI.CreateLuaWidget("Widget_TeammateInfo&qu ...
- cesium编程入门(七)3D Tiles,模型旋转
cesium编程入门(七)3D Tiles,模型旋转 上一节介绍了3D Tiles模型的位置移动,和贴地的操作,这一节来聊一聊模型的旋转, 参考<WebGl编程指南>的第四章 假设在X轴和 ...
- 【OCP-12c】CUUG最新考试原题整理及答案(071-9)
9.(5-5) choose the best answerView the Exhibit and examine the structure of the SALES and STORES tab ...
- Jquery中each
1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function ...
- php中递归查找父级名称
/** * 获取所属公司 * @param array 列表 * @param $id 上级ID * @return array */ private static function get_top_ ...
- shell__常用命令__grep
grep | zgrep (不用解压zip就能直接搜索) -i 不区分大小写 -I 忽略二进制文件 -R或r 递归文件目录 -c 计算找到的总数量 -n 显示行号 -v 显示不包含匹配文本的所有行 - ...