Accessing variables across different scopes in Javascript & YUI 3 <p>I'm new to YUI 3 and YUI in general. I'm trying to pass data between classes and methods.</p> <p>In this example I'm using the <strong>gallery-sm-treeview</strong> plugin to build a file tree. There's a class named <strong>TreeView</strong> to initialize and render the tree. There's another class named <strong>MenuBar</strong> where I want to access some of the plugin-specific methods through <strong>TreeView</strong>'s getter method.</p> <p>However, the variable <strong>var treeview</strong> inside the YUI().use() scope is of course not accessible from outside. How to do it?</p> <pre><code>YUI.add('treetool.TreeView', function(Y) { Y.treetool.TreeView = Class.extend({ init : function(elementId) { YUI({ gallery: 'gallery-2013.06.20-02-07'}).use('gallery-sm-treeview', function (Y) { // Create a new TreeView with a few nodes. var treeview = new Y.TreeView({ // Tell the TreeView where to render itself. container: elementId, // Populate the treeview with some tree nodes. nodes: [ {label: 'Node 1'}, {label: 'Node 2', children: [ {label: 'Child 1'}, ] }); // Render the treeview inside the #treeview element. treeview.render(); }); }, getSomeData : function () { return treeview.getSelectedNodes(); } }); }, '0.0.1', { requires : [ 'jquery' ] }); </code></pre> <p>and</p> <pre><code>YUI.add('treetool.MenuBar', function(Y) { Y.treetool.MenuBar = Class.extend({ init : function(treeObj) { var someData = treeObj.getSomeData(); }, }); }, '0.0.1', { requires : [ 'jquery' ] }); </code></pre>