js类型判断

时间:2021-06-23 09:14:36
 console.log('---------------------');
var a="string"; console.log(a); //string
var a=1; console.log(a); //number
var a=false; console.log(a); //boolean
var a; console.log(typeof a); //undfined var a = null; console.log(typeof a); //object
var a = document; console.log(typeof a); //object
var a = []; console.log(a); //object
var a = []; console.log("是否是数组:"+(a instanceof Array)); //object var a = function(){}; console.log(typeof a) //function 除了可以判断数据类型还可以判断function类型
var b = null; console.log(typeof b)//object
var c; console.log(typeof c)//undefined
var c = undefined; console.log(typeof c)//undefined

this是指window对象

<script type="text/javascript">
myglobal = "hello";
console.log(myglobal);
console.log(window.myglobal);
console.log(window['myglobal']);
console.log(this.myglobal);
//alert(this) function sum(x,y){
//不声明的任何变量都会成为一个全局变量,绑定到window上
result = x+y;
return result;
}
sum(1,2);
console.log(this.result)
//debugger; // 预解析:var散布的问题
myname = "global"; // 全局变量
myname1 = "mayn";
function func() {
alert(myname1);//"mayn"
alert(myname); // "undefined"
var myname = "local";
alert(myname); // "local"
}
func();
//第一个alert会弹 出”undefined”是因为myname被当做了函数的局部变量(尽管是之后声明的),
//所有的变量声明当被悬置到函数的顶部了。因此,为了避免这种混
//乱,最好是预先声明你想使用的全部变量。
</script>