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
编写更合理的 JavaScript 代码
Search
Adolescent Developer Community
October 28, 2013
Programming
320
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
编写更合理的 JavaScript 代码
时间:2013年7月27日 北京
演讲类型:学生演讲
讲者:廖晗 - 来自黎松计算机科技
主要内容:编写更合理的 JavaScript 代码
Adolescent Developer Community
October 28, 2013
More Decks by Adolescent Developer Community
See All by Adolescent Developer Community
密SQL指令与数据库的安全
adcmnt
0
210
数据挖掘与无障碍技术
adcmnt
0
190
小谈信息学竞赛与软件开发之间的关系
adcmnt
0
180
走进用户群,才能收获更多
adcmnt
0
110
移动设备应用开发概述
adcmnt
0
110
探索电池
adcmnt
0
160
成长网介绍
adcmnt
0
97
青少年互联网开发与人机交互
adcmnt
0
94
Other Decks in Programming
See All in Programming
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
270
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
450
ChromeでローカルLLM
ryu1013
0
140
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
残高管理から台帳サービスへの進化
artoy
1
200
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
190
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
Featured
See All Featured
Designing for Performance
lara
612
70k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
630
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
730
Leo the Paperboy
mayatellez
10
2.4k
Discover your Explorer Soul
emna__ayadi
2
1.3k
What's in a price? How to price your products and services
michaelherold
247
13k
Prompt Engineering for Job Search
mfonobong
0
520
New Earth Scene 8
popppiees
4
2.7k
Transcript
编写更合理的JS代码 黎松团队·∙廖晗
有多少人接受过JS培训?
演讲大纲 l 更合理的JS代码的实质 l 更合理的代码注释 l 更合理的函数传参模式
l 更合理的DOM l 更合理的编码模式(好书推荐)
更合理的JS代码的实质
合理:合乎道理或事理 l 代码更规范 l 编写更合理 l 更易理解 l 利于开发
开发更合理的JS代码的意义 l 改善代码可读性 l 提高团队效率 l 锻炼严谨的思维 l 减少开发成本
更合理的注释 l 精练,防止多义性 l 使用规范的注释模式和英文 l 勤更新
l 用来解释的内容: l 生涩的代码(算法) l 一段代码想要达到的效果 l 函数的用途
精练的注释 不精练的注释 //Selector the Elements var _find
= window._find = function( exp ){ ....................... } //Selector the Elements by GetElementById & getElementsByTagN .... var _find = window._find = function( exp ){ ....................... }
注释 != 字幕 好的 // find [ID
= A] var oA = document.getElementById('A'); 不好的 /*Found through the document.getElementByID*/ var oA = document.getElementById('A');
更合理的函数传参模式 好的: // op is JOSN data
function Ajax( op ){ ...... } 不好的: function Ajax( type,url,...... ){ ...... }
更合理的DOM l 缓存DOM对象 l innerHTML而不是createElement l 检索元素选择的方式 l 中间变量对于DOM操作的优化
缓存DOM对象 好的 var oDoms = {
oAs = document.getElementsByTagName('a'), oUserImage = document.getElementById('userImage') } oDoms.userImages.src= 'http://a.com/u.png'; 不好的 document.getElementByID('userImage').src = 'http://a.com/u.png';
createElement? 好的 var oA = document.getElementsByTagName('a')[0]; oA.innerHTML
+= '<a href="http://a.com">demo</a>' 不好的 var oNewA = document.createElement('a'); var oA = document.getElementsByTagName('a')[0]; oA.appendChild(oA); 原因:innerHTML属于静态操作
检索元素的方式 方式大全: getElementsByTagName getElementById getElementsByClassName
getElementsByName querySelector 选择方式: 就近原则,匹配范围最小
中间变量对DOM操作的优化 好的 for(var j = 0;j<30;j++){
var tmp = j+'<br/>'; oA.innerHTML += tmp; } 不好的 for(var j = 0;j<30;j++){ oA.innerHTML += j+'<br/>';; }
为什么要使用模块化开发 l 便于管理 l 加快开发速度 l 减少错改代码的次数 l 利于构建大型项目
l 减少项目的复杂性
更合理的编码规范(好书推荐) l 《编写可维护化的JavaScript代码》
l 《编码匠艺》 l 《编写高质量的代码》 ¥40.00 ¥46.75 ¥36.80
END