Created
January 9, 2014 09:56
-
-
Save prog-mitsu/8331911 to your computer and use it in GitHub Desktop.
「JavaScriptパターン」ピックアップ ref: http://qiita.com/ms32/items/b84b62182854fd622d5b
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
| // for-inループ | |
| var man = { | |
| hands: 2, | |
| legs: 2, | |
| heads: 1 | |
| }; | |
| for (var i in man) { | |
| if (man.hasOwnProperty(i)) { // フィルタ | |
| console.log(i, ":", man[i]); | |
| } | |
| } |
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
| function looper() { | |
| var i = 0, | |
| max, | |
| myarray = []; | |
| // ... | |
| for (i=0, max = myarray.length; i < max; i += 1){ | |
| // myarray[i]に対する処理 | |
| } | |
| } |
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
| var inspect_me = 0, | |
| result = ''; | |
| switch (inspect_me) { | |
| case 0: | |
| result = "zero"; | |
| break; | |
| case 1: | |
| result = "one;" | |
| break; | |
| default: | |
| result = "unknown"; | |
| } |
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
| function outer(a, b) { | |
| var c = 1, | |
| d = 2, | |
| inner; | |
| if (a > b) { | |
| inner = function() { | |
| return { | |
| r : c - d | |
| }; | |
| }; | |
| } else { | |
| inner = function() { | |
| return { | |
| r : c + d | |
| }; | |
| }; | |
| } | |
| } |
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
| // JSDoc記法 | |
| /** | |
| * 文字列を反転させる | |
| * | |
| * @param {String} 反転させたい文字列 | |
| * @return {String} 反転された文字列 | |
| */ | |
| var reverse = function(input) { | |
| // ... | |
| return output; | |
| } |
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
| /** | |
| * My JavaScript application | |
| * | |
| * @module myapp | |
| */ | |
| var MYAPP = {}; | |
| /** | |
| * A math utility | |
| * @namespace MYAPP | |
| * @class math_stuff | |
| */ | |
| MYAPP.math_stuff = { | |
| /** | |
| * Sums two numbers | |
| * | |
| * @method sum | |
| * @param {Number} a First number | |
| * @param {Number} b The second number | |
| * @return {Number} The sum of the two inputs | |
| */ | |
| sum : function(a, b) { | |
| return a + b; | |
| }, | |
| /** | |
| * Multiplies two numbers | |
| * | |
| * @method multi | |
| * @param {Number} a First number | |
| * @param {Number} b The second number | |
| * @return {Number} The two inputs multiplied | |
| */ | |
| multi : function(a, b) { | |
| return a * b; | |
| } | |
| }; | |
| /** | |
| * Constructs Person objects | |
| * @class Person | |
| * @constructor | |
| * @namespace MYAPP | |
| * @param {String} first First name | |
| * @param {String} last Last name | |
| */ | |
| MYAPP.Person = function(first, last) { | |
| /** | |
| * Name of the person | |
| * @property first_name | |
| * @type String | |
| */ | |
| this.first_name = first; | |
| /** | |
| * Last (family) name of the person | |
| * @property last_name | |
| * @type String | |
| */ | |
| this.last_name = last; | |
| }; | |
| /** | |
| * Returns the name of the person object | |
| * | |
| * @method getName | |
| * @return {String} The name of the person | |
| */ | |
| MYAPP.Person.prototype.getName = function() { | |
| return this.first_name + ' ' + this.last_name; | |
| }; |
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
| var Person = function(name) { | |
| this.name = name; | |
| this.say = function() { | |
| return "I am " + this.name; | |
| }; | |
| }; |
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
| Person.prototype.say = function () { | |
| return "I am " + this.name; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment