各种判断的集锦

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oYuLian/article/details/82964599

注:
prototype 原型
property  属性

_proto_ 是js变量都有的,而prototype是js函数特有的

*.typeof 判断数据类型

undefined 未定义
Number   数字
String   字符串
boolean  逻辑
function 函数
Object   Object Array null 

var ff7 = 11, ff1 = '11', ff2 = false, ff3 = function gg() {console.log("  ")}, ff4 = [], ff5 = {}, ff6 = null;

console.log(typeof ff0);

for (var i = 1; i < 8; i++) {
	//使用eval() 将字符串转成变量名称
	console.log(typeof eval("ff" + i));
}

*.js判断一个对象中是否含有某个属性


-*.使用 hasOwnProperty 返回true或者是false

-*.使用 typeof 不存在即输出"undefined"字符串 表明未定义

var hh={}

console.log(hh.hasOwnProperty('k'));// false
console.log(typeof(hh.k)=="undefined");// false

*.js判断一个变量的类型(数组,对象,null)

-*.Array.isArray()

-*.使用 typeof 和 isNaN 联合判断

-*.使用 toString() 转成变量所代表的类型

var ff4 = [], ff5 = {}, ff6 = null;

Array.isArray(ff4); //true

(typeof(ff4)==object)&&isNaN(ff4.length)//false

Object.prototype.toString.call(ff4)=="[object Array]" //true

*.js去除空格

-*.使用replace()

-*.固定格式可以使用字符串截取

-*.使用trim()

var ff=" 88 o 56 ";
ff.replace(/ /,"");//去除最左端空格
ff.replace(/ /g,"");//去除全部空格
去除所有空格: ff = ff.replace(/\s*/g,"");      
去除两头空格: ff = ff.replace(/^\s*|\s*$/g,"");
去除左空格: ff = ff.replace( /^\s*/, “”);
去除右空格: ff = ff.replace(/(\s*$)/g, "");
ff.replace(/ /,"").split("").reverse().join("").replace(/ /,"").split("").reverse().join("");// 去除两端空格

ff.trim();// 去除两端的空格

猜你喜欢

转载自blog.csdn.net/oYuLian/article/details/82964599