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
Why YUI3
Search
ningzbruc
September 25, 2013
Technology
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Why YUI3
Why we use YUI3?
ningzbruc
September 25, 2013
More Decks by ningzbruc
See All by ningzbruc
如何写出一个优秀的开源库
ningzbruc
0
68
去啊无线前端的一天
ningzbruc
1
200
React & Component
ningzbruc
0
47
阿里旅行去啊H5首页总结&Promise
ningzbruc
0
270
KISSY.Base - all about that Base
ningzbruc
0
140
Hammer.js
ningzbruc
1
340
淘宝旅行门票iPad版开发
ningzbruc
0
140
Travel on KISSY mini
ningzbruc
0
210
Events
ningzbruc
2
130
Other Decks in Technology
See All in Technology
株式会社シーエーシー エンジニア向け会社紹介資料
cac
0
57k
OpenTelemetryのメトリクスをCloudWatchに送ってPromQLで見てみた
ota1022
0
150
DEFCON_CHV_CTF_Write-up.pdf
bata_24
0
150
OpenTelemetry eBPF Instrumentationの舞台裏 / Behind the Scenes of OpenTelemetry eBPF Instrumentation
ymotongpoo
4
1.3k
人間はどの意思決定を手放せるのか
kawasima
14
6.6k
Omarchy Quattro の日本語設定周り
simosako
2
180
研究開発部の紹介 / Sansan R&D Profile
sansan33
PRO
5
25k
顧客に向き合う開発組織へ。リアーキテクチャとフィーチャーチーム化で挑む組織改革
safie
0
1.8k
[2026-09-11]SREは誰のもの?運用エンジニアが始める 「SRE領域への越境」とチームの進化の軌跡 〜Road to NEXT CRE
tosite
0
230
AI時代、データエンジニアが一番おもろい
genshun9
0
580
おい、エージェントを使って終わらせろ
nwiizo
0
220
Minecraft JavaのMODをSwiftで作る
1mash0
0
160
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Balancing Empowerment & Direction
lara
6
1.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Amusing Abliteration
ianozsvald
1
300
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
330
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
280
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Statistics for Hackers
jakevdp
799
230k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Transcript
淘宝前端开发⼯工程师 @虎⽛牙K 虎⽛牙 Why YUI3?
• ⻚页⾯面多⽽而重 • 交互愈发细致复杂 • 代码量迅速膨胀 • 团队⼈人员扩张 • 维护成本增加
前端⼯工程师的困扰
• 更好地代码管理 • 使代码更有灵活性 • 编写可维护的代码 YUI3能做什么?
管理你的代码
<!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script src="a.js"></script> <script src="A.js"></script>
<script src="b.js"></script> <script src="Ab.js"></script> </head> <body> <!-- More Code --> </body> </html> 还记得那⼀一年...
脚本阻塞
按序加载执行
异步脚本加载
处理依赖关系
单兵作战
多人协作
Oops! 代码冲突!
None
You need a Boss!!!
None
None
# module
开始使⽤用YUI3 <script src="http://yui.yahooapis.com/3.9.1/build/yui/yui-min.js"></script>
YUI().use('node', function(Y) { doSomething(); }); 开始使⽤用YUI3 <script src="http://yui.yahooapis.com/3.9.1/build/yui/yui-min.js"></script>
Combo 请求 Combo
Combo 请求 Combo
YUI({ combine: false }).use('node', function(Y) { doSomething(); }); 取消 Combo
<script src="http://yui.yahooapis.com/3.9.1/build/yui/yui-min.js"></script>
combine: false 并⾏行下载
YUI3模块列表
YUI3模块列表 300+
如何⾃自定义模块?
YUI.add('hello', function(Y) { Y.Hello = { say: function() { console.log('hello');
} }; }, '0.0.1', { requires: ['node', 'event'] }); 注册模块 hello.js
YUI().use('hello', function(Y) { Y.Hello.say(); }); 使⽤用模块 page.html
YUI().use('hello', function(Y) { Y.Hello.say(); }); 使⽤用模块 page.html :(
YUI({ modules: { 'hello': { path: 'hello/hello.js', requires: ['node', 'event']
} } }).use('hello', function(Y) { Y.Hello.say(); //hello }); 配置模块 page.html :)
YUI.add('hello', ...); YUI().use('hello', ...); YUI?
YUI.add('hello', ...); YUI().use('hello', ...); YUI?
YUI.add('hello', ...); YUI().use('hello', ...); YUI? YUI() == new YUI();
YUI().use('hello', function(Y) { Y.Hello.say(); }); YUI.add('hello', function(Y) { Y.Hello =
{ say: function() { console.log('hello'); } }; }, '0.0.1', { requires: ['node', 'event'] }); Y?
YUI().use('hello', function(Y) { Y.Hello.say(); }); YUI.add('hello', function(Y) { Y.Hello =
{ say: function() { console.log('hello'); } }; }, '0.0.1', { requires: ['node', 'event'] }); Y?
YUI().use('node', function(Y) { YUI().use('node', function(Y2) { Y.log(Y2 == Y); //false
}); }); 强沙箱
YUI().use('node', function(Y) { YUI().use('node', function(Y2) { Y.log(Y2 == Y); //false
}); }); 强沙箱 YUI().use('node', function(Y) { Y.use('node', function(Y2) { Y.log(Y2 == Y); //true }); });
✓ 模块解耦 ✓ 按需加载 ✓ 异步加载 ✓ 减少HTTP请求 ✓ 强沙箱机制
✓ 处理依赖关系 ✓ 模块顺序执⾏行 ✓ 模块信息配置 YUI3模块系统
更专注于编码
不可或缺的...
“钱不是万能的,但没有钱是万万不能的!”
# DOM
YUI().use('node', fn); DOM
query var item = Y.one('li'); var itemList = Y.all('li'); one
all
methods itemList.setAttribute('num', '10'); itemList.setStyle('zIndex', 50); itemList.each(function(item, index) { item.setContent(index); });
one item.addClass('item'); item.appendChild('<p>Hello YUI3!</p>'); all
Y.one('p').get('innerHTML'); Y.one('input').get('value'); Y.one('div').get('offsetWidth'); get properties Y.one('p').set('innerHTML', 'hello'); Y.one('input').set('value', '1234'); set
# Events
Events YUI().use('event', fn);
Y.one('li').on('click', fn); on Y.all('li').on('click', fn); Y.one('li').on({ 'click': clickFn, 'dblclick': dblclickFn,
'mouseover': mouseoverFn, });
delegate Y.all('li').on('click', fn);
delegate Y.all('li').on('click', fn); Y.one('ul').delegate('click', fn, 'li');
node.on('click', fn); node.detach(); node.detach('click'); node.detach('click', fn); detach
node.on('click', fn); node.detach(); node.detach('click'); node.detach('click', fn); detach
var handler = node.on('click', fn); node.detach(); node.detach('click'); handler.detach(); detach
Y.one('a').on('click', function(e) { }); e
Y.one('a').on('click', function(e) { }); e e.preventDefault();
Y.one('a').on('click', function(e) { }); e e.preventDefault(); e.stopPropagation();
Y.one('a').on('click', function(e) { }); e e.preventDefault(); e.stopPropagation(); e.halt();
Y.one('a').on('click', function(e) { }); e e.preventDefault(); e.stopPropagation();
Y.one('a').on('click', function(e) { }); e e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation();
Y.one('a').on('click', function(e) { }); e e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); e.halt(true);
Y.one('ul').on('click', function(e) { Y.log(e.target.get('tagName')); //li Y.log(e.currentTarget.get('tagName')); //ul Y.log(e.target == this);
//true Y.log(e.target instanceof Y.Node;); //true e.target.addClass('item'); }); e
Y.on('domready', fn); Y.on('contentready', fn, '#content'); Y.on('available', fn, '#content'); lifecycle events
使代码更加灵活
• ⾼高内聚低耦合 • 易通信易扩展 灵活性
# OOP
• extend • augment OOP
function Animal() {} Animal.prototype.move = function() { Y.log('move'); }; extend
function Cat() { Cat.superclass.constructor.apply(this, arguments); } Y.extend(Cat, Animal); var cat = new Cat(); cat.move(); //move
function Fly() {} Fly.prototype.fly = function() { Y.log('fly'); }; augment
Y.augment(Cat, Fly); var cat = new Cat(); cat.fly(); //fly
Y.log(cat instanceof Animal); //true Y.log(cat instanceof Fly); //false extend vs.
augment
# Base
Base YUI().use('base', fn);
“Everything is based on Base” Base
• LifeCycle • Custom Event • Attribute • Extension •
PluginHost Base
function Optimus() { Optimus.superclass.constructor.apply(this, arguments); } Optimus.NAME = 'optimus'; Optimus.ATTRS
= {}; Y.extend(Optimus, Y.Base, { initializer: function() {}, destructor: function() {} }); LifeCycle
var optimus = new Optimus();
optimus.destroy();
None
function Optimus() { Optimus.superclass.constructor.apply(this, arguments); } Optimus.NAME = 'optimus'; Optimus.ATTRS
= {}; Y.extend(Optimus, Y.Base, { initializer: function() { this.publish('rollout'); }, destructor: function() {}, commandTo: function(where) { this.fire('rollout', { where: where }); } }); Custom Event
var optimus = new Optimus(); optimus.commandTo('USA'); optimus.js Custom Event optimus.on('rollout',
function(e) { bumblebee.rollout(e.where); }); bumblebee.js
var optimus = new Optimus(); optimus.commandTo('USA'); optimus.js Custom Event Y.on('optimus:rollout',
function(e) { bumblebee.rollout(e.where); }); bumblebee.js Optimus.NAME = 'optimus';
参数设置
Optimus.ATTRS = { node: { getter: function(v) { return v;
}, setter: function(v) { return Y.one(v); }, value: null } }; Attribute
var optimus = new Optimus({node: '#node'}); var node = optimus.get('node');
optimus.set('node', '#otherNode'); Attribute
Y.extend(Optimus, Y.Base, { run: function() { this.fire('run'); }, stop: function()
{ this.fire('stop'); } }); Custom Event
var optimus = new Optimus(); optimus.run(); optimus.run(); optimus.run(); optimus.js Custom
Event Y.on('optimus:run', function(e) { bumblebee.transform(); //3 times bumblebee.run(); }); bumblebee.js
function Optimus() { this.running = false; } optimus.running = true;
Property 什么都没有发⽣生...
Optimus.ATTRS = { running: { getter: function(v) { return v;
}, setter: function(v) { return !!v; }, value: false } }; Attribute
optimus.after('runningChange', function(e) { Y.log(e.prevVal); //false Y.log(e.newVal); //true }); optimus.set('running', true);
Attribute Change Event
Attribute Optimus.ATTRS = { running: { getter: function(v) { return
v; }, setter: function(v) { return !!v; }, value: false } }; Y.extend(Optimus, Y.Base, { initializer: function() { this.after('runningChange', function(e) { this.fire(e.newVal ? 'run' : 'stop'); }); }, run: function() { this.set('running', true); }, stop: function() { this.set('running', false); } });
var optimus = new Optimus({running: null}); optimus.run(); optimus.run(); optimus.run(); optimus.js
Attribute Y.on('optimus:run', function(e) { bumblebee.transform(); //1 time bumblebee.run(); }); bumblebee.js
Y.Base.create('optimus', Y.Base, [ OptimusHead, OptimusBody, OptimusFooter ], { initializer: function()
{}, destructor: function() {}, run: function() {}, stop: function() {} }, { ATTRS: {} }); Base.create
None
optimus.plug(Optimus.Weapon, { bullets: 50 }); optimus.weapon.fire(); optimus.unplug(Optimus.Weapon); PluginHost Optimus.Weapon.NS =
'weapon';
可维护性的代码
无兄弟不篮球
- 你被耍了 - 每当我扣篮,看下30、40分 - 你还真以为,那是我⼀一个⼈人干的 - 不过说真的 - ⽐比赛是整个团队的
- 信不信由你 ⽆无兄弟不篮球
- 你被耍了 - 每当我做项⺫⽬目,开发30、40个组件与模块 - 你还真以为,那是我⼀一个⼈人干的 - 不过说真的 - 编程是整个团队的
- 信不信由你 ⽆无兄弟不前端
• 分⼯工明确 - 模块化开发 • ⽆无缝配合 - 事件通信 • 接⼝口清晰
- API清晰统⼀一 • 磨合简单 - ⽂文档完善 • ⾓角⾊色灵活 - 代码⻛风格统⼀一 多⼈人协作
http://yuilibrary.com/yui/docs/yui/ ⽂文档
http://yuilibrary.com/yui/docs/api/files/yui_js_yui.js.html 注释
http://yui.github.io/yuidoc/ YUIDoc
编码规范
Y?UI
没有最完美的,只有最适合的
Q&A
Thanks!