Skip to content

Instantly share code, notes, and snippets.

@stevobengtson
Last active August 29, 2015 14:08
Show Gist options
  • Save stevobengtson/c876d7f4593845b08b2e to your computer and use it in GitHub Desktop.
Save stevobengtson/c876d7f4593845b08b2e to your computer and use it in GitHub Desktop.
Nested Namespace Example
<html>
<head>
<script type="application/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<script type="application/javascript">
(function( VT, $, undefined ) {
VT.whoami = "VT";
VT.testFunc = function() {
console.log('test function from VT');
vtPrivate();
console.log('I can access ' + test + ' variable.');
};
var test = "Private";
function vtPrivate() {
console.log('This is a ' + test + ' function for VT.');
}
}( window.VT = window.VT || {}, jQuery ));
(function( Test, $, undefined ) {
Test.whoami = "Test";
Test.testFunc = function() {
console.log('test function from VT.Test');
testPrivate();
console.log('I can access ' + test + ' variable.');
VT.testFunc();
};
Test.testTwoFunc = function() {
console.log('test two function from VT.Test');
};
var test = 'Test Private';
function testPrivate() {
console.log('This is a ' + test + ' function for Test.');
}
}( window.VT.Test = window.VT.Test || {}, jQuery ));
$(function () {
VT.testFunc();
VT.Test.testFunc();
VT.Test.testTwoFunc();
console.error(VT.test);
console.error(VT.whoami);
console.error(VT.vtPrivate());
console.error(VT.Test.test);
console.error(VT.Test.whoami);
console.error(VT.Test.testPrivate());
});
</script>
</body>
</html>
<!--
Console output:
[Log] test function from VT (test.html, line 12)
[Log] This is a Private function for VT. (test.html, line 19)
[Log] I can access Private variable. (test.html, line 14)
[Log] test function from VT.Test (test.html, line 27)
[Log] This is a Test Private function for Test. (test.html, line 39)
[Log] I can access Test Private variable. (test.html, line 29)
[Log] test function from VT (test.html, line 12)
[Log] This is a Private function for VT. (test.html, line 19)
[Log] I can access Private variable. (test.html, line 14)
[Log] test two function from VT.Test (test.html, line 34)
[Error] undefined
(anonymous function) (test.html, line 48)
j (jquery-1.11.1.min.js, line 2)
fireWith (jquery-1.11.1.min.js, line 2)
ready (jquery-1.11.1.min.js, line 2)
J (jquery-1.11.1.min.js, line 2)
[Error] VT
(anonymous function) (test.html, line 49)
j (jquery-1.11.1.min.js, line 2)
fireWith (jquery-1.11.1.min.js, line 2)
ready (jquery-1.11.1.min.js, line 2)
J (jquery-1.11.1.min.js, line 2)
[Error] TypeError: undefined is not a function (evaluating 'VT.vtPrivate()')
(anonymous function) (test.html, line 50)
j (jquery-1.11.1.min.js, line 2)
fireWith (jquery-1.11.1.min.js, line 2)
ready (jquery-1.11.1.min.js, line 2)
J (jquery-1.11.1.min.js, line 2)
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment