4

Is there any issue doing the following....

var myObject = {
 name: "Johnny",
 init: function() {
 $("body").mousemove(this.setStatus);
 },
 setStatus: function(ev) {
 $("body").append("<div>Mouse Move by: " + myObject.name + "</div>");
 }
};
myObject.init();

Is this the best way of referencing an object's property when a class is created in this way (i.e. calling myObject.name from the setStatus function)?

tshepang
12.5k25 gold badges98 silver badges140 bronze badges
asked Dec 1, 2011 at 9:26

4 Answers 4

2

It looks like you're aware that when this.setStatus is triggered the context variable this will refer to the element that received the event (i.e. your <body>) rather than to myObject.

However you really shouldn't refer to myObject within the object itself - an object should have no knowledge of what it's being called outside of itself.

To resolve that you can pass this as an additional parameter to .mouseMove thus:

$("body").mousemove(this, this.setStatus);

and then within your event handler you can retrieve your current object reference via the event's data field:

setStatus: function(ev) {
 var self = ev.data;
 $("body").append("<div>Mouse Move by: " + self.name + "</div>");
}
answered Dec 1, 2011 at 9:37
Sign up to request clarification or add additional context in comments.

4 Comments

Why shouldn't the object reference itself? Is there a technical reason why you're advising this?
@JohnnyRambo it's bad OO design. You could currently do var tmp = myObject; myObject = null and your code would break. The change I've suggested isolates the internals of your implementation from the external references by which your object is known and would allow the code to keep working.
I figured that but the reason I created the object in this way was because there won't be any other instances of the class, nor will it be set to null. I will still make the change you're suggesting though as its safer. Thanks.
@JohnnyRambo you're welcome! FWIW, it's better for code maintainability too. If you decide to rename myObject you'd no longer have to change any of the internal implementation.
1

This is perfectly fine. If setStatus isn't used anywhere else, you can move it to an anonymous function and pass it to mousemove.

answered Dec 1, 2011 at 9:29

Comments

0

You need to ensure that the this is still available. When passing the function to an event handler, this will point to the related DOM node.

Since you are using jQuery, you can use $.proxy() to bind this inside the function to something else:

$("body").mousemove($.proxy(this.setStatus, this));

However, it would be cleaner to make the original this available by other means:

var myObject = {
 name: "Johnny",
 init: function() {
 var self = this;
 $("body").mousemove(function(ev) {
 self.setStatus.call(this, ev, self);
 });
 },
 setStatus: function(ev, self) {
 $("body").append("<div>Mouse Move by: " + self.name + "</div>");
 }
};
answered Dec 1, 2011 at 9:41

1 Comment

that works, but then breaks the convention that this in an event handler refers to the element that received the event.
0

That way is fine. Another way of doing it would be to just pass an anonymous function to the mousemove:

var myObject = {
 name: "Johnny",
 init: function() {
 var thisObject = this;
 $("body").mousemove(function() {
 $("body").append("<div>Mouse Move by: " + thisObject.name + "</div>");
 });
 }
};
myObject.init();

See fiddle

answered Dec 1, 2011 at 9:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.