Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define a closure to wrap all your code
- (function() {
- // Private var. This var is not accesible outside closure.
- var privateVar1 = "test value";
- // Private function. This function is only accesible inside this closure.
- function privateFunction1() {
- var localVar = privateVar1 + "aaa";
- }
- /** We use window global object to export this function
- * to global context and do it public.
- * One problem with this, is that outside we can override
- * ref to this function doing:
- * window.publicFunction2 = otherFunctionOrWhatever;
- **/
- window.publicFunction2 = function(arg1, arg2) {
- // We can use here, private vars and functions
- var localVar2 = privateFunction1() + privateVar1;
- //...
- }
- /** We use window global object to export this variable
- * to global context and do it public.
- * This variable is read-writeable
- **/
- window.publicVar2 = "test value 2";
- /** Example of exporting a module-like.
- * A module is a set of functions and classes that
- * are related and it access following way:
- * moduleName.function(...)
- **/
- window.moduleName = {
- /**
- * Example of function in module.
- **/
- function1: function(arg1, arg2, arg3) {
- //...
- // 'this' is whole module
- this.function1(3, 4, 5);
- },
- /**
- * Example of a class definition inside a module
- **/
- clase1: function Clase1(arg1) {
- // create instance outside:
- // var objClase1 = new moduleName.clase1(3);
- // @see OOP.js to see how to define a class
- }
- }
- })();
- // Public function. This function is accesible wherever
- function publicFunction3() {
- // We can use public functions and vars for example
- // from closure
- var localVar3 = publicFunction2(2, 3) + publicVar2
- + publicVar3;
- //...
- }
- var publicVar3 = "test value 3";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement