日期:2014-05-16  浏览次数:20441 次

javascript笔记3

1、var定义的是作用域上的变量,没有var则是全局上的变量,var定义的变量不能再函数域中使用。

2、当javascript运行到一个函数时,会在当前作用域中建立一个子作用域,将当前作用域的全局性切换给这个新建的子作用域。

3、函数本身有个caller属性,调用函数调用者,全局调用者是null,上代码

<script type="text/javascript">

function whocallme(){
  alert("my caller is"+whocallme.caller);
}

function callA(){
	whocallme();
}
function callB(){
	whocallme();
}
alert(whocallme.caller);
callA();//mycaller is function callA(){}
callB();//mycaller is function callB(){}
whocallme();//输出my caller is null
</script>
caller属性opera浏览器不支持哦,其他浏览器基本支持。
4、javascript函数就是对象,这也是javascript特别之处,看个例子

<script type="text/javascript">



function sing(){
	alert(sing.author +" : "+sing.poem);
}
sing.author = "李白";
sing.poem= "白云依山尽"
sing();

sing.author="王维";
sing.poem="黄河入海流"
sing();
</script>

5、javascript中this作用

<script type="text/javascript">
function whoAmI(){
	alert("i am "+this.name +" of "+typeof(this));
}
whoAmI();//i am  of obhect
var Gates = {name:"bill gates"};
Gates.whoAmI = whoAmI;
Gates.whoAmI();//i am bill gates of object

var steven = {name:"steven"};
steven.whoAmI = whoAmI;
steven.whoAmI();//i am steven of object

whoAmI.call(Gates);//i am bill gates of object
whoAmI.call(steven);//i am steven of object

Gates.whoAmI.call(steven);//i am steven of object
steven.whoAmI.call(Gates);//i am bill gates of object
whoAmI.whoAmI = whoAmI;//将whoami函数设置为本身
whoAmI.name = "whoami";
whoAmI.whoAmI();//i am whoaimi of function
</script>
从上述代码可以看出this并不是指函数本身所属于的对象,this只是当前任意对象和function结合时的一个概念,在javascript中你只能把this看成当前要服务的对象。