Home > Web Front-end > JS Tutorial > body text

How to use this in js

php中世界最好的语言
Release: 2017-11-17 18:01:21
Original
2551 people have browsed it

What does this in js mean?

This is always used in JS. Today I will talk to you about what this is that can easily confuse people and how to use this.

This points to 1, ordinary function points to the caller of the function: a simple way is to see if there is a dot in front of the function. If there is a dot, then point to the value in front of the dot; 2. The arrow function points to the domain where the function is located: Pay attention to understanding the scope. Only the {} of the function constitutes the scope. The {} of the object and if(){} do not constitute the scope;

const obj = {
name: 'objName',
say() {
console.log(this.name);  
      }, 
read: () => {
     console.log(this.name);
             }
       }
obj.say(); // objName
obj.read(); // undefined
Copy after login


Ordinary function, the caller is obj, so the result is objname; also understand that say() is an ordinary function, with a dot in front, so this points to obj;

arrow Function, this points to the scope where the function is located. The current scope is the global environment, so this.name is undefined.

Take the following example to understand more clearly where the arrow function this points, where the arrow function is located The scope is the ordinary function say, and the caller of say() is obj

const obj = { 
say: function () {
  setTimeout(() => {
   console.log(this)
                  });
                 }
             }
Copy after login


obj.say(); // obj, at this time this refers to the definition of him obj



##Additional knowledge points

The default this of the browser is window

function test() {  
console.log(this);
}
Copy after login

test( ); //The default this for the global environment in window


##node.js

is {}, and the default this for ordinary functions is global


Come on two questions to check whether you have mastered it

example1

const length = 10;
function fn() {
console.log(this.length);
}
const  obj = {
  length: 5,
  method: function(fn) { 
  fn();
  arguments[0]();   
  }
};
obj.method(fn, 1);
Copy after login

Output 10, 2

I just saw this question at the beginning I was also confused about the question, and now I finally understand it. The method function passes in two parameters, one parameter is fn(), fn() is an ordinary function, and this points to the caller of the function, which at this time points to the whole world (you can also See that there is no dot in front of this function), so the running result is 10. arguments are all parameters of the function, which is an object of class

array

, arguments[0] (), which can be regarded as arguments.0() , what calls this function is arguments, at this time this refers to arguments, this.length refers to angument.length, which is the total number of parameters passed in 2


Note: The running result of the above example in the node environment is undefined 2, const length = 10 is changed to global.length = 10; because the

variable

defined globally in the node environment will not be bound to global, and the browser It will also be automatically bound to the global environment windowWhat will be the result if it is changed to the following?

const length = 10;
function fn() {
    console.log(this.length);
}
const obj = {
    length: 5,
    method: function(fn) {
        fn();
        const fun = arguments[0];
        fun();
    }
};
obj.method(fn, 1);
10, 10
example 2
window.val = 1;
var obj = {
    val: 2,
    dbl: function() {
        this.val *= 2
        val *= 2;
        console.log(val);
        console.log(this.val);
    }
}
obj.dbl(); // 2 4
var func = obj.dbl;
func(); // 8 8
Copy after login


This is a bit roundabout, but step by step It is easy to understand when analyzing:

obj.dbl(); When executing this line of code, this refers to obj, so this.val === obj.val*=2, and the final result is 4 ,val*=2 === window.val *= 2, the final result is 2

func(), when executing this line of code, func() does not have any prefix, this refers to window.func( ); So at this time the value of this is window, this.val === window.val *= 2, at this time window.val is 4, val*=2 === window.val *2, the final result is 8, finally console.log(this.val), and console.log(val) both refer to window.val.


The above is my little understanding of this. If you have any questions about this article of mine, please feel free to communicate with me.

Related recommendations:

How to understand JavaScript object-oriented basics and this pointing issues

Detailed explanation of the difference between this and event in JS

Introduction to this, apply, call, and bind in JS

The above is the detailed content of How to use 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!