How to extend a member variable when extending a jquery object how to implement _jquery

Source: Internet
Author: User
Tags extend
First look at a piece of code:
Copy Code code as follows:

JQuery.fn.extend (
{
Myownmember:3,
Getmyownmember:function () {return this.myownmember;},
Setmyownmember:function (v) {this.myownmember = V; return this.myownmember;}
}
);

$ ("Body"). Myownmember; 3
$ ("Body"). Getmyownmember (); 3
$ ("Body"). Setmyownmember (4); 4
$ ("Body"). Getmyownmember (); 3

This code extends a member Myownmember to the JQuery object, and two function getmyownmember,setmyownmember are used to return and set the Myownmember value respectively. But we saw that Setmyownmember didn't work, and when we getmyownmember again, we returned the original value. What is this for? The reason is that $ ("body") creates a new object each time, so the Myownmember in each $ ("body") is an initial value. If we change the code to:
Copy Code code as follows:

JQuery.fn.extend (
{
Myownmember:3,
Getmyownmember:function () {return this.myownmember;},
Setmyownmember:function (v) {this.myownmember = V; return this.myownmember;}
}
);

var BODY = $ ("body");
Body.myownmember; 3
Body.getmyownmember (); 3
Body.setmyownmember (4); 4
Body.getmyownmember (); 4

That's the effect we want, because $ ("body") is created only once, followed by a reference through the body variable. However, this method in the actual use of the process is still problematic, because I can not be able to refer to the global scope of the body variable, often through the $ ("body") to get the DOM node, so how do we save a jquery object extension variable value? The solution is not to save the variable on a jquery object, but to save it on a DOM node, regardless of how many jquery objects are created on a DOM node, pointing to the same DOM node. So we'll change the code as follows:
Copy Code code as follows:

JQuery.fn.extend (
{
Getmyownmember:function () {return this[0].m Yownmember;
Setmyownmember:function (v) {this[0].myownmember = V; return this[0].myownmember;}
}
);

$ ("body"). Getmyownmember ();//undefined
$ (' body '). Setmyownmember (4);//4
$ ("body"). Getmyownmember ( ); 4

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.