Use closure like
var settings = function(){ var private = {}; private.slides = '#slides'; var public = {} public.inDent = ($(private.slides).find('li').outerWidth()); public.doSomething = function(){ console.log( public.inDent ); } return public; }();
The advantage of this is that it gives you "encapsulation" for free
By the way, you should not rely on anything publicly available, because maybe it can be changed, for example. settings.inDent = null , and then settings.doSomething() may not work correctly. What is the right way to do this:
... private.inDent = ($(settings.slides).find('li').outerWidth()); public.inDent = private.inDent; public.doSomething = function(){ console.log( private.inDent ); }
i.e. make the inDent only inDent value (in a sense, that nothing outside the settings object can actually change the internal implementation of private.inDent ); as long as you always use private.inDent from settings , you will be safe, because even if someone does settings.inDent = null , settings.doSomething(); will function properly
source share