카테고리 없음2016. 1. 28. 17:00

http://www.sorting-algorithms.com/

Posted by 미랭군
카테고리 없음2015. 11. 17. 17:01

browser: (function () {

var ua = navigator.userAgent.toLowerCase();

var mobile = (ua.search(/mobile/g) != -1);

if (ua.search(/iphone/g) != -1) {

return { name: "iphone", version: 0, mobile: true }

} else if (ua.search(/ipad/g) != -1) {

return { name: "ipad", version: 0, mobile: true }

} else if (ua.search(/android/g) != -1) {

var match = /(android)[ \/]([\w.]+)/.exec(ua) || [];

var browserVersion = (match[2] || "0");

return { name: "android", version: browserVersion, mobile: mobile }

} else {

var match = /(msie) ([\w.]+)/.exec(ua) ||

/(trident)(?:.*? rv:([\w.]+)|)/.exec(ua) ||

/(opera|opr)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||

/(chrome)[ \/]([\w.]+)/.exec(ua) ||

/(webkit)[ \/]([\w.]+)/.exec(ua) ||

ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||

[];


var browser = (match[1] || "");

var browserVersion = (match[2] || "0");


var browserName = {

"msie"   : "ie",

"trident": "ie",

"opr"    : "opera"

};

if (browser in browserName) browser = browserName[browser];


return {

name: browser,

version: browserVersion,

mobile: mobile

}

}

})(),

브라우져체크를 하기 위한 함수로써 navigator.userAgent에 지정된 브라우져 정보를 파싱해서 체크한다.

ua.search(/mobile/g)과 같이 /g 옵션이 들어갈 경우 Global 즉 전체를 검색하겠다는 것이다. 

/g옵션이 들어가지 않을 경우에는 첫번째 검색된 결과만 리턴한다.

/i는 대소문자 구분을 하지 않겠다는 것이다.


일반적인 문자열(literal) 찾기를 할 경우엔 indexOf()가 search()보다 검색속도가 빠르다. 

하지만 search()의 경우엔 정규식을 사용할 수 있다. 상황에 맞게 사용하여야 할 것 같다.



Posted by 미랭군
2015. 11. 17. 16:52

each:  function(obj, callback){

if(obj){

var name, i = 0, length = obj.length,

isObj = length === undefined || Object.isFunction( obj );

if ( isObj ) { 

for ( name in obj ) {

if ( callback.call( obj[ name ], name, obj[ name ] ) === false ) {

break;

}

}

} else {

for ( ; i < length; ) {

if ( callback.call( obj[ i ], i, obj[ i++ ] ) === false ) {

break;

}

}

}

}

},


1. obj가 Object형일 경우에는 for in문을 사용하여 Object안에 담긴 모든 key, value를 가져와서 callback함수로 지정된 함수를 호출하여 가공처리 한다.

2. obj가 Object형이 아니고 Array형일 경우에는 배열에 담긴 객체수만큼 루프를 돌면서 callback함수를 호출한다.

Posted by 미랭군