Skip to content

Instantly share code, notes, and snippets.

@pguillory
Created October 19, 2012 19:04
Show Gist options
  • Save pguillory/3920044 to your computer and use it in GitHub Desktop.
Save pguillory/3920044 to your computer and use it in GitHub Desktop.
Classify vs Closures
Causes.SomeClass = Causes.classify({
init : function() {
this.value = 0;
$('#something').on('click', this.handlerFunc.bind(this));
},
handlerFunc : function(event) {
event.preventDefault();
this.value += 5;
},
// public methods mixed with private ones
incrementValue : function() {
this.value += 1;
},
getValue : function() {
return this.value;
}
});
Causes.SomeClass = function() {
var value = 0;
$('#something').on('click', handlerFunc);
function handlerFunc(event) {
event.preventDefault();
value += 5;
}
// public methods are distinct
return {
incrementValue : function() {
value += 1;
},
getValue : function() {
return value;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment