Skip to content

Instantly share code, notes, and snippets.

@nulltask
Created February 20, 2012 15:24
Show Gist options
  • Select an option

  • Save nulltask/1869660 to your computer and use it in GitHub Desktop.

Select an option

Save nulltask/1869660 to your computer and use it in GitHub Desktop.
Pseudo private function using `Function.prototype.bind`
/**
* 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