<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>this-调用函数的方式有关</title>
<script type="text/javascript">
/*
var a = 1;
function test(){
var a = 2;
console.log(this);
console.log(this.a);
}
test();
var obj = {a:3};
obj.test=test; //引用数据类型
obj.test(); //3
var obj2 = {a:5};
obj2.test=obj.test; //引用数据类型
obj2.test(); //5
//test obj.test obj2.test 指向同一个函数 调用的方式不同 this不同
var test2 = obj2.test;
//加括号为调用方法,不加为方法的引用
test2(); //window
//
*/
var obj = {
name:"terry",
sayName:function(){
alert(this.name);
}
}
//var callMethod=function(method){method();}
function callMethod(method){
method(); //method中的this 指向window window中找不到name
//obj.sayName();
}
callMethod(obj.sayName);
/*obj.sayName();
var test = obj.sayName();
test();
*/
</script>
</head>
<body>
</body>
</html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>this-调用函数的方式有关</title>
<script type="text/javascript">
/*
var a = 1;
function test(){
var a = 2;
console.log(this);
console.log(this.a);
}
test();
var obj = {a:3};
obj.test=test; //引用数据类型
obj.test(); //3
var obj2 = {a:5};
obj2.test=obj.test; //引用数据类型
obj2.test(); //5
//test obj.test obj2.test 指向同一个函数 调用的方式不同 this不同
var test2 = obj2.test;
//加括号为调用方法,不加为方法的引用
test2(); //window
//
*/
var obj = {
name:"terry",
sayName:function(){
alert(this.name);
}
}
//var callMethod=function(method){method();}
function callMethod(method){
method(); //method中的this 指向window window中找不到name
//obj.sayName();
}
callMethod(obj.sayName);
/*obj.sayName();
var test = obj.sayName();
test();
*/
</script>
</head>
<body>
</body>
</html>