Skip to content

Instantly share code, notes, and snippets.

@prog-mitsu
Created January 9, 2014 09:56
Show Gist options
  • Select an option

  • Save prog-mitsu/8331911 to your computer and use it in GitHub Desktop.

Select an option

Save prog-mitsu/8331911 to your computer and use it in GitHub Desktop.
「JavaScriptパターン」ピックアップ ref: http://qiita.com/ms32/items/b84b62182854fd622d5b
// for-inループ
var man = {
hands: 2,
legs: 2,
heads: 1
};
for (var i in man) {
if (man.hasOwnProperty(i)) { // フィルタ
console.log(i, ":", man[i]);
}
}
function looper() {
var i = 0,
max,
myarray = [];
// ...
for (i=0, max = myarray.length; i < max; i += 1){
// myarray[i]に対する処理
}
}
var inspect_me = 0,
result = '';
switch (inspect_me) {
case 0:
result = "zero";
break;
case 1:
result = "one;"
break;
default:
result = "unknown";
}
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
};
};
}
}
// JSDoc記法
/**
* 文字列を反転させる
*
* @param {String} 反転させたい文字列
* @return {String} 反転された文字列
*/
var reverse = function(input) {
// ...
return output;
}
/**
* 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;
};
var Person = function(name) {
this.name = name;
this.say = function() {
return "I am " + this.name;
};
};
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