Skip to content

Instantly share code, notes, and snippets.

@yulanggong
Created March 5, 2014 03:38
Show Gist options
  • Save yulanggong/9360700 to your computer and use it in GitHub Desktop.
Save yulanggong/9360700 to your computer and use it in GitHub Desktop.
/**
* 把数组填充到字符串内
* @param {Array} array
* @param {Function} fn
* @return {String}
*/
function each(array, fn){
var s = '', l = array.length;
for (var i = 0; i < l; i++) {
if (typeof array[i] != 'undefined'){
s += fn(array[i], i, array);
}
};
return s;
}
/**
* 类似 if else 语句
* @param {Boolean} condition
* @param {String|Function} whentrue
* @param {String|Function} [whenfalse]
* 参数可以是任意多个 一个条件后跟一个表达式或函数,最后可带一个条件为假情况下的表单式或函数
* @return {String}
*/
function ifel(condition, whentrue, whenfalse){
if (condition) {
return typeof whentrue == 'function' ? whentrue() : whentrue;
} else if (arguments.length == 2){
return '';
} else if (arguments.length == 3){
return typeof arguments[2] == 'function' ? arguments[2]() : arguments[2];
} else {
return ifel.apply(this, [].slice.call(arguments, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment