Why do they use references instead of jQuery objects?
In 开发者_运维技巧this tutorial, they use containerElement.rating and starElement.rating for storing rating.
The question is : Why?? is it possible to use jQuery objects container and star like
container.rating and star.rating??
In their example, How does it work:
star.click(function() {
containerElement.rating = this.rating;//What does 'this' refer to?? Star or starElement??
elements.triggerHandler("ratingchanged", {rating: this.rating});
});
It's doable/allowable, but not recommended. Nothing says that jquery couldn't suddenly decide to add a .star
attribute at some point in the future.
If you need to attach your own data to a dom element, then use someelement.data(key, val)
instead, which adds your data in a method guaranteed to not conflict with any future changes to the DOM specs.
It is possible to store objects like you're saying, but I believe this tutorial creates some circular references (JS->DOM->JS) which is bad. They should be using jQuery's .data() function to avoid circular references, which cause memory leaks.
精彩评论