Skip to content

Instantly share code, notes, and snippets.

@stoeffel
Created December 17, 2014 13:43
Show Gist options
  • Select an option

  • Save stoeffel/bfc344fb218f4af1a39c to your computer and use it in GitHub Desktop.

Select an option

Save stoeffel/bfc344fb218f4af1a39c to your computer and use it in GitHub Desktop.
var self = this; // source http://jsbin.com/mafofa
<!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>
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