1. javascript 五种基本数据类型 Undefined Boolean Number String Null,Undefined 对应的值只有一个 undefined, Boolean 对应的值为true false, Null 对应的值只有null,
Number对应的值为所有浮点数和整数(var a = 2.4), String 对应的值为定义的字符串
2.强制类型转换,
Boolean(value),"undefined",null,0,false,"" 转换均为false,其他为true,也可用!!value转换.
String(value); //http://jsbin.com/lukaxoqi/1/edit
1 var a = 12;//Number 2 alert(String(a));//12 3 alert(typeof(String(a)));//string 4 5 var b = null;//Null 6 alert(String(b));//null 7 8 var c = undefined;//Undefined 9 alert(String(c));//undefined 10 11 var d = {a:1}; 12 alert(String(d));//'[object Object]' 13 alert(typeof(String(d)));//string
Number(value)
1 var a='23'; 2 alert(Number(a));//23 3 4 var b='abs123'; 5 alert(Number(b));//Nan 6 7 var c='qq'; 8 alert(Number(c));//Nan
隐式类型转换
1 var a='23',b = 12; 2 alert(a+b);//2312,转为string后进行字符串拼接 3 4 var c; 5 var d='test'; 6 alert(c+d);//undefinedtest,转为string后进行字符串拼接 7 8 alert(b+c);//Nan
3.typeof运算符,typeof运算所有可能的结果,undefined,string,number,boolean,object //http://jsbin.com/tatudaru/1/edit
1 var a='23' 2 ,b = 12 3 ,c = undefined 4 ,d = null 5 ,e = {key:12} 6 ,f = [] 7 ,g = false; 8 alert(typeof(a));//string 9 alert(typeof(b));//number 10 alert(typeof(c));//undefined 11 alert(typeof(d));//object 12 alert(typeof(e));//object 13 alert(typeof(f));//object 14 alert(typeof(g));//boolean 15 16 var m = new String('mstr'); 17 alert(typeof(m));//object
4.js 遍历object对象 http://jsbin.com/zetiheza/1/edit
//hasOwnProperty,Object的prototype的方法 Object.prototype.bar = "object上定义的属性";//先给Object定义属性 var test = { ownpro : '对象自己的属性' }; alert('bar' in test);//true for(var i in test){ console.log(i);//ownpro,bar } for(var i in test){ if(test.hasOwnProperty(i)){ console.log(i);//ownpro } } //如果test中定义了hasOwnProperty(),则会覆盖继承自Object的hasOwnProperty方法,再判断的话就可能出问题,可以用call解决 var test={ ownpro : '对象自己的属性', hasOwnProperty : function(){ return false; } }; for(var i in test){ if(test.hasOwnProperty(i)){ console.log(i);//always return false; } } //we can fix following bellow for(var i in test){ if({}.hasOwnProperty.call(test,i)){ console.log(i); } }