Home > Web Front-end > JS Tutorial > How to use this in Javascript

How to use this in Javascript

jacklove
Release: 2018-05-21 15:19:37
Original
1372 people have browsed it

In daily study, we often encounter the usage of this. This article will explain it in detail.

You may have encountered this JS interview question:

var obj = {  foo: function(){    console.log(this)
  }
}var bar = obj.foo
obj.foo() // 打印出的 this 是 objbar() // 打印出的 this 是 window
Copy after login

Please explain why the values ​​of the functions in the last two lines are different.

——-

Beginners’ understanding of this has always been vague. Today’s article will explain it all once and for all.

And you can’t see this explanation anywhere else. Understand this article, all interview questions about this are just side dishes.

Function call

First of all, we need to start with the function call.

There are three function call forms in JS (ES5):

func(p1, p2)
obj.child.method(p1, p2)
func.call(context, p1, p2) // 先不讲 apply
Copy after login

Generally, beginners know the first two forms and think that the first two forms are "better" than the third form.

From reading this article, you must remember that the third calling form is the normal calling form:

func.call(context, p1, p2)

The other two are syntax sugar and can be equivalently changed into call form:

func(p1, p2) is equivalent to func.call(undefined, p1, p2)obj .child.method(p1, p2) is equivalent to obj.child.method.call(obj.child, p1, p2)

Please note it down. (We call this code "conversion code" for easy reference below)

So far our function call has only one form:

func.call(context, p1, p2)

In this way, this can explain

this, which is the context in the above code. It's that simple.

This is the context passed when you call a function. Since you never use the call form of function call, you never know it.

Let’s first look at how to determine this in func(p1, p2):

When you write the following code

function func(){ console.log(this)}func()
等价于
function func(){ console.log(this)}func.call(undefined) // 可以简写为 func.call()
Copy after login

Logically speaking, the printed this should be undefined, right? , but there is a rule in the browser:

If the context you pass is null or undefined, then the window object is the default context (the default context in strict mode is undefined)
So the above print result is window.

If you hope that this here is not window, it is very simple:

func.call(obj) // Then this inside is the obj object.

Look at obj again. How to determine this of child.method(p1, p2)

var obj = { foo: function(){   console.log(this)
 }
}
obj.foo()
Copy after login

According to "Conversion Code", we convert obj.foo() to

obj.foo.call(obj)

Okay, this is obj. Done.

Back to the topic:

var obj = { foo: function(){   console.log(this)
 }
}var bar = obj.foo
Copy after login

obj.foo() // Convert to obj.foo.call(obj), this is objbar()// Convert to bar.call()/ / Since no context is passed// so this is undefined// Finally the browser gives you a default this - window object

[ ] Syntax

function fn (){ console.log(this) }
var arr = [fn, fn2]
arr0 // 这里面的 this 又是什么呢?
Copy after login

We can imagine arr0 is arr.0(). Although the syntax of the latter is wrong, the form corresponds to obj.child.method(p1, p2) in the conversion code, so you can convert happily:

arr0
Imagine it as arr.0()
and then convert it to arr.0.call(arr)
Then this inside is arr:)

Summary

this is the context passed in when you call a function.
If your function call form is not call form, please follow the "Conversion Code" to convert it to call form.

In the future, you will have no doubts about all written test questions related to this.

This article provides a related explanation of this. For more related content, please pay attention to the php Chinese website.

Related recommendations:

About Math, arrays, Dat

HTML5/CSS3 related knowledge explanation# Related examples of ##e

Summary of common APIs used by Javascript to operate DOM

The above is the detailed content of How to use this in Javascript. 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