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
Basic JavaScript Coding Patterns
Search
Ji Guang
November 09, 2012
Technology
660
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Basic JavaScript Coding Patterns
no
Ji Guang
November 09, 2012
Transcript
20 slides
Basic JavaScript Coding Patterns @laserji 2012-05-16
Variable Definition var a = 0, b=1, c=“xxx”; //
shorter var obj = {}; // faster var arr = []; var regex = /something/;
Default Value arg = arg || „default‟; // fallback
e || e = window.event; return a || „default‟
Condition answer = obj && obj.xx && obj.xx. property
if( obj.xx ){ … } // don‟t: if(obj != null){} if( a === b){ … } // always if(document.getElementById){ … } // ability detect
Ternary check ? value1 : value2; var foo
= (condition) ? value1 : value2; return (condition) ? value1 : value2; foo = predicate ? ”one” : predicate ? ”two” : ”default”;
Iteration var name = value[i]; i++ var
name = values[i++];
DOM Operation el.style.display = „none‟; // offline //
operation el.style.display = „block‟; var fragment = document.createDocumentFragment(); innerHTML // Be careful with NodeList var imges = document.getElementsByTagName(„img‟);
Event Agent attach event at a higher level
<div id=“highLevelElement”> <div><ul><li> … </li></ul></div> </div> $(„#id‟).click(function(evt){ … });
Object as a Namespace var MYAPP = {};
MYAPP.namespace(„event‟);
Chaining var obj = new MYAPP.dom.Element(„span‟); obj.setText(„hello‟)
.setStyle(„color‟, „red‟) .serStyle(„font‟, „Verdana‟); document.body.appendChild(obj); // return this
Function (function(){ … })(); // block scope (function($){
… }(jQuery);
Configure Obj function foo( conf ){ … }
foo ({ key1 : 1, key2 : 2 })
Type Conversion +‟010‟ === 10 Number(„010‟) === 10
parseInt(„010‟, 10) === 10 10 + “” === “10”
Prototype Array.prototype.forEach = function(){ … } // only
for forward compatible
Loop for ( var I = 0, j =
xx.length; i<j; i++){ … } // cache var I = 0, j = xx.lenght; for (;i<j; i++){ … } // hoisting for ( var I in foo ){ if( foo.hasOwnProperty(i) ){ // required console.log(i) } }
New Feature Array.forEach() // use it when possible
getElementsByClassName() querySlectorAll()
Lazy Definition function lazyDef(){ if(condition1){ lazyDef =
function(){ … }; }else if(condition2){ lazyDef = function(){ … }; } return lazyDef(); }
Private Functions as Public Methods var MYAPP = {};
MYAPP.dom = (function(){ var _setStyle = function(el, prop, value){ console.log(„setStyle‟); }; return { setStyle: _setStyle }; })(); // MYAPP.dom.setStyle = otherFunc
Design Patterns Singleton Factory Decorator Observer
……
Thank You!
More Decks by Ji Guang
See All by Ji Guang
Adobe Edge Preview
jiguang
0
87
Blogging Like A Hacker
jiguang
5
290
Signature Tool
jiguang
1
91
Javascript正则表达式
jiguang
3
700
虚拟机调试
jiguang
1
120
DNS缓存清除
jiguang
0
150
哈工大90周年校庆吉祥物评比
jiguang
0
200
Other Decks in Technology
See All in Technology
Amazon EFS のマルウェアスキャンをClamAV で自前実装したら苦労した話
mtmtfactory01
0
130
Webメール開発を大幅に変えたJMAPというゲームチェンジャー
tascript
9
5.6k
実測データからサービスの成長余力を測る。負荷シナリオ・テストデータ・限界値の設計
kazatohiei
1
190
認知負荷の減らし方 ~開発にも日常業務にも活かせるノウハウ~
minorun365
PRO
19
17k
想定外の利用がプロダクトの価値を広げる
lycorptech_jp
PRO
0
120
IVRy における AI 時代のデータプロダクトと事業開発
ivry_presentationmaterials
0
130
その修正で本当に速くなったのか? k6とOpenTelemetryで確かめる性能改善 / Performance review with k6 and OpenTelemetry
ymotongpoo
3
470
VTuberになってみた話
yoshimura_datam
0
120
設計手法を「道具」として使いこなすために
a_suenami
7
1.8k
個人ASを取得して、自宅Kubernetes上のVMをグローバルIPで公開した話
munenick
0
230
そのイメージ、どのCIが作った? Artifact Attestationsで来歴を固定する / Which CI system created that image? Pinning provenance with Artifact Attestations
chmikata
0
290
本の挿絵に困ったから挿絵メーカーを自作した
yoshimura_datam
1
120
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The SEO Collaboration Effect
kristinabergwall1
1
630
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
49k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Designing Experiences People Love
moore
143
24k
Chasing Engaging Ingredients in Design
codingconduct
0
410
Game over? The fight for quality and originality in the time of robots
wayneb77
1
340
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Thoughts on Productivity
jonyablonski
76
5.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Embracing the Ebb and Flow
colly
88
5.2k