Skip to content

Instantly share code, notes, and snippets.

@fb55
Created May 7, 2012 13:13
Show Gist options
  • Save fb55/2627682 to your computer and use it in GitHub Desktop.
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)
/*
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;
}
}
/*
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