Created
February 20, 2012 15:24
-
-
Save nulltask/1869660 to your computer and use it in GitHub Desktop.
Pseudo private function using `Function.prototype.bind`
This file contains hidden or 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
| /** | |
| * User constructor. | |
| */ | |
| function User(firstName, lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| } | |
| /** | |
| * Set `User` prototype functions. | |
| */ | |
| !function(proto) { | |
| /** | |
| * Returns full name. (pseudo private function) | |
| * | |
| * @return {String} Full name | |
| * @api private | |
| */ | |
| var _buildFullname = (function() { | |
| return this.firstName + " " + this.lastName; | |
| }).bind(proto); | |
| /** | |
| * Returns initial. (pseudo private function) | |
| * | |
| * @param {String} firstName | |
| * @param {String} lastName | |
| * @return {String} Initial | |
| * @api private | |
| */ | |
| var _buildInitial = (function() { | |
| return this.firstName.slice(0, 1) + "." + this.lastName.slice(0, 1) + "."; | |
| }).bind(proto); | |
| /** | |
| * Full name getter function. | |
| * | |
| * @return {String} Full name. | |
| * @api public | |
| */ | |
| proto.getFullname = function() { | |
| return _buildFullname(); | |
| }; | |
| /** | |
| * Initial getter function. | |
| * | |
| * @return {String} | |
| * @api public | |
| */ | |
| proto.getInitial = function() { | |
| return _buildInitial(); | |
| }; | |
| }(User.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment