JavaScript this 关键词
JavaScript this 关键词
实例
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表 <b>person</b> 对象。</p>
<p>因为 person 对象“拥有” fullName 方法。</p>
<p id="demo"></p>
<script>
// 创建对象:
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
// 显示来自对象的数据:
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 代表 person 对象。 因为 person 对象“拥有” fullName 方法。 Bill Gates
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
this 是什么?
JavaScript this
关键词指的是它所属的对象。
它拥有不同的值,具体取决于它的使用位置:
- 在方法中,
this
指的是所有者对象。 - 单独的情况下,
this
指的是全局对象。 - 在函数中,
this
指的是全局对象。 - 在函数中,严格模式下,
this
是 undefined。 - 在事件中,
this
指的是接收事件的元素。
像 call()
和 apply()
这样的方法可以将 this 引用到任何对象。
方法中的 this
在对象方法中,this
指的是此方法的“拥有者”。
在本页最上面的例子中,this
指的是 person 对象。
person 对象是 fullName 方法的拥有者。
fullName : function() {
return this.firstName + " " + this.lastName;
}
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表 <b>person</b> 对象。</p>
<p>因为 person 对象“拥有” fullName 方法。</p>
<p id="demo"></p>
<script>
// 创建对象:
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
// 显示来自对象的数据:
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 代表 person 对象。 因为 person 对象“拥有” fullName 方法。 Bill Gates
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
单独的 this
在单独使用时,拥有者是全局对象,因此 this
指的是全局对象。
在浏览器窗口中,全局对象是 [object Window]
:
示例代码:
var x = this;
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 引用 window 对象:</p>
<p id="demo"></p>
<script>
var x = this;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 引用 window 对象: [object Window]
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
在严格模式中,如果单独使用,那么 this
指的是全局对象 [object Window]
:
示例代码:
"use strict";
var x = this;
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 引用 window 对象:</p>
<p id="demo"></p>
<script>
"use strict";
var x = this;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 引用 window 对象: [object Window]
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
函数中的 this(默认)
在 JavaScript 函数中,函数的拥有者默认绑定 this
。
因此,在函数中,this
指的是全局对象 [object Window]
。
示例代码:
function myFunction() {
return this;
}
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表“拥有” myFunction 的对象:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction();
function myFunction() {
return this;
}
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 代表“拥有” myFunction 的对象: [object Window]
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
函数中的 this(严格模式)
JavaScript 严格模式不允许默认绑定。
因此,在函数中使用时,在严格模式下,this
是未定义的(undefined
)。
示例代码:
"use strict";
function myFunction() {
return this;
}
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在函数中,默认地,<b>this</b> 引用全局对象。</p>
<p>在严格模式中,<b>this</b> 是 <b>undefined</b>,因为严格模式不允许默认绑定:</p>
<p id="demo"></p>
<script>
"use strict";
document.getElementById("demo").innerHTML = myFunction();
function myFunction() {
return this;
}
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在函数中,默认地,this 引用全局对象。 在严格模式中,this 是 undefined,因为严格模式不允许默认绑定: undefined
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
事件处理程序中的 this
在 HTML 事件处理程序中,this
指的是接收此事件的 HTML 元素:
示例代码:
<button onclick="this.style.display='none'">
点击来删除我!
</button>
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<button onclick="this.style.display='none'">单击来删除我!</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
对象方法绑定
在此例中,this
是 person 对象(person 对象是该函数的“拥有者”):
示例代码:
var person = {
firstName : "Bill",
lastName : "Gates",
id : 678,
myFunction : function() {
return this;
}
};
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表“拥有” fullName 方法的 person 对象。</p>
<p id="demo"></p>
<script>
// 创建对象:
var person = {
firstName : "Bill",
lastName : "Gates",
id : 678,
myFunction : function() {
return this;
}
};
// 显示来自对象的数据:
document.getElementById("demo").innerHTML = person.myFunction();
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 代表“拥有” fullName 方法的 person 对象。 [object Object]
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表 <b>person</b> 对象。</p>
<p>因为 person 对象“拥有” fullName 方法。</p>
<p id="demo"></p>
<script>
// 创建对象:
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
// 显示来自对象的数据:
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 代表 person 对象。 因为 person 对象“拥有” fullName 方法。 Bill Gates
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
换句话说,this.firstName 意味着 this(person)对象的 firstName 属性。
显式函数绑定
call()
和 apply()
方法是预定义的 JavaScript 方法。
它们都可以用于将另一个对象作为参数调用对象方法。
您可以在本教程后面阅读有关 call()
和 apply()
的更多内容。
在下面的例子中,当使用 person2 作为参数调用 person1.fullName 时,this
将引用 person2,即使它是 person1 的方法:
示例代码:
var person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person2 = {
firstName:"Bill",
lastName: "Gates",
}
person1.fullName.call(person2); // 会返回 "Bill Gates"
完整实例:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<strong>this</strong> 引用 person2,即使它是 person1 的方法:</p>
<p id="demo"></p>
<script>
var person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person2 = {
firstName:"Bill",
lastName: "Gates",
}
var x = person1.fullName.call(person2);
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
运行结果:
Javascript this 关键词 在本例中,this 引用 person2,即使它是 person1 的方法: Bill Gates
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html