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

The pointing meaning of this object

一个新手
Release: 2017-09-25 10:40:45
Original
1442 people have browsed it

1. In the function:

function foo(){
   return this;
}
Copy after login

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();
Copy after login

(2).

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

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
Copy after login

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!

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