Created
December 17, 2014 13:43
-
-
Save stoeffel/bfc344fb218f4af1a39c to your computer and use it in GitHub Desktop.
var self = this; // source http://jsbin.com/mafofa
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> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| var l = function(func) { | |
| console.log(func()); | |
| }; | |
| function FooClass(foo) { | |
| self = this; // var is missing | |
| self.foo = foo; | |
| setTimeout(function() { | |
| console.log(self.foo + ' always moo, never bar'); | |
| },500); | |
| } | |
| console.log(window.self.open); | |
| // both Foos share the same foo attribute | |
| new FooClass('bar'); | |
| new FooClass('moo'); | |
| console.log('window.self ' + self.foo); // foo is now global | |
| console.log(self.open); // window.self is gone... | |
| // http://stackoverflow.com/questions/337878/var-self-this | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">var l = function(func) { | |
| console.log(func()); | |
| }; | |
| function FooClass(foo) { | |
| self = this; // var is missing | |
| self.foo = foo; | |
| setTimeout(function() { | |
| console.log(self.foo + ' always moo, never bar'); | |
| },500); | |
| } | |
| console.log(window.self.open); | |
| // both Foos share the same foo attribute | |
| new FooClass('bar'); | |
| new FooClass('moo'); | |
| console.log('window.self ' + self.foo); // foo is now global | |
| console.log(self.open); // window.self is gone... | |
| // http://stackoverflow.com/questions/337878/var-self-this</script></body> | |
| </html> |
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
| var l = function(func) { | |
| console.log(func()); | |
| }; | |
| function FooClass(foo) { | |
| self = this; // var is missing | |
| self.foo = foo; | |
| setTimeout(function() { | |
| console.log(self.foo + ' always moo, never bar'); | |
| },500); | |
| } | |
| console.log(window.self.open); | |
| // both Foos share the same foo attribute | |
| new FooClass('bar'); | |
| new FooClass('moo'); | |
| console.log('window.self ' + self.foo); // foo is now global | |
| console.log(self.open); // window.self is gone... | |
| // http://stackoverflow.com/questions/337878/var-self-this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment