Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ペライチのJS技術 Underscore.js入門編
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Yusuke Katsuki
April 12, 2015
Programming
4.4k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ペライチのJS技術 Underscore.js入門編
Yusuke Katsuki
April 12, 2015
Transcript
24 slides
ペライチのJS技術 Underscore.js入門編
ペライチを支えるJS技術 ・Backbone.js ・require.js ・Underscore.js ・uploadify ・magnific-popup.js
Underscore.jsとは JavaScriptの便利関数 の集合体ライブラリ。100 個くらいの関数が定義さ れている。 Backbone.jsも Underscore.jsに依存し ている。
Underscore.jsを使うメリット - 関数名により処理内容が明示的になるため、可 読性が上がる - コード量が減る - 配列扱うときに繰り返し処理とかやらなくてよく なる
Underscore.jsを使うメリット Before After
Underscore.jsの導入 1. 公式サイトからファイルをダウンロードする http://underscorejs.org/ 2.HTMLのheadタグで読み込む
Underscore.jsの書き方 Underscore.jsを読み込むと、「_」というオブジェク トがが生成される。 このオブジェクトに便利な機能が詰まっている。
メソッドの種類 ・コレクション(each, reduce etc...) ・配列(range, uniq etc...) ・関数(bind, defer etc...)
・ユーティリティ(times, random etc...) ・チェーン(chain, value)
Underscore.js 便利関数10選
each PHPでいうforeachに近い。配列にもオブジェクトにも使える。第 二引数である関数の第二引数にはindex番号が渡される(オブ ジェクトの場合はkey)
map eachに似ている。第二引数の関数内での処理が各値に実行さ れた上で、配列となって返ってくる。
reduce 繰り返し処理。mapと異なるのは、返り値が単一の値であるこ と。第一引数である配列の中身が順に渡り、第二引数である関 数の実行結果が第一引数(memo)にストックされる。第三引数 はmemoの初期値。
groupBy 配列を渡すと、関数処理によってグループ分けされたオブジェク トとして返ってくる
sortBy 配列を渡すと、関数処理によってグループ分けされたオブジェク トとして返ってくる
filter 集合要素から条件にマッチしたものを探して配列にして
where オブジェクトの配列にから、条件に合うオブジェクトのみを探索 する
isUndefined is系はやたら充実。他にもisEmpty, isString, isNull, isNumber など。
keys, values keys:オブジェクトのプロパティ名を配列にして返す values:オブジェクトのプロパティの値をすべて返す
template テンプレオートを引数で置き換えるテンプレート関 数を返す。HTMLの変換に便利。
jQueryとの棲み分け jQueryにもeachやbindとかはあるけど???
jQueryとの棲み分け jQueryにもeachやbindとかはあるけど??? AjaxやDOM操作etc... 配列・オブジェクト操作etc...
jQueryとの棲み分け UnderscoreはjQueryのタ キシードとBackbone.jsの サスペンダーにあうようなネ クタイです。 親和性は非常に高いので両方読み込んで便利に使える。
https://peraichi.com/
ペライチお手伝いメンバー募集 ペライチでは平日夜や土日にお手伝いいただける方を 募集しています。 - Webサービス開発に興味がある方 - スタートアップの中身を覗きたい方 - チーム開発に参加してみたい方 ご興味ある方は以下のフォームよりお気軽にご連絡く
ださい。かならずご返信いたします! お問合せフォーム
More Decks by Yusuke Katsuki
See All by Yusuke Katsuki
大規模サービスのCakePHP2.xを4.xにジャンプアップした話
katsukii
3
4.3k
若手エンジニアに知ってほしいそれぞれの働き方の現実
katsukii
0
710
健康で文化的な最低限度のSPA
katsukii
0
1.2k
エンジニア採用のベストプラクティス「お手伝いメンバー制度」
katsukii
0
1.1k
Webサービス「ペライチ」を支える 技術とチームとその変遷
katsukii
1
1.7k
Other Decks in Programming
See All in Programming
はじめてのPLATEAU
nokonoko1203
1
640
Modernizing iOS Apps
seyfoyun
0
150
GraphQL の onError と Nullability のこれから
quramy
0
210
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
1
430
AI Augmentation Techniques with Liberty
ivargrimstad
1
470
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
420
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
230
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
140
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
170
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
310
SODA - FACT BOOK(JP)
sodainc
1
40k
Featured
See All Featured
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
570
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
4
1.2k
Prompt Engineering for Job Search
mfonobong
0
580
Building an army of robots
kneath
307
47k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
640
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.3k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
What the history of the web can teach us about the future of AI
inesmontani
PRO
3
810
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Designing for humans not robots
tammielis
254
26k