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
Ji Guang
November 09, 2012
Technology
650
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
More Decks by Ji Guang
See All by Ji Guang
Adobe Edge Preview
jiguang
0
80
Blogging Like A Hacker
jiguang
5
290
Signature Tool
jiguang
1
85
Javascript正则表达式
jiguang
3
700
虚拟机调试
jiguang
1
110
DNS缓存清除
jiguang
0
140
哈工大90周年校庆吉祥物评比
jiguang
0
180
Other Decks in Technology
See All in Technology
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
11k
背中から、背中へ /paying forward to community
naitosatoshi
0
210
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
690
Tech-Verse 2026_Keynote
lycorptech_jp
PRO
0
120
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
7.9k
トークン最適化のためのユーザーストーリー分析 / User Story Analysis for Token Optimization
oomatomo
0
160
BPaaSで進むAIオペレーションの現在地 AI実装が効く領域とスケーラビリティの選定と実装
kentarofujii
0
250
Microsoft のサポートとフィードバック総まとめ
murachiakira
PRO
0
130
初めてのDatabricks勉強会
taka_aki
2
220
どうして今サーバーサイドKotlinを選択したのか
nealle
0
180
きのこカンファレンス2026_肩書きを外したとき私は誰か
yamasatimi
1
120
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
110
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
150
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Why Our Code Smells
bkeepers
PRO
340
58k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
The Pragmatic Product Professional
lauravandoore
37
7.3k
Side Projects
sachag
455
43k
How to Talk to Developers About Accessibility
jct
2
270
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
450
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
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!