欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

this指针的初运用

程序员文章站 2022-05-18 22:39:04
this一般运用场景: 1.位于函数中,谁调用指向谁 var make = "Mclaren"; var model = "720s" function fullName() { console.log(this.make + " " + this.model); } var car = { mak ......

this一般运用场景:

1.位于函数中,谁调用指向谁

var make = "mclaren";

var model = "720s"
function fullname() {
console.log(this.make + " " + this.model);
}
var car = {
make: "lamborghini",
model: "huracán",
fullname: function () {
console.log(this.make + " " + this.model);
}
}
car.fullname(); // lmborghini huracán
window.fullname(); // mclaren 720s
fullname(); // mclaren 720s
 
2.事件调用,指向调用元素
<button onclick="this.style.display='none'">
remove me!
</button>
 
3.方法中调用
var car = {
make: "lamborghini",
model: "huracán",
fullname: function () {
console.log(this.make + " " + this.model);
console.log(car.make + " " + car.model);
}
}
car.fullname();
 
4.直接使用this,则默认全局