var b = document.body 含义:把body这个对象在内存中的地址放到b变量里面,变量b(b是内存地址的别名)本身也存在内存中,以后的操作是针对body这个地址
变量命名规范
由字母(a-zA-Z)数字(0-9)下划线(_)以及美元符号($)
不能由数字开头
命名尽量用英文并且具有一定的含义
如果有多个英文单词,后面单词的首字母大写
不能使用关键字
首字母不要大写,大写是有特殊含义的
1、window作用域
只要在script标签中定义的变量,默认就在window的作用域之下
默认就是window这个对象里面写代码
2、数据类型
如何判断js中的数据类型:
typeof、instanceof、 constructor、 prototype方法比较
如何判断js中的类型呢,先举几个例子:
1 2 3 4 5 6
var a = "iamstring."; var b = 222; var c= [1,2,3]; var d = newDate(); var e = function(){alert(111);}; var f = function(){this.name="22";};
最常见的判断方法:typeof
1 2 3 4 5 6
alert(typeof a) ------------> string alert(typeof b) ------------> number alert(typeof c) ------------> object alert(typeof d) ------------> object alert(typeof e) ------------> function alert(typeof f) ------------> function
其中typeof返回的类型都是字符串形式,需注意,例如:
1 2
alert(typeof a == "string") -------------> true alert(typeof a == String) ---------------> false
functionA(){}; functionB(){}; A.prototype = new B(); //A继承自B var aObj = new A(); alert(aobj.constructor === B) -----------> true; alert(aobj.constructor === A) -----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
1 2
alert(aobj instanceof B) ----------------> true; alert(aobj instanceof B) ----------------> true;
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
1 2 3
aobj.constructor = A; //将自己的类赋值给对象的constructor属性 alert(aobj.constructor === A) -----------> true; alert(aobj.constructor === B) -----------> false; //基类不会报true了;