What's wrong with the $ (this) of jQuery?

Source: Internet
Author: User

Did you have a problem writing jquery recently?



I was surprised to find that the effect of the "one Day" button was not removed. jquery is out of the question?

I did not hesitate to ask myself such a question, and then I vetoed it. Why? Because I think I think more ...

But it's a matter to understand.

But the question is below, why does the settimeout () below not work?

I can't think of the solution.

So, I printed something like this in the anonymous function of settimeout.

Cosole.log (This = = = window);

Return value True//////? What.

How can this, I was writing to play ...

So I took a closer look.

Originally

In the traditional OnEvent property code, this reference receives the event element---but only in the property, not in the function called by the slave.

What does that mean? It is indeed in our click. But if you call the closure function inside, the This object again points back to our window object.

How to solve this problem ... Well done.

<span style= "FONT-SIZE:18PX;" ><script type= "Text/javascript" >$ (function () {$ (' a '). Click (Function (event) {$this = $ (this); $this. addclass (' clicked '); SetTimeout (function () {Console.log (this = = window), $this. Removeclass (' clicked ');//2},3000);}); </script></span>

That's right. Save $ (this) as a local variable: So why is it possible to copy this or $ (this) to a local variable to solve this problem?

JavaScript creates a closure for local variables of the parameter kernel function.

Closures can be summed up as a 4 of the content.

1 You can nest another function in a JavaScript function, and nesting can be multilevel.

The 2 function can not only read its own parameters and local variables, but also read and write the variables in the nested function.

3 even if an external function has returned, it is also valid to invoke the inner function. Like settimeout.

4 whether anonymous or named functions are the same. This is a special JavaScript keyword, so none of these principles apply, and by copying the value of this to a local variable, you can use the closure to make the value available in any nested function.


Best Wishes.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.