Home >Web Front-end >JS Tutorial >The pointing meaning of this object

The pointing meaning of this object

一个新手
一个新手Original
2017-09-25 10:40:451465browse

1. In the function:

function foo(){
   return this;
}

Whoever calls the function points to it; the direct call points to the window;

2. In the event: in the html event, point to the window; in the dom0 event, point to the trigger of the event (the node of the bound element); in the dom2 event, point to the bound element node in non-IE; in IE, point directly to the window;

3. In closure: this points to window;

4. In object: this points to current object; if there are multiple levels of object wrapping , refers to the upper-level object;
(1).

var foo = {
         a:18,
        num:{
         a:10,
           num:function(){
          console.log(this.a);//10
                   }
               }
       }
  foo.num.num();

(2).

var foo = {
        a:18,
         num:{
         num:function(){
         console.log(this.a);//undefined
                   }
               }
       }
  foo.num.num();

5. The call function and the apply function can change the point of this, and the bind function can also change the function point;

6. Constructor module:

Summary : In the constructor, if the return value is a basic data type, then this points to the instance of the constructor; if the return value is an object, then this points to the object;

 function Foo(){           
     this.user = 'my';           
     return {};       
     }       
     var na = new Foo();       
     console.log(na.user);//返回值undefined;
 function Foo(){           
     this.user = 'my';           
     return 1;      
      }       
      var na = new Foo();       
      console.log(na.user);//返回值my

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

Statement:
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