ECMAScript5中Array.isArray是原生的判断数组的方法,IE9及以上支持。考虑到兼容性,在没有此方法的浏览器中,可以使用 Object.prototype.toString.call(obj) === '[object Array]'替代。
var isArray = Array.isArray || function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}JQ的确封装了一个函数jQuery.inArray( value, array ) 搜索数组中指定值并返回它的索引(如果没有找到则返回-1)。
value要搜索的值。array一个数组,通过它来搜索。
function inArray1(needle,array,bool){
if(typeof needle=="string"||typeof needle=="number"){
for(var i in array){
if(needle===array[i]){
if(bool){
return i;
}
return true;
}
}
return false;
}
}函数
最简单且性能最好的办法就是 typeof obj == 'function'。考虑到某些版本浏览器存在的bug,最靠谱的办法是 Object.prototype.toString.call(obj) === '[object Function]'。
var isFunction = function(obj) {
return Object.prototype.toString.call(obj) === '[object Function]';
}
if(typeof /./ != 'function' && typeof Int8Array != 'object') {
isFunction = function(obj) {
return typeof obj == 'function';
}
}对象
在JavaScript中复杂类型是对象,函数也是对象。对上述2者使用typeof,可以分别得到'object'和'function'。另外,还要排除null值的情况,因为typeof null 得到的也是 'object'。
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
Copyright © 2019- unap.cn 版权所有
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务