码迷,mamicode.com
首页 > 编程语言 > 详细

javascript中的this对象

时间:2016-11-04 23:28:36      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:rip   纯粹   var   log   alert   this   lob   生成   全局对象   

this表示函数运行时,自动生成的一个内部对象,只能在函数内部运行

1 function test(){
2     this.x = 1;
3  }

随着使用场景的变化,this的值会发生变化

原则:this指的是调用函数的那个对象。

使用场景:

1.纯粹的函数调用

这是函数最常见的用法,属于全局性调用,因此this就代表全局对象Global

1 function test(){
2      this.x = 1;
3      alert(this.x);
4 }
5 test();//1

 

1 var x = 1;
2 function test(){
3 this.x = 0;
4 }
5 test();
6 alert(x);//0

2.作为对象方法调用

1 function test(){
2 alert(this.x);
3 }
4 var o = {};
5 o.x = 1;
6 o.m = test;
7 o.m();//1

 

javascript中的this对象

标签:rip   纯粹   var   log   alert   this   lob   生成   全局对象   

原文地址:http://www.cnblogs.com/facous/p/6031831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!