4 ms·
I agree. For small projects I use the same pattern but in the global namespace. I normally also create an intentional closure when initiating the object, this w
by aarongough 17y ago
I agree. For small projects I use the same pattern but in the global namespace. I normally also create an intentional closure when initiating the object, this way I can refer to the parent object regardless of whether it's methods have been copied away or not (like when you assign an object method to an event handler)
var testObject = function()
{
// Make sure the 'this' object is always available
// to the object's methods via the 'thisObj' variable
var thisObj = this;
this.publicVar = "This is a string";
this.alertBox = function()
{
alert( thisObj.publicVar );
}
}
object1 = new testObject();
window.onload = object1.alertBox
- monos 17y agoI know what you mean. When I have lots of event-handlers in a prototype I like to assign var self = this; but that's bikeshedding
- aarongough 17y agoWhat do you mean by 'bikeshedding'?
- monos 17y agohttp://catb.org/jargon/html/B/bikeshedding.html http://catb.org/jargon/html/B/bikeshedding.html wether we should call it thisObj or self :)
- aarongough 17y agoAh gotcha. I checked the definition at a few places but the context still seemed unclear. I agree, one way or the other is fine. Do you use 'self' because you're a Ruby coder? That actually the reason I would stay away from it! But then again I'm easily confused :-p