数据类型
数据类型有 undefined,null,bool,number,string,object和function共七种类型。
typeof运算得到的结果是一个string类型的值,所以使用两次typeof运算符得到的结果一定是string。
有如下几个需要注意,平时经验多积累,以下是在浏览器控制台输入的
typeof undefined
"undefined"
typeof null
"object"
typeof NaN
"number"
typeof Infinity
"number"
NaN !=NaN
true
Infinity/Infinity
NaN
"123" == 123
true
undefined,null,"",0这四个值转换为逻辑值是false,除这四个加上false本身,任何其他东西(包括简单类型、所有对象和函数)转为逻辑值都是true。
这5个值除了undefined == null,其他互不相等。
但要注意
undefined === null
false
"123" === 123
false
对象类型的一个好例子
<scrpit>
var life = {};
for(life.age = 1;life.age<=3;life.age++){
switch(life.age){
case 1:
life.body = "卵细胞";
life.say = function(){
alert(this.age+this.body);
}
break;
case 2:
life.tail = "尾巴";
life.gill = "腮";
life.body = "蝌蚪";
life.say = function(){
alert(this.age+this.body+" -"+this.tail+" ,"+this.gill);
}
break;
case 3:
delete life.tail;
delete life.gill;
life.legs = "四条腿";
life.lung = "肺";
life.body = "青蛙";
life.say = function(){
alert(this.age+this.body+" -"+this.legs+" ,"+this.lung);
}
break;
}
life.say();
}
</script>
JavaScript执行顺序
注意如下问题,将弹出两次zhanglulu
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>javascript学习</title>
</head>
<body>
<script type="text/javascript">
function hbk(){
alert("huangbaokang");
}
hbk();
function hbk(){
alert("zhanglulu");
}
hbk();
</script>
</body>
</html>
这样才会先弹出huangbaokang,然后再弹出zhanglulu,结论:JavaScript是按段执行的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>javascript学习</title>
</head>
<body>
<script type="text/javascript">
function hbk(){
alert("huangbaokang");
}
hbk();
</script>
<script type="text/javascript">
function hbk(){
alert("zhanglulu");
}
hbk();
</script>
</body>
</html>
var关键字的注意点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>javascript学习</title>
</head>
<body>
<script type="text/javascript">
var yourName = "张露露";
myName = "黄宝康";
console.log(myName +" like "+yourName);// 黄宝康 like 张露露
changeNames();// 改变
function changeNames(){
console.log("你的名字"+yourName);// 你的名字undefined
console.log("我的名字"+myName);// 我的名字黄宝康
var yourName = "zhanglulu";
myName = "huangbaokang";
console.log(myName + " like "+yourName); // huangbaokang like zhanglulu
}
console.log(myName + " like "+yourName);// huangbaokang like 张露露
</script>
</body>
</html>
输出结果如下:
黄宝康 like 张露露
你的名字undefined
我的名字黄宝康
huangbaokang like zhanglulu
huangbaokang like 张露露
结论:用var修饰在函数内外是两个东西,没有用var修饰,函数内外是一个东西。
访问Javascript内部作用域对象,提供了函数本身,函数的caller属性,this关键字和arguments隐含对象。
如,使用自身
<script type="text/javascript">
function hbk(){
alert(hbk.toString());// 函数体内使用自身标识符
}
hbk();
</script>
弹出如下:
函数自身有一个caller属性,表示调用当前函数的上层函数。这就提供了一个可以追溯函数调用来源的线索,特别是对于调试JavaScript来说是不可多得的宝贝。
<script type="text/javascript">
function whoCallMe(){
console.log("调用者是"+whoCallMe.caller);// 输出自己的caller
}
function huangbaokang(){
whoCallMe();
}
function zhanglulu(){
whoCallMe();
}
console.log(whoCallMe.caller);// 输出 null
whoCallMe();// 调用者是null
huangbaokang();
zhanglulu();
</script>
控制台输出如下:
null
调用者是null
调用者是function huangbaokang(){
whoCallMe();
}
调用者是function zhanglulu(){
whoCallMe();
}
arguments对象
<script type="text/javascript">
function huangbaokang(){
console.log(arguments[0]+arguments[1]);// 输出helloworld
}
huangbaokang("hello","world");
</script>
this关键字的理解
arguments对象转数组的方法 Array.prototype.slice.apply(arguments);
prototype原型
<script type="text/javascript">
/*原型*/
function Person(name){
this.name = name;
}
Person.prototype.sayHello = function(){
alert("hello "+this.name);
}
function HBK(name,address){
Person.call(this,name);// 调用父类
this.address = address;// 扩展属性
}
HBK.prototype = new Person();// 子类的原型设置成父类
HBK.prototype.telAddress = function(){
alert(this.name+"'s address is "+this.address);
}
var me = new HBK("huangbaokang","ganzhou");
me.sayHello();// 调用父类继承过来的 弹出 hello huangbaokang
me.telAddress();// 调用自己原型链上的 弹出 huangbaokang's address is ganzhou
</script>
块级作用域问题
<script type="text/javascript">
for(var i=0;i<10;i++){
var tmp = "huangbaokang";
}
console.log(i);// 10
console.log(tmp);// huangbaokang
for(var j=0;j<10;j++){
console.log(tmp);// 打印10次huangbaokang
}
</script>
Javascript它不支持函数外部的块级作用域。因此,无论是i的值,还是循环过程或者条件逻辑中定义的任何“临时”变量,都会在其所在的块执行后继续存在。把块包含在一个函数中,并直接在代码里调用该函数,后续的变量将变成undefined。
<script type="text/javascript">
(function(){
for(var i=0;i<10;i++){
}
})();
console.log(i); // undefined
</script>