Created
May 7, 2012 13:13
-
-
Save fb55/2627682 to your computer and use it in GitHub Desktop.
with statement in Java vs JS (or: the first time I recognize Java being better than JS)
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
/* | |
In Java, it is possible to drop the `this` in front of instance variables | |
*/ | |
class Example{ | |
private int foo; | |
public Example(int bar){ | |
foo = bar; | |
} | |
public int getFoo(){ | |
return foo; | |
} | |
} |
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
/* | |
In JS, you need to use the `with` statement. Ugly. | |
Why isn't `this` added to the scope chain? | |
Right, because someone could always add properties. | |
*/ | |
var Example = function(bar){ | |
this.foo = bar; | |
}; | |
Example.prototype.getFoo = function(){ | |
with(this){ | |
return foo; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment