PDA

View Full Version : Explain 'scope'



supermonchi
28 Mar 2011, 5:39 PM
Can anyone please explain what 'scope' does and in which situation would you want to use it?

Sample snippet of code from Kitchen Sink:

this.navigationPanel = new Ext.NestedList({
store: sink.StructureStore,
useToolbar: Ext.is.Phone ? false : true,
updateTitleText: false,
dock: 'left',
hidden: !Ext.is.Phone && Ext.Viewport.orientation == 'portrait',
toolbar: Ext.is.Phone ? this.navigationBar : null,
listeners: {
itemtap: this.onNavPanelItemTap,
scope: this
}
});

jay@moduscreate.com
29 Mar 2011, 4:42 AM
execution scope is basically the object for which an function executes, and defines what 'this' references.

The default execution scope for "var" referenced function is "window".

When you execute a function via a key reference, the default execution scope is the object for which the key belongs.

All that said, JavaScript gives you the ability to force execution scope with a functions' "call" and "apply" methods.

Sencha Touch uses those methods to force execution scope of methods.

Lots of resources on the web, including : http://www.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/

supermonchi
29 Mar 2011, 2:39 PM
Thanks for the explanation and link. I kind of understand what you're saying but I think I need to read more and try some stuff out to really get it. Thanks, much appreciated.

clabasky
13 Sep 2011, 2:44 PM
I'm having similar problems understanding what "scope" is capable of, and how to use it. After reading your post, and the link, I still don't have a clue. Can you give any more advice/references to something that could help? thanks!