<!DOCTYPE html>
<html>
<head>
<title>class12: arguments对象,this对象,call apply</title>
<script type="text/javascript">
// auguments对象 表示函数参数
// arguments.callee 指的是函数本身,与名称无关
/* function fact(num) {
if (num<=1) return 1;
else return num*arguments.callee(num-1);
}
var F = fact;
fact= null;
alert(F(5));*/
// this对象:是指在运行时期基于执行环境所绑定的
/* var k = 10;
function test() {
this.k=20;
}
test();
alert(test.k);// undefined */
// call apply方法
// (1) 扩充作用域
/* window.color = "red";
var obj={color:'blue'};
function showColor() {
alert(this.color);
}
showColor.call(this);//red
showColor.call(obj);//blue*/
// (2) 使用call apply扩充作用域最好的优点是对象与方法之间不需要任何耦合关系
// (3) call方法的简单实现
// function方法
function test1(a,b) {
return a+b;
}
// 自定义对象
function obj(x,y) {
this.x = x;
this.y = y;
return x*y;
}
var o = new obj(10,20);
test1.call(o, 10, 20);//30
// 内部实现如下:
var o = new obj(10,20);
o.method = test1;
alert(o, method(o.x,o.y));
delete o.method;
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title>class12: arguments对象,this对象,call apply</title>
<script type="text/javascript">
// auguments对象 表示函数参数
// arguments.callee 指的是函数本身,与名称无关
/* function fact(num) {
if (num<=1) return 1;
else return num*arguments.callee(num-1);
}
var F = fact;
fact= null;
alert(F(5));*/
// this对象:是指在运行时期基于执行环境所绑定的
/* var k = 10;
function test() {
this.k=20;
}
test();
alert(test.k);// undefined */
// call apply方法
// (1) 扩充作用域
/* window.color = "red";
var obj={color:'blue'};
function showColor() {
alert(this.color);
}
showColor.call(this);//red
showColor.call(obj);//blue*/
// (2) 使用call apply扩充作用域最好的优点是对象与方法之间不需要任何耦合关系
// (3) call方法的简单实现
// function方法
function test1(a,b) {
return a+b;
}
// 自定义对象
function obj(x,y) {
this.x = x;
this.y = y;
return x*y;
}
var o = new obj(10,20);
test1.call(o, 10, 20);//30
// 内部实现如下:
var o = new obj(10,20);
o.method = test1;
alert(o, method(o.x,o.y));
delete o.method;
</script>
</head>
<body>
</body>
</html>