In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The discussion of this has always been a hot topic. Some people say that if you master this, you will master 80% of JavaScript, which is a bit of an exaggeration, but you can see the importance of this. So far, I have recorded a lot of piecemeal notes about this, so let's make a summary today.
I have seen the simplest explanation for this: whoever calls this function, this points to whom. Okay, it's that simple. Let's talk about it in detail. Please use this statement to verify it at any time.
Let's first take a look at a warm-up of js interview questions:
Var obj = {foo: function () {console.log (this)}} var bar = obj.fooobj.foo () / / objbar () / / window
It is easy to understand that obj.foo () applies the phrase "who calls to point to whom", but why bar () is window. In fact, this can also be applied to this simple statement. Earlier we var a variable bar, please note that this bar is a global variable, that is, bar===window.bar, and assign the function obj.foo to bar, at this point directly call bar (), which is equal to window.bar (). It's easy to understand that this function is called by window, and there's nothing wrong with it. For an explanation of this question, you can also refer to this article: http://mp.weixin.qq.com/s/ksqtCd8ouxU-cVc_HnA4Aw
Keep this sentence in mind: "who calls points to whom?" to find out who called it, it is enough to solve most this problems. Here are a few special cases of this:
1. When the function is called by a specific object, it points to the direct caller. If there is a function nested in this function, the this of the function in it will not point to the caller, but can be changed through that=this. The internal function calls the object property with that.
Var name= "out"; var obj= {name: "in", outFoo:function () {var that=this;// assigns this to that, that of internal function points to obj function inFoo () {this.name; / / out, this points to window that.name; / / in, that points to obj}},}
2. If the function is called through new, an object is created and the this points to the newly created object.
Function Foo () {/ / var this=new Object (); inside the function implicitly creates a this object and returns this this.name= "foo"; / / return this;} var obj=new Foo (); / / assigns the returned this to objconsole.log (obj.name); / / foo
3. The this in the setTimeOut and setInterval functions points to window.
4. When called through apply or call, this dynamically changes its direction. Both methods accept two parameters, the first of which is the object that calls the function. Usage: functionNmae.apply (called object, parameter 2).
5. When calling through bind (), this is similar to the previous two usages, except that bind () statically binds this, which will not change once bound, even with call or apply, and returns a function body to be executed.
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.