Created
February 17, 2012 11:34
-
-
Save nulltask/1852850 to your computer and use it in GitHub Desktop.
JavaScript pseudo private function
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
| body { | |
| font-family: "Helvetica Neue", "Arial"; | |
| } |
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
| <h1>Log</h1> | |
| <ul id="log"></ul> |
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 | |
| */ | |
| function _buildFullname() { | |
| return this.firstName + " " + this.lastName; | |
| } | |
| /** | |
| * Returns initial. (pseudo private function) | |
| * | |
| * @param {String} firstName | |
| * @param {String} lastName | |
| * @return {String} Initial | |
| * @api private | |
| */ | |
| function _buildInitial(firstName, lastName) { | |
| return firstName.slice(0, 1) + "." + lastName.slice(0, 1) + "."; | |
| } | |
| /** | |
| * Full name getter function. | |
| * | |
| * @return {String} Full name. | |
| * @api public | |
| */ | |
| proto.getFullname = function() { | |
| return _buildFullname.call(this); | |
| }; | |
| /** | |
| * Initial getter function. | |
| * | |
| * @return {String} | |
| * @api public | |
| */ | |
| proto.getInitial = function() { | |
| return _buildInitial(this.firstName, this.lastName); | |
| }; | |
| }(User.prototype); | |
| /** | |
| * Usage. | |
| */ | |
| $(function() { | |
| /** | |
| * Logger | |
| * | |
| * @param {String} message | |
| */ | |
| function log(message) { | |
| if (console) { | |
| console.log(message); | |
| } | |
| $('#log').append($('<li>' + message + '</li>')); | |
| } | |
| log('=== start ==='); | |
| var user = new User('Seiya', 'Konno'); | |
| log(user.getFullname()); | |
| log(user.getInitial()); | |
| try { | |
| log(user._buildFullname()); // can't call pseudo private function. | |
| } catch (e) { | |
| log(e.message); | |
| } | |
| log('=== finish ==='); | |
| }); |
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
| name: pseudo private function | |
| description: pseudo private function | |
| authors: | |
| - Seiya Konno |
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
| === start === | |
| Seiya Konno | |
| S.K. | |
| Object # has no method '_buildFullname' | |
| === finish === |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/gh/gist/jquery/1.7.1/1852850/