您的位置:首页 > Web前端 > JQuery

JQuery学习笔记:this表示什么?

2008-07-28 15:28 645 查看
这一句:

if (window == this) return new jQuery(a, c);

我就对this这个对象进行一些测试:

1.它代表window这个对象

测试代码:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Test</title>

</head>

<body>

<script language="javascript" type="text/javascript">

window.name = "window_name";

alert(this.name);

this.alert("OK");

</script>

</body>

</html>

window对象是指整个IE窗口,包括菜单栏及状态栏,例如:

window.status="hello";

window.moveTo(-12,-12);

2.它代表当前的类

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Test</title>

</head>

<body>

<script language="javascript" type="text/javascript">

class1 = function()

{

var name1 = "ok";

this.name = "asd";

alert(name1);

alert(this.name);

}

var o = new class1;

alert(o.name1);

alert(o.name);

</script>

</body>

</html>

用VS跟踪时,在类class1中,this还是空空的一个Object,当它得到name之后,就可以在类之外访问到了。所以这种可以当做类的公开属性。私有属性用var来定义,外部不可访问。

对于方法也可以用this来区分公有与私有

class1 = function()

{

function aa()

{

alert("aa");

}

this.bb=function ()

{

alert("bb");

}

}

var o = new class1;

alert(o.aa);

o.aa();

alert(o.bb);

o.bb();

如果在类的外面,也可以通过class1.prototype来代替this

class1.prototype.cc=function()

{

alert("cc");

}

var o = new class1;

alert(o.cc);

o.cc();

3.函数调用中出现this时,它代表什么?

经常有函数中出现this,如

<a herf="#" onclick="dosomething(this);">iii</a>

function dosomething(abc)

{

abc.innerText="OK";

}

运行时,this代表的就是元素A。

4.函数中出现this时,它代表什么?

<a herf="#" onclick="dosomething();">iii</a>

function dosomething()

{

this.innerText="OK";

}

这时,它却是window对象,因为此时dosomething归window所有。

如果改为

<a id="a1" href="#">iii</a>

a1.onclick=function dosomething()

{

this.innerText="OK";

}

这时,this就是a1了。

本来,我遇到的就是这几种情况,但后来,在CSDN上又看到一段代码(http://topic.csdn.net/t/20060517/22/4759575.html#),又引起一阵折腾:

<html>

<head>

<script language="jscript">

function myclass()

{

this.name="myname";

this.showName=showName;

function showName()

{

alert(getName());

}

function getName()

{

return this.name;

}

}

var obj=new myclass;

obj.showName();

</script>

</head>

<body>

</body>

</html>

系统最后提示空白,而不是我想象中的myname。跟踪进去,发现此时的this却是表示window,这就说明我以前的想法有不正确的。处理的方法是有几种,如:

1:把getName也提升为共用方法

function getName()

改为

this.getName=function ()

2:用call指明

alert(getName());

改为

alert(getName.call(this));

3:用一个变量代替this

先在第一句加入

var a=this;

然后

return this.name;

改为

return a.name;

但我找不到理论上的支持。后来从“JavaScript 面向对象程序设计”中找到一段:

1.2 this 和执行上下文

……

那就先来看看什么是执行上下文吧。那什么是执行上下文呢?

如果当前正在执行的是一个方法,则执行上下文就是该方法所附属的对象,如果当前

正在执行的是一个创建对象(就是通过 new 来创建)的过程,则创建的对象就是执行

上下文。

如果一个方法在执行时没有明确的附属于一个对象,则它的执行上下文是全局对象(

顶级对象),但它不一定附属于全局对象。全局对象由当前环境来决定。在浏览器环

境下,全局对象就是 window 对象。

定义在所有函数之外的全局变量和全局函数附属于全局对象,定义在函数内的局部变

量和局部函数不附属于任何对象。

……

以下代码做为测试用

var x = "全局变量";

function method()

{

alert("全局方法中 x:" + x);

alert("全局方法中 this.x:" + this.x);

}

function class1()

{

var x = "私有变量";

function method1()

{

alert("私有方法中 x:" + x);

alert("私有方法中 this.x:" + this.x);

}

var method2 = method;

this.x = "公用变量";

this.method1 = function()

{

alert("公用方法中 x:" + x);

alert("公用方法中 this.x:" + this.x);

}

this.method2 = method;

//构造器

{

this.method1(); //结果:公用方法中 私有变量、公用变量

this.method2(); //结果:全局方法中 全局变量、公用变量

method1(); //结果:私有方法中 私有变量、全局变量

method2(); //结果:全局方法中 全局变量、全局变量

method1.call(this); //结果:私有方法中 私有变量、公用变量

method2.call(this); //结果:全局方法中 全局变量、公用变量

}

}

var o = new class1();

method(); //结果:全局方法中 全局变量、全局变量

o.method1(); //结果:公用方法中 私有变量、公用变量

o.method2(); //结果:全局方法中 全局变量、公用变量

看来私有方法中,this是指向全局的。这就造成我们写程序时别扭,因此用一个代码把它强制转过来,这段代码就是

if (window == this) return new jQuery(a, c);

即,如果此时的this是window,则建立一个对象出来,把参数传给新对象,在新对象的区域中执行,这样this就是指jQuery了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: