I’m writing a lot of JavaScript widgets and small apps using the Object Literal pattern.
One thing I do is inside one of the methods I will abbreviate the application name to “app.”
I do this primarily because it’s easier to write, and also I understand there is a small performance gain.
var ApplicationName = {
property : "foo",
methodOne : function(){
var app = ApplicationName;
return app.property;
},
init: function(){
var app = ApplicationName;
app.methodOne();
}
}
ApplicationName.init();
Are there any issues with this approach?
I don’t think there’s anything wrong with your approach, but it’s not strictly necessary (and you can shorten it even further). Calling
initlike this:…sets the value of
thisinsideinittoApplicationName. Which allows you to do this: