this指针的使用场景

mac2022-06-30  20

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 οnclick="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,则默认全局  

转载于:https://www.cnblogs.com/angle-xiu/p/10786396.html

最新回复(0)