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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
84
Blogging Like A Hacker
jiguang
5
290
Signature Tool
jiguang
1
89
Javascript正则表达式
jiguang
3
700
虚拟机调试
jiguang
1
120
DNS缓存清除
jiguang
0
150
哈工大90周年校庆吉祥物评比
jiguang
0
190
Other Decks in Technology
See All in Technology
MCPゲートウェイを作って運用してわかったこと — Agent時代の権限管理の現在地
mtpooh
10
2.9k
GitHub Agentic Workflows を触ってみる
htkym
2
920
AI Made Us Faster at Solving the Wrong Problems
marceloancelmo
0
150
SDDの運用にめげずに向き合った話
sansantech
PRO
0
110
TiDBファミリーにDWHが新登場!! TiDB最新情報 / TiDB update 202609
yoshiakiyamasaki
0
190
組み立てて楽しむ AWS Blocks 入門
kmiya84377
0
150
検証フェーズはAIの回答を判断するための学習機会
toru_kubota
2
370
営業オントロジーの作り方と、エージェントからの辿り方 ── ナレッジワークの現場から
kworkdev
PRO
1
230
オブザーバビリティを高める AI エージェント体験を考える / Designing AI Agent Experiences That Enhance Observability
aoto
PRO
2
220
Amazon Bedrock Agents ClassicからAmazon Bedrock AgentCoreへ移行した際、ガードレール設定が2箇所に割れた話
matsunobu
0
170
AI駆動開発で仕様はどこまで書くべきか? ― 人とAIの責務境界から考える開発プロセスの実践
takahiromatsui
1
300
企業の現実世界をグラフで写し取る
sansantech
PRO
0
250
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
290
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
RailsConf 2023
tenderlove
30
1.6k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
310
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Unsuck your backbone
ammeep
672
58k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Prompt Engineering for Job Search
mfonobong
0
470
YesSQL, Process and Tooling at Scale
rocio
174
15k
Code Review Best Practice
trishagee
74
20k
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!