复习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
/* 思考this是谁,和call的用法
var a = 10;
var obj = {
a: 2,
add:function() {
return this.a++;
}
}
console.log(obj.add());// 2 obj.a 3
console.log(obj.add()); // 3 obj.a 4
obj.add.call(window);
alert(a);// 11
*/
/*
var a = 10;
var obj = {
a: 2,
add:function() {
return this.a++;
}
};
var obj2 = {
a: 12,
add2:obj.add
};
obj2.add2();
alert(obj2.a);//13
*/
/*
function fun(fn,x,y,z) {
arguments[0](); // arguments [fun2,11,2]
}
function fun2(a,b,c,d,e,f) {
alert(this.length);// 3
alert(this.callee.length); // 4
alert(arguments.length); // 0
alert(arguments.callee.length);//6
}
fun(fun2,11,2);
*/
/*
var length = 10;
function fn() {
alert(this.length);
}
var obj = {
length: 5,
method: function(fn) {
alert(this===obj); // true
fn(); // 10
fn.call(this); // 5
arguments[0]();// 1 [fn]
}
}
obj.method(fn);
*/
function getLength() {
return this.length;
}
function foo() {
this.length = 1; // this===window
return (function() {
var length = 2;
return {
length: function(a,b,c) {
return this.arr.length;
},
arr: [1,2,3,4],
info: function() {
/*this.length == function(a,b,c) {
return this.arr.length;
}
*/
return getLength.call(this.length);
}
}
})();
}
var x = foo().info();// foo() {length:...arr: info...}.info()
alert(x);
</script>
</body>
</html>