Building A Javascript Library 2560
Building A Javascript Library 2560
JavaScript Library
John Resig (ejohn.org)
jQuery JavaScript Library / Mozilla Corporation
✦ $(“#main”).slideDown(“slow”);
✦ $(“#contents”).load(“doc.html”);
Doing something right?
✦ ~25 people on the jQuery Team
✦ 1/4 million visitors per month
✦ Application.events.addListener(“quit”, fn);
✦ Application.browser
.open("http://google.com/").active = true;
✦ Application.bookmarks.all.forEach(function(cur){
if ( cur.url.match(/google.com/) )
cur.remove();
});
Need to know:
✦ Writing a Solid API
✦ Implementation
✦ Complex Applications
✦ Browser Bugs
✦ Documentation
✦ Testing
✦ Maintenance
Writing A Solid API
Orthogonal
✦ Perform Universal Actions
✦ CRUD your methods!
add/remove/delete/modify
✦ FUEL has the following on every object:
✦ .all, .add(...), .remove(...), .get(...)
✦ Argument position
✦ .method( options, arg2, ..., callback )
✦ Callback context
✦ .method(function(){
// this == DOMElement
});
Implementation
Evolution of a JavaScript Coder
✦ “Everything is a reference!”
✦ “You can do OO code!”
✦ “Huh, so that’s how Object Prototypes
work!”
✦ “Thank God for closures!”
Functional Programming
✦ Closures are essential
✦ Understand this snippet:
✦ (function(){
// your code...
})();
✦ Great for ‘local variables’
✦ Perfect for macros
✦ var event = [‘click’,’focus’,’blur’,...];
jQuery.each(event,function(i,name){
jQuery.prototype[name] = function(fn){
return this.bind(name,fn);
};
});
Quick Tip: Local Vars
✦ (function(){
// your code...
var test = false;
this.method(function(){
return test;
});
}).call(this);
✦ Locally-scoped variables
✦ Access to instance
Encapsulation
✦ Contain all of your code to a scope
✦ Hide your code and prevent it from
leaking
✦ All of your code should be wrapped in a:
✦ (function(){
// your code...
})();
.css(Number + “px”)
✦ .map(String) becomes:
.map(new Function(String))
✦ Error messages
✦ Quite useful
✦ Byte-costly
✦ Solution: Move to ‘debugging’ extension
Quick Tip for OO
✦ Tweak your Object constructor
✦ function jQuery(str, con){
if ( window == this )
return new jQuery(str, con);
// ...
}
✦ $("div.section").removeClass("section").hide();
✦ $("div.section")
.find("dt")
.addClass("section")
.click(function(){
$(this).next().toggle();
})
.end()
.find("dd")
.hide()
.filter(":first")
.show()
.end()
.end();
One-Liners
✦ Remove unsightly anonymous functions!
✦ $("div.section")
.find("dt")
.addClass("section")
.onclick()
.next().toggle().end()
.end()
.end()
.find("dd")
.hide()
.filter(":first")
.show()
.end()
.end();
Domain-Specific Language
✦ $("div.section")
find("dt")
addClass("section")
click(
next()
toggle()
)
end()
find("dd")
hide()
filter(":first")
show()
end()
end()
Domain-Specific Language
✦ $("div.section"
find("dt"
addClass("section")
click(
next(
toggle())))
find("dd"
hide()
filter(":first"
show())))
Domain-Specific Language
✦ Lisp-y!
✦ ($ "div.section"
(find "dt"
(addClass "section")
(click (next (toggle))))
(find "dd"
(hide)
(filter ":first"
(show)))))
Domain-Specific Language
✦ Python-y!
✦ div.section:
dt:
addClass "section"
click
next toggle
dd:
hide
:first: show
✦ Give it a try:
http://ejohn.org/apps/jquery2/
✦ Secret Sauce:
<script type=”text/jquery”>...</script>