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
var a = 12;//Number
alert(String(a));//
alert(typeof(String(a)));//string var b = null;//Null
alert(String(b));//null var c = undefined;//Undefined
alert(String(c));//undefined var d = {a:1};
alert(String(d));//'[object Object]'
alert(typeof(String(d)));//string
Number(value)
var a='23';
alert(Number(a));// var b='abs123';
alert(Number(b));//Nan var c='qq';
alert(Number(c));//Nan
隐式类型转换
var a='23',b = 12;
alert(a+b);//2312,转为string后进行字符串拼接 var c;
var d='test';
alert(c+d);//undefinedtest,转为string后进行字符串拼接 alert(b+c);//Nan
3.typeof运算符,typeof运算所有可能的结果,undefined,string,number,boolean,object //http://jsbin.com/tatudaru/1/edit
var a='23'
,b = 12
,c = undefined
,d = null
,e = {key:12}
,f = []
,g = false;
alert(typeof(a));//string
alert(typeof(b));//number
alert(typeof(c));//undefined
alert(typeof(d));//object
alert(typeof(e));//object
alert(typeof(f));//object
alert(typeof(g));//boolean var m = new String('mstr');
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);
}
}