underscorejs.org/
 

Underscore.js

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being produc

underscorejs.org

<script src="https://cdn.jsdelivr.net/npm/underscore@1.12.1/underscore-esm-min.js"></script>

GitHub에서도 개발자들이 많이 사용하고 있는 유명한 함수형 자바스크립트라고 참조하여 공부 중인 책에서 사용하는 것을 권장하고 있는 javascript 라이브러리다.

사용할 인자들을 더 제공하여 다양한 로직을 짤 수 있다길래 한번 사용해 봐야겠다.

 

1. 인자 늘리기

See the Pen rNWbqzY by JNoony (@jnoony) on CodePen.

 

 

2.function identity(v) {return v;}

_.identity = function(v){ return v; };
var a = 10;
console.log( _.ientity(a) ); // 10

 

Underscore.js에서 제공하는 _. identity 함수.

기본 함수에 수식이 없어  변수안에 담아놓은 10이 당연히 리턴한 값이 된다.

 

 

See the Pen eYBojQa by JNoony (@jnoony) on CodePen.

 

Truthy Values( Boolean으로 평가했을 때 true로 평가되는 값들 )만 남았다. 

책 저자는 _.identity를 자만 이용하면 유용한 함수들을 만들 수 있다고 평가하고 있다.

 

/* 참고 */
_.falsy = function(v){ return !v;}
_.truthy = function(v){ return !!v;}

 

See the Pen bGBJQWo by JNoony (@jnoony) on CodePen.

 

some은 배열중 하나라도 참이면 true를, 하나도 없다면 false를 리턴하고,  every는 모두가 참일 때 true를 리턴한다.

 

 

 

728x90

+ Recent posts