jquery's $.proxy () application example describes _jquery

Source: Internet
Author: User
Today, looking at << sharp jquery>> saw the use of proxy (), feeling very vague, looking for information everywhere.



jquery's source is not clear.

But finally understand the use of proxy;
Copy Code code as follows:

<input type= "button" value= "Test" id= "guobtn" name= "I am the name of the button"/>

Copy Code code as follows:

var obj = {
Name: "I am the name of obj",
Sayname:function () {
alert (this.name);
}
}
$ ("#guoBtn"). Click (obj.sayname); I'm the name of the button
What if I want to access the name of obj?
$ ("#guoBtn"). Click ($.proxy (Obj.sayname,obj));//"I am the name of obj"
$ ("#guoBtn"). Click ($.proxy (obj, "sayname")); "I am the name of obj"

It can be seen from the use of the above proxy (a,b) that his parameters are written in two ways.

The first: A is a function, B is the object owner of the function.

The second: A is an object, B is a string, and is the property name of a.

And this example is an example of a << sharp jquery>>.
Copy Code code as follows:

<div id= "Panel" style= "Display:none;" >
<button>Close</button>
</div>

Copy Code code as follows:

$ ("#panel"). FadeIn (function () {
$ ("#panel button"). Click (function () {
$ (this). Fadeout ();
});
});


The button disappeared, but the panel did not disappear. You can use proxy to solve this problem.
Copy Code code as follows:

$ ("#panel"). FadeIn (function () {
var obj = this;
$ ("#panel button"). Click ($.proxy () (function () {
$ (this). Fadeout ();
}, obj));
});

The Panel does not disappear after you click the button.

Personal feeling proxy is the most important way to modify the context object when the function is executed.

is the encapsulation on the basis of apply, so the proxy is our own apply to jquery.

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.