Last active
August 29, 2015 14:18
-
-
Save aguestuser/0492f99b711bf5fe8a2d to your computer and use it in GitHub Desktop.
crockford on constructors
This file contains 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
// 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