JavaScript 之 this 小析
this指向当前运行所处的环境
<script>
var n = 'zs'; // 如果后面要对window对象属性进行测试,最好不要覆盖window的原属性,如window.name
var person = {
n: '张三',
describe: function(){
return '姓名: ' + this.n; // this指向当前运行所处的环境(对象)
}
};
var result1 = person.describe(); // result1为describe的运行结果,故describe的环境为person,即this.n指向的是person.n
console.log(result1); //张三
var result2 = person.describe; // result2=describe
console.log(result2()); // 即此时就相当于describe()直接运行在window当中(仅为理解方便),this.n指向的是window.n, 结果:zs
</script>
以上仅做简单理解参考,详解回头有空再做补充