Skip to content

Instantly share code, notes, and snippets.

@aguestuser
Last active August 29, 2015 14:18
Show Gist options
  • Save aguestuser/0492f99b711bf5fe8a2d to your computer and use it in GitHub Desktop.
Save aguestuser/0492f99b711bf5fe8a2d to your computer and use it in GitHub Desktop.
crockford on constructors
// pseudo-classical v1
var Mammal = function(name){
this.name = name;
this.get_name = function(){
return this.name;
};
this.purr = function() {
return "purr"
};
}
var myMammal = new Mammal('Icarus');
// "pseudo-classical" v2
var Mammal = function (name) {
this.name = name;
};
Mammal.prototype.get_name = function () {
return this.name;
};
Mammal.prototype.says = function () {
return 'purr';
};
var myMammal = new Mammal('Icarus');
// functional
var mammal = function (spec) {
var that = {};
that.get_name = function () {
return spec.name;
};
that.says = function () {
return 'purr';
};
return that;
};
var myMammal = mammal({name: 'Herb'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment