2

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});
      });
1
  • See the jQuery documentation. It guarantees this will be the DOM element that the click event occurred upon (this is actually how event callbacks -- even "normal ones" -- work in JavaScript in general). Commented Aug 5, 2011 at 20:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Note: this causes memory leaks only in Internet Explorer 7 and earlier. It is not a problem for other browsers, nor IE8+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.