this:
/**函数中的this
* 所有函数内部都有变量this
* 它的值是调用函数的当前对象
*/
function Person(name) {
console.log(this);
this.name = name;
this.getName = function () {
console.log(this);
return this.name;
};
this.setName = function (name) {
console.log(this);
this.name = name;
};
}
Person('A'); //this -> window
var p = new Person('B'); //this -> p
p.getName(); //this -> p
var obj = {};
p.setName.call(obj, 'C'); //this -> obj
var tes = p.setName;
tes(); //this -> window
function fun1() {
function fun2() {
console.log(this);
}
fun2(); //this -> window
}
fun1();