Home > Web Front-end > JS Tutorial > Discussion on the pointing issue of this in js

Discussion on the pointing issue of this in js

小云云
Release: 2018-03-16 17:09:09
Original
1277 people have browsed it

This article mainly shares with you the discussion on the pointing issue of this in js. The this keyword represents the object of the method currently being executed. If there is no current method, it refers to the global variable. That is to say, this represents the reference of the object calling the method.

#1. This in the global scope or ordinary function points to the global object window.

//直接打印
    console.log(this) //window
//function声明函数
    function bar () {conso
le.log(this)}bar() //window
//function声明函数赋给变量
    var bar = function () {console.log(this)}bar() //window
//自执行函数
    (function () {console.log(this)})(); //window
Copy after login

##2. Who calls this and points to whom in the method call

//对象方法调用
    var person = {run: function () {console.log(this)}}person.run()// person
//事件绑定
    var btn = document.querySelector("button")btn.onclick = function () {console.log(this) // btn}
//事件监听
    var btn = document.querySelector("button")btn.addEventListener('click', function () {console.log(this) //btn})
//jquery的ajax
    $.ajax({ self: this,    type:"get",    url: url,    async:true,    success: function (res) {console.log(this)
 // this指向传入$.ajxa()中的对象
    console.log(self) // window  } }); 
//这里说明以下,将代码简写为$.ajax(obj) ,this指向obj,在obj中this指向window,因为在在success方法中,独享obj调用自己,所以this指向obj
Copy after login

3. In the constructor or constructor prototype object this points to the instance of the constructor

//不使用new指向window
    windowfunction Person (name) {console.log(this) // window    
    this.name = name;}Person('inwe')
//使用new
    function Person (name) {this.name = name   console.log(this) //people     
    self = this  }  var people = new Person('iwen')  console.log(self === people) //true
//这里new改变了this指向,将this由window指向Person的实例对象people
new改变this指向,将this指向window改为指向person的实例people
Copy after login

Change the pointer of this:

The function itself is a special type, Most people think that it is a variable. Who this points to cannot be determined when the function is defined. Only when the function is executed can it be determined who this points to. In fact, this points to the function that finally calls it.

Related recommendations:

Detailed explanation of the usage of $this and access qualifiers in PHP

Modify the this pointer in JavaScript Various methods

This rules and this object usage examples in JavaScript

The above is the detailed content of Discussion on the pointing issue of this in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template