1 console.log('---------------------'); 2 var a="string"; console.log(a); //string 3 var a=1; console.log(a); //number 4 var a=false; console.log(a); //boolean 5 var a; console.log(typeof a); //undfined 6 7 var a = null; console.log(typeof a); //object 8 var a = document; console.log(typeof a); //object 9 var a = []; console.log(a); //object 10 var a = []; console.log("是否是数组:"+(a instanceof Array)); //object 11 12 var a = function(){}; console.log(typeof a) //function 除了可以判断数据类型还可以判断function类型 13 var b = null; console.log(typeof b)//object 14 var c; console.log(typeof c)//undefined 15 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>