Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of this pointing in JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis pointed to by this in JavaScript, I believe that most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

This

Look at the code first:

Function test () {console.log (this);} in the method

Person= {name: "Zhang San", eat:function () {console.log (this)}} in object

In a method, this represents the object to which the method belongs. Because the first is a method on window, the window is printed, and the eat method is the Person method, so it prints except for the object Person.

So you can see that this is used separately on the console to represent global objects.

Hidden this

In the object, you can declare one in advance:

Var Person1= {name: "Zhang San", age:18} var Person2= {name: "Li Si", age:19}

It can be troublesome to write like this, so you can learn from the concept of the java class, like this:

Var Person=function (name,age) {this.name=name, this.age=age} var Person1=new Person (Zhang San, 18); var Person2=new Person (Li Si, 19)

In fact, a return this is hidden in new, and if you do not use new, you will find that it does not return the newly created object.

So now let's make it up and see:

Var Person=function (name,age) {this.name=name, this.age=age return this;} var Person1=new Person (Zhang San, 18); var Person2=new Person (Li Si, 19)

In this way, you can even fake the effect of a this:

Var Person=function (name,age) {var that= {}; that.name=name, that.age=age return that;} var Person1=new Person (Zhang San, 18); var Person2=new Person (Li Si, 19)

Strict mode

This has some magical situations in strict mode and non-strict mode.

Function test () {return this;} # if adding "use strict" before js represents strict mode "use strict"; function test () {return this;}

This shows that in a function in non-strict mode, the owner of the function is bound to this by default. So you can print out the global, but in strict mode the function is not bound to this, when this is undefined.

Can change the direction of the this

Look at the code first.

Var Person=function (name,age) {this.name=name, this.age=age, this.fun=function () {console.log ("print", this.name);} var Person1=new Person ("Zhang San", 18); var Person2=new Person ("Li Si", 19)

You can see the value of an in the window that this points to instead of the value in the method test, but some keywords can be modified to point to.

You can see who the object in front of the method is, who is calling this, but it can be modified, such as using the keywords call, apply, bind.

Looking at the words above, you can see that call and apply are very similar, while bind does not execute the function immediately, it needs to be executed with ().

However, if you take the parameters, you will find that call and apply are still different, but both of them must be objects. After all, the point of this is the object.

Keyword directly calls the method parameter call will run automatically all the methods can be formatted as follows: obj1.obj1Fun.call (obj2, parameter 1, parameter 1...) The methods used by apply can be formatted as follows: obj1.obj1Fun.apply ([obj2, parameter 1, parameter 1 …]) Comparing the parameters of call with [] around the methods that bind will not run automatically, you need to follow + (). Calls can be made with the parameter format as follows: all can be formatted as follows: obj1.obj1Fun.bind ([obj2, parameter 1, parameter 1 …]) (); the parameters of apply are the same, except that it needs to be followed by + () to call all the contents of the above article "sample Analysis pointed to by this in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report