In this link: http://css-tricks.com/snippets/jquery/jquery-plugin-template/ it has a line of code that says
// Add a reverse reference to the DOM object
base.$el.data("yourPluginName", base);
what does the “reverse reference to the DOM object” mean?
Assuming that you know the jQuery data function:
It’s storing a reference to the instance of the class in the data cache of jQuery, meaning that the stored instance can be used to access the initial
baseobject if it in the current context is not available.This way, the class instance can be used later. However, the use of the
prototypekeyword upon the initial class that the instance were created from will modify the instance.EDIT:
Ooops, it seems that Anurag is right, and I was giving wrong information.
Sorry, the information I gave in initial answer was not completely correct. I’ve updated the answer, so it now tells the truth.
In the comments you’re asking:
It seems that none of the statements are correct.
As I did obviously not remember adequately, the thing stored in
datais only a reference to the object:BTW, JavaScript variables with names like this
base-thing (but, more often seen asthator similar) are typically used to represent the current context, accessed through thethiskeyword, which on many occasions is more easy to store in another variable due to scoping/context changes, that will make the current context and thereforethis, change.Also due to issues with context, the stored value in
datacould be used to access the specific object instance from another context (that is, whenthisrepresents something else), instead of the version of thebaseobject that was continually used after a copy of it was stored.I hope this answered you questions 😀