Created
May 23, 2012 16:35
-
-
Save benjchristensen/2776246 to your computer and use it in GitHub Desktop.
Javascript Scope Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <title>Javascript Scope Test</title> | |
| <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9"> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> | |
| </head> | |
| <body> | |
| <div id="output"></div> | |
| <script> | |
| // keep everything within this privately namespaced unless specifically published | |
| (function(window) { | |
| function MyObject(name) { | |
| var self = this; | |
| this.publicName = name; | |
| this.getNamePublicFunction1 = function() { | |
| return this.publicName | |
| } | |
| this.getNamePublicFunction2 = function() { | |
| return name | |
| } | |
| this.getPublicVarUsingEventCallback = function() { | |
| if(this.publicName == undefined) { | |
| return log("ERROR: getPublicVarUsingEventCallback => " + this.publicName) | |
| } else { | |
| return log("SUCCESS: getPublicVarUsingEventCallback => " + this.publicName) | |
| } | |
| } | |
| this.getPublicVarViaSelfUsingEventCallback = function() { | |
| if(self.publicName == undefined) { | |
| return log("ERROR: getPublicVarViaSelfUsingEventCallback => " + self.publicName) | |
| } else { | |
| return log("SUCCESS: getPublicVarViaSelfUsingEventCallback => " + self.publicName) | |
| } | |
| } | |
| this.getPrivateVarUsingEventCallback = function() { | |
| if(name == undefined) { | |
| return log("ERROR: getPrivateVarUsingEventCallback => " + name) | |
| } else { | |
| return log("SUCCESS: getPrivateVarUsingEventCallback => " + name) | |
| } | |
| } | |
| this.getSomethingViaPublicFunctionUsingEventCallback = function() { | |
| if(getSomething(name) == undefined) { | |
| return log("ERROR: getSomethingViaPublicFunctionUsingEventCallback => " + getSomething(name)) | |
| } else { | |
| return log("SUCCESS: getSomethingViaPublicFunctionUsingEventCallback => " + getSomething(name)) | |
| } | |
| } | |
| var getNamePrivateFunction = function() { | |
| return name | |
| } | |
| this.getNamePublicViaPrivate = function() { | |
| return getNamePrivateFunction() | |
| } | |
| this.getSomethingViaPublicFunction = function() { | |
| return getSomething(name) | |
| } | |
| } | |
| MyObject.getInstanceVariableFromStaticMethod = function() { | |
| return this.publicName; | |
| } | |
| MyObject.prototype.getInstanceVariableFromPublicMethod = function() { | |
| return this.publicName; | |
| } | |
| // public MyObject | |
| window.MyObject = MyObject; | |
| // this is not published so should remain private | |
| function MyPrivateObject(name) { | |
| } | |
| function getSomething(name) { | |
| return name + "FromSomething" | |
| } | |
| })(window) | |
| // instantiate MyObject | |
| var o1 = new MyObject("one") | |
| log("SUCCESS: MyObject 1: " + o1) | |
| var o2 = new MyObject("two") | |
| log("SUCCESS: MyObject 2: " + o2) | |
| // MyPrivateObject should be undefined | |
| try { | |
| var p1 = new MyPrivateObject("p1") | |
| log("ERROR: MyPrivateObject was publicly accessible: " + p1) | |
| }catch(e) { | |
| log("SUCCESS: MyPrivateObject could not be instantiated because it is private.") | |
| } | |
| if(o1.publicName == 'one') { | |
| log("SUCCESS: o1.publicName: " + o1.publicName) | |
| } else { | |
| log("ERROR: o1.publicName: " + o1.publicName) | |
| } | |
| if(o1.getNamePublicFunction1() == 'one') { | |
| log("SUCCESS: o1.getNamePublicFunction1: " + o1.getNamePublicFunction1()) | |
| } else { | |
| log("ERROR: o1.getNamePublicFunction1: " + o1.getNamePublicFunction1()) | |
| } | |
| if(o1.getNamePublicFunction2() == 'one') { | |
| log("SUCCESS: o1.getNamePublicFunction2: " + o1.getNamePublicFunction2()) | |
| } else { | |
| log("ERROR: o1.getNamePublicFunction2: " + o1.getNamePublicFunction2()) | |
| } | |
| try { | |
| o1.getNamePrivateFunction() | |
| log("ERROR: o1.getNamePrivateFunction() was publicly accessible: " + p1) | |
| }catch(e) { | |
| log("SUCCESS: o1.getNamePrivateFunction() could not be invoked because it is private.") | |
| } | |
| if(o1.getNamePublicViaPrivate() == 'one') { | |
| log("SUCCESS: o1.getNamePublicViaPrivate: " + o1.getNamePublicViaPrivate()) | |
| } else { | |
| log("ERROR: o1.getNamePublicViaPrivate: " + o1.getNamePublicViaPrivate()) | |
| } | |
| try { | |
| getSomething("hello") | |
| log("ERROR: getSomething() was publicly accessible") | |
| }catch(e) { | |
| log("SUCCESS: getSomething() could not be invoked because it is private.") | |
| } | |
| if(o1.getSomethingViaPublicFunction() == 'oneFromSomething') { | |
| log("SUCCESS: o1.getSomethingViaPublicFunction: " + o1.getSomethingViaPublicFunction()) | |
| } else { | |
| log("ERROR: o1.getSomethingViaPublicFunction: " + o1.getSomethingViaPublicFunction()) | |
| } | |
| if(MyObject.getInstanceVariableFromStaticMethod() != undefined) { | |
| log("ERROR: MyObject.getInstanceVariableFromStaticMethod() should not be able to access instance variables.") | |
| } else { | |
| log("SUCCESS: getInstanceVariableFromStaticMethod() could not access instance variable.") | |
| } | |
| if(o1.getInstanceVariableFromPublicMethod() == "one") { | |
| log("SUCCESS: o1.getInstanceVariableFromPublicMethod: " + o1.getInstanceVariableFromPublicMethod()) | |
| } else { | |
| log("ERROR: o1.getInstanceVariableFromPublicMethod: " + o1.getInstanceVariableFromPublicMethod()) | |
| } | |
| setTimeout(o1.getPublicVarUsingEventCallback,50) | |
| setTimeout(function() { | |
| o1.getPublicVarUsingEventCallback() | |
| },50) | |
| setTimeout(o1.getPublicVarViaSelfUsingEventCallback,50) | |
| setTimeout(function() { | |
| o1.getPublicVarViaSelfUsingEventCallback() | |
| },50) | |
| setTimeout(o1.getPrivateVarUsingEventCallback,50) | |
| setTimeout(function() { | |
| o1.getPrivateVarUsingEventCallback() | |
| },50) | |
| setTimeout(o1.getSomethingViaPublicFunctionUsingEventCallback,50) | |
| setTimeout(function() { | |
| o1.getSomethingViaPublicFunctionUsingEventCallback() | |
| },50) | |
| function log(text) { | |
| $("#output").html($("#output").html() + text + "<br>") | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment