摘要:对象类型常见的有,,,正则新增自己提供的乐行判断如果不对对象做严格区分使用。的实现使用了原型继承的表示左表达式,表示右表达式,它是用是否等于来判断对象的类型的。常见框架和库的实数据类型判断测试这里将的实现原理抽取出来,用原生实现。
JavaScript一共有六种数据类型,分为原始类型(又名基本类型)和对象类型(又名引用类型)
原始类型有五种,分别为number,string,boolean,undefined,null五种。
对象类型常见的有Function,Array,Date,正则
ES6新增Symbol
JavaScript 自己提供的乐行判断 type如果不对对象做严格区分使用type。
number:
typeof 1; // "number"
string:
typeof "hello world"; // "string"
boolean:
typeof true; // "boolean"
undefined:
typeof undefined; // "undefined"
null:
typeof null; // "undefined" (特别)
object:
typeof {}; // "object"
Symbol:
typeof Symbol(); // "symbol"
null要多带带处理,就像jQuery中一样,如果要判断的对象为null,就直接返回 String(obj) 。
因此,可以写一个判断基本类型的方法:
var type = function(obj) {
if(obj === null) return String(obj);
return typeof obj;
}
instanceof
instanceof 是用来明确对象为某种特定类型的方法。
instanceof 的实现使用了原型继承的L表示左表达式,R表示右表达式,它是用L.__proto__ 是否等于 R.prototype 来判断对象的类型的。
String:
var str = new String("hello world");
str instanceof String // true
String:
var str = new Number("10");
str instanceof String // true
Datte:
var o = new Date(); o instanceof Date; // true
Array:
var o = new Array(); o instanceof Array; // true
RegExp:
var reg = new RegExp("/^[a-z]$/");
reg instanceof RegExp; // true
Object:
var o = new Object();
o instanceof Object; // true
var o2 = {};
o2 instanceof Object; // true
Function:
var o = function() {};
o instanceof Function; // true
Func(自定义):
function Func() {}
var o = new Func();
o instanceof Func; // true
instanceof 即可以验证自定义对象,也可以验证内置对象类型,但有一点我们要注意,那就是他们本身就是对象。
var str = new String("hello world");
str instanceof Object; // true
我们在一开始就说明了instanceof的实现原理,知道instanceof左边是右边的实例,所以我们可以用如下代码获取对象的类型名:
obje.__proto__.constructor.name;
因此,我们可以写一个判断对象类型的方法如下:
objectType = function(obj) {
if(typeof obj === "object" || typeof obj === "function") {
return obj.__proto__.constructor.name;
}
}
上面的方法虽好,但是ie浏览器不支持 __proto__(这个属性也是我们判断浏览器与非浏览器的常用方式)。
下面介绍一个万能的方法。
Object.prototype.toString.call(obj)Object.prototype.toString.call("hello world"); // "[object String]"
Object.prototype.toString.call(1); // "[object Number]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(); // "[object Undefined]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call(/test/i); // "[object RegExpArray]"
Object.prototype.toString.call(function () {}); // "[object Function]"
获取数据类型的代码如下:
var core_toString = Object.prototype.toString;
var getObjectType = function(obj) () {
return core_toString.call(obj).slice(8, -1);
}
这个方法可以判断所有的数据类型,也是官方推荐的,但是在实际的开发中,我们使用 typeof 来判断基本类型,用 Objet.prototype.toString.call(obj) 判断引用类型。
常见框架和库的实数据类型判断jQuery:
var class2type = {};
var core_toString = Object.prototype.toString;
"Boolean Number String Function Array Date RegExp Object Error".split(" ").forEach(function(name, i) {
class2type["[object " + name + "]"] = name.toLowerCase();
});
var getType = function (obj) {
if (obj == null) {
return String(obj);
}
return typeof obj === "object" || typeof obj === "function" ?
class2type[ core_toString.call(obj) ] || "object" :
typeof obj;
};
// 测试
getType(function(){}); // "function"
getType([]); // "array"
这里将jQuery的实现原理抽取出来,用原生js实现。
vue.js
/**
* 判断是否为基本数据类型
*/
function isPrimitive (value) {
return (
typeof value === "string" ||
typeof value === "number" ||
// $flow-disable-line
typeof value === "symbol" ||
typeof value === "boolean"
)
}
/**
* 判断是否为普通对象
*/
function isObject (obj) {
return obj !== null && typeof obj === "object"
}
/**
* 获取原生类型,如: [object Object]
*/
var _toString = Object.prototype.toString;
function toRawType (value) {
return _toString.call(value).slice(8, -1)
}
/**
* 普通对象
*/
function isPlainObject (obj) {
return _toString.call(obj) === "[object Object]"
}
/**
* 正则对象
*/
function isRegExp (v) {
return _toString.call(v) === "[object RegExp]"
}
angular2:
function isPresent(obj) {
return obj !== undefined && obj !== null;
}
exports.isPresent = isPresent;
function isBlank(obj) {
return obj === undefined || obj === null;
}
exports.isBlank = isBlank;
function isBoolean(obj) {
return typeof obj === "boolean";
}
exports.isBoolean = isBoolean;
function isNumber(obj) {
return typeof obj === "number";
}
exports.isNumber = isNumber;
function isString(obj) {
return typeof obj === "string";
}
exports.isString = isString;
function isFunction(obj) {
return typeof obj === "function";
}
exports.isFunction = isFunction;
function isType(obj) {
return isFunction(obj);
}
exports.isType = isType;
function isStringMap(obj) {
return typeof obj === "object" && obj !== null;
}
exports.isStringMap = isStringMap;
function isPromise(obj) {
return obj instanceof _global.Promise;
}
exports.isPromise = isPromise;
function isArray(obj) {
return Array.isArray(obj);
}
exports.isArray = isArray;
function isDate(obj) {
return obj instanceof exports.Date && !isNaN(obj.valueOf());
}
exports.isDate = isDate;
我们常见的就是这几种实现方式,或是这几种方式的混合(zepto.js)。
JavaScript相关文章github.com
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/92545.html
摘要:和体现了对不同异常情况的分类。是程序正常运行中,可以预料的意外情况,可能并且应该被捕获,进行相应的处理。是指在正常情况下,不大可能出现的情况,绝大部分都会使程序处于非正常不可恢复的状态。常见的非对称加密包括等。 面试,无非都是问上面这些问题(挺多的 - -!),聘请中高级的安卓开发会往深的去问,并且会问一延伸二。以下我先提出几点重点,是面试官基本必问的问题,请一定要去了解! 基础知识...
摘要:所以,刚开始,我从源码比较短的包含注释只有行开始学习起。一般,在客户端浏览器环境中,即为,暴露在全局中。学习以后判断直接使用看起来也优雅一点滑稽脸。在的函数视线中,的作用执行一个传入函数次,并返回由每次执行结果组成的数组。 前言 最近在社区浏览文章的时候,看到了一位大四学长在寻求前端工作中的面经,看完不得不佩服,掌握知识点真是全面,无论是前端后台还是其他,都有涉猎。 在他写的文章中,有...
摘要:最近开始看源码,并将源码解读放在了我的计划中。今天就跟大家聊一聊中一些常用类型检查方法,以及一些工具类的判断方法。用是否含有属性来判断工具类判断方法接下来看下一些常用的工具类判断方法。 Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中。 阅读一些著名框架类库的源码,就好像和一个个大师对话...
阅读 3216·2023-04-25 19:08
阅读 1779·2021-11-16 11:45
阅读 2683·2021-10-13 09:40
阅读 5333·2021-09-30 09:47
阅读 2686·2019-08-30 15:44
阅读 2737·2019-08-30 13:03
阅读 1595·2019-08-30 12:56
阅读 2117·2019-08-26 14:04