Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Basic JavaScript Coding Patterns
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Ji Guang
November 09, 2012
Technology
650
4
Share
Basic JavaScript Coding Patterns
no
Ji Guang
November 09, 2012
More Decks by Ji Guang
See All by Ji Guang
Adobe Edge Preview
jiguang
0
80
Blogging Like A Hacker
jiguang
5
280
Signature Tool
jiguang
1
84
Javascript正则表达式
jiguang
3
690
虚拟机调试
jiguang
1
110
DNS缓存清除
jiguang
0
130
哈工大90周年校庆吉祥物评比
jiguang
0
170
Other Decks in Technology
See All in Technology
[みん強]AIの価値を最大化するデータ基盤戦略:Self-Service型Data Meshへの転換とAgentic AI Meshに向けた取り組み with Snowflake他
y_matsubara
1
140
SpeechTranscriber + AIによる文字起こし機能
kazuki1220
0
110
全社統制を維持しながら現場負担をどう減らすか〜プラットフォームチームとセキュリティチームで進めたSecurity Hub活用によるAWS統制の見直し〜/secjaws-security-hub-custom-insights
mhrtech
1
590
CARTA HOLDINGS エンジニア向け 採用ピッチ資料 / CARTA-GUIDE-for-Engineers
carta_engineering
0
47k
"スキルファースト"で作る、AIの自走環境
subroh0508
0
610
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.6k
Oracle Cloud Infrastructure presents managed, serverless MCP Servers for Oracle AI Database
thatjeffsmith
1
370
20260515 ログイン機能だけではないアカウント管理を全体で考える~サービス設計者向け~
oidfj
1
800
JaSSTに関わることで変わった人生観 #jasstnano
makky_tyuyan
0
140
How to learn AWS Well-Architected with AWS BuilderCards: Security Edition
coosuke
PRO
0
150
Swift Sequence の便利 API 再発見
treastrain
1
290
AI時代に、 データアナリストがデータエンジニアに異動して
jackojacko_
0
1k
Featured
See All Featured
Become a Pro
speakerdeck
PRO
31
5.9k
Unsuck your backbone
ammeep
672
58k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
380
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Exploring anti-patterns in Rails
aemeredith
3
360
A Modern Web Designer's Workflow
chriscoyier
698
190k
A designer walks into a library…
pauljervisheath
211
24k
We Are The Robots
honzajavorek
0
230
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Ethics towards AI in product and experience design
skipperchong
2
270
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
54
Transcript
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!