In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use this pointing in JavaScript. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
This is also an amazing thing in JavaScript, representing a current object reference in object-oriented (such as java), but this is not fixed in JavaScript, but changes with the running environment.
This
The old rule is to look at the code:
Function test () {console.log (this);} in the method
Function test () {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.
The above is all the content of the article "how to use this in JavaScript". Thank you for reading! Hope to share the content to help you, more related 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.