DrStrangeLove
DrStrangeLove

Reputation: 11557

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});
      });

Upvotes: 2

Views: 75

Answers (2)

rav
rav

Reputation: 696

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.

Upvotes: 1

Marc B
Marc B

Reputation: 360702

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.

Upvotes: 1

Related Questions