Skip to content

Instantly share code, notes, and snippets.

@yangsu
Created January 29, 2013 21:09
Show Gist options
  • Save yangsu/4667892 to your computer and use it in GitHub Desktop.
Save yangsu/4667892 to your computer and use it in GitHub Desktop.
JavaScript Function Invocation - Constructor
// this refers to the newly created object returned by new
var Counter = function (init) {
this.value = init || 0;
}
Counter.prototype.increment = function (inc) {
inc = inc || 1;
this.value += inc;
}
var anotherObj = new Counter();
var bad = Counter(); // Don't do this, value gets attached to the global object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment