SlideShare a Scribd company logo
1 of 65
Download to read offline
Your JavaScript Library
Edge of the Web ’09
Dmitry Baranovskiy
http://www.atlassian.com/
http://raphaeljs.com/ http://g.raphaeljs.com/
Why should I write
a library of my own?
function trim(str) {
return str.replace(/^s+|s+$/g, "");
}
function $(id) {
return document.getElementById(id);
}
Low Level
High Level
Toolbox
Widgets
Prototype
Scriptaculous jQuery UI
gRaphaël
D
o
j
o
R
a
p
h
a
ë
l
jQuery
Ext
API & Functionality
Library is the answer.
So, what is the question?
Library is the answer.
So, what is the question?
Who is the target?
Java, Ruby, PHP, JavaScript…
Who is the target?
Java, Ruby, PHP, JavaScript…
“Everything should be made
as simple as possible,
but not simpler.”
Albert Einstein
JavaScript is your friend
Performance
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0; i < a.length; i++) {
a[i] *= 2;
}
var j = a.length;
while (j--) {
a[j] *= 2;
}
function parseColour(colour) {
// #XXXXXX
var value = parseInt(colour.substring(1), 16);
return {
r: (value & 0xff0000) >> 16,
g: (value & 0xff00) >> 8,
b: value & 0xff,
};
}
var parseColour = (function () {
var cache = {};
return function (colour) {
if (colour in cache) {
return cache[colour];
}
// calculation
cache[colour] = value;
return value;
};
})();
var parseColour = (function () {
var cache = {},
count = [];
return function (colour) {
if (colour in cache) {
return cache[colour];
}
// calculation
cache[colour] = value;
count.push(colour);
if (count.length > 1000) {
delete cache[count.shift()];
}
return value;
};
})();
Performance
Rocks
by Thomas Fuchs & Amy Hoy
JavaScript Rocks! presents...
find more awesome JavaScript stuff at http://www.jsrocks.com
JavaScript
Animation
Bulletproof
Global Scope
Global Scope
Treat it as a public toilet
var myLib = {
method1: function () {},
method2: function () {},
// ...
};
var myLib = {};
(function () {
var libVariable = 2;
myLib.method1 = function () {};
myLib.method2 = function () {};
})();
Native Prototypes
String.prototype.trim = function () {
return this.replace(/^s+|s+$/g, "");
};
Number.prototype.times = function (func) {
for (var i = 0; i < this; i++) {
func(i);
}
};
Object.prototype
for (var value in cache) {
this.setAttribute(value, cache[value]);
}
var horizontal = {left: 1, right: 1};
if (direction in horizontal) {
this.horizontal(direction);
}
Object.prototype.top = 3;
// ...
for (var value in cache) {
this.setAttribute(value, cache[value]);
}
var horizontal = {left: 1, right: 1};
if (direction in horizontal) {
this.horizontal(direction);
}
Object.prototype.top = 3;
// ...
for (var value in cache) {
if (cache.hasOwnProperty(value)) {
this.setAttribute(value, cache[value]);
}
}
var horizontal = {left: 1, right: 1};
if (horizontal.hasOwnProperty(direction)) {
this.horizontal(direction);
}
function isArray(object) {
return object && (object instanceof Array);
}
Beware of <iframe>
function isArray(object) {
return Object.prototype.toString.call(object)
=== "[object Array]";
}
undefined
function setSomething(a) {
if (a == undefined) {
a = 5;
}
this.set(a);
}
var undefined;
function setSomething(a) {
if (a == undefined) {
a = 5;
}
this.set(a);
}
function setSomething(a) {
this.set(a || 5);
}
Packaging
Minify / Pack / Obfuscate
JSMin
Dojo ShrinkSafe
Packer
YUI Compressor
jQuery
Prototype
Raphaël
18 Kb
24 Kb
19 Kb
52 Kb
80 Kb
56 Kb
121 Kb
138 Kb
120 Kb
Original Minified GZIP
function calculate(value) {
if (typeof value == "number") {
return parseFloat(value);
}
if (isArray(value)) {
var i = value.length;
while (i--) value[i] = parseFloat(value[i]);
return value.join(",");
}
var values = value.split(","),
i = values.length;
while (i--) values[i] = parseFloat(values[i]);
return values.join(",");
}
394 b
function calculate(c){if(typeof c=="number"){return
parseFloat(c);}if(isArray(c)){var
b=c.length;while(b--){c[b]=parseFloat(c[b]);}return
c.join(",");}var a=c.split(","),b=a.length;while(b--)
{a[b]=parseFloat(a[b]);}return a.join(",");}
235 b
394 b
function calculate(value) {
var parseFloat = parseFloat;
if (typeof value == "number") {
return parseFloat(value);
}
if (isArray(value)) {
var i = value.length;
while (i--) value[i] = parseFloat(value[i]);
return value.join(",");
}
var values = value.split(","),
i = values.length;
while (i--) values[i] = parseFloat(values[i]);
return values.join(",");
} 427 b
235 b
394 b
function calculate(value) {
var parseFloat = parseFloat;
if (typeof value == "number") {
return parseFloat(value);
}
if (isArray(value)) {
var i = value.length;
while (i--) value[i] = parseFloat(value[i]);
return value.join(",");
}
var values = value.split(","),
i = values.length;
while (i--) values[i] = parseFloat(values[i]);
return values.join(",");
} 427 b
235 b
394 b
function calculate(d){var b=b;if(typeof d=="number")
{return b(d);}if(isArray(d)){var
c=d.length;while(c--){d[c]=b(d[c]);}return
d.join(",");}var a=d.split(","),c=a.length;while(c--)
{a[c]=b(a[c]);}return a.join(",");}
216 b
427 b
235 b
394 b
element.setAttribute("width", 320);
element.setAttribute("width", 320);
var setAttribute = function (element, key, value) {
element.setAttribute(key, value);
}
// ...
setAttribute(element, "width", 320);
element.setAttribute("width", 320);
var setAttribute = function (element, key, value) {
element.setAttribute(key, value);
}
// ...
setAttribute(element, "width", 320);
var setAttribute = "setAttribute";
// ...
element[setAttribute]("width", 320);
Error Handling
function setWidth(width) {
width = parseFloat(width);
if (isNaN(width)) {
handleErrors("‘width’ is not a number");
}
}
function handleErrors(message) {
throw new Error(message);
}
function update(x, y, width, height) {
try {
this.setX(x);
this.setY(y);
this.setWidth(width);
this.setHeight(height);
} catch (err) {
throw new Error("Some error happened…
Somewhere.");
}
}
JSLint
http://jslint.com/
Share the magic
Thank You
Icons used with permission of Iconfactory
http://dmitry.baranovskiy.com
dmitry@baranovskiy.com

More Related Content

What's hot

Keeping It Small with Slim
Keeping It Small with SlimKeeping It Small with Slim
Keeping It Small with SlimRaven Tools
 
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1Vagmi Mudumbai
 
Undercover Pods / WP Functions
Undercover Pods / WP FunctionsUndercover Pods / WP Functions
Undercover Pods / WP Functionspodsframework
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KThomas Fuchs
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 
Introduction to the Pods JSON API
Introduction to the Pods JSON APIIntroduction to the Pods JSON API
Introduction to the Pods JSON APIpodsframework
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Arc & Codementor
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012xSawyer
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaStephen Vance
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScriptStalin Thangaraj
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkJeremy Kendall
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with MooseDave Cross
 

What's hot (20)

Play á la Rails
Play á la RailsPlay á la Rails
Play á la Rails
 
Keeping It Small with Slim
Keeping It Small with SlimKeeping It Small with Slim
Keeping It Small with Slim
 
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
Undercover Pods / WP Functions
Undercover Pods / WP FunctionsUndercover Pods / WP Functions
Undercover Pods / WP Functions
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Sprockets
SprocketsSprockets
Sprockets
 
Introduction to the Pods JSON API
Introduction to the Pods JSON APIIntroduction to the Pods JSON API
Introduction to the Pods JSON API
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012
 
Ruby meets Go
Ruby meets GoRuby meets Go
Ruby meets Go
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprima
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with Moose
 

Viewers also liked

Unbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichUnbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichFESD GKr
 
mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Team
 
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Martina Goehring
 
E Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 PersoenlichkeitE Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 Persoenlichkeitthomasabauer
 
Kelantan
KelantanKelantan
Kelantanwmzuri
 
Financial Institutions Must Support Their Clients on Twitter
Financial Institutions Must Support Their Clients on TwitterFinancial Institutions Must Support Their Clients on Twitter
Financial Institutions Must Support Their Clients on TwitterChristophe Langlois
 
Plan de mejora jefatura de sector 06 21 fts4006o
Plan de mejora jefatura de sector 06  21 fts4006oPlan de mejora jefatura de sector 06  21 fts4006o
Plan de mejora jefatura de sector 06 21 fts4006oRoberto Carlos Vega Monroy
 
EdChang - Parallel Algorithms For Mining Large Scale Data
EdChang - Parallel Algorithms For Mining Large Scale DataEdChang - Parallel Algorithms For Mining Large Scale Data
EdChang - Parallel Algorithms For Mining Large Scale Datagu wendong
 
Uv08 Dcii Tema 3 DiseñO Y Gestion Servicios
Uv08 Dcii Tema 3 DiseñO Y Gestion ServiciosUv08 Dcii Tema 3 DiseñO Y Gestion Servicios
Uv08 Dcii Tema 3 DiseñO Y Gestion ServiciosJordi Miro
 
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...Mary Elizabeth Francisco
 
Analisis Grafico 3 Ejercicios
Analisis Grafico 3 EjerciciosAnalisis Grafico 3 Ejercicios
Analisis Grafico 3 EjerciciosMarcos A. Fatela
 
Gestión y diseño de los instrumentos de comunicacion de marketing
Gestión y diseño de los instrumentos de comunicacion de marketingGestión y diseño de los instrumentos de comunicacion de marketing
Gestión y diseño de los instrumentos de comunicacion de marketingJordi Miro
 
Simce 2014
Simce 2014Simce 2014
Simce 201415511
 
Salesforce.com Agile Transformation - Agile 2007 Conference
Salesforce.com Agile Transformation - Agile 2007 ConferenceSalesforce.com Agile Transformation - Agile 2007 Conference
Salesforce.com Agile Transformation - Agile 2007 ConferenceSteve Greene
 
Science Interactive Notebook
Science Interactive NotebookScience Interactive Notebook
Science Interactive NotebookEboni DuBose
 
Creating Tribal Ideas for Gen C - By Dan Pankraz for Nokia World '09
Creating Tribal Ideas for Gen C  - By Dan Pankraz for Nokia World '09Creating Tribal Ideas for Gen C  - By Dan Pankraz for Nokia World '09
Creating Tribal Ideas for Gen C - By Dan Pankraz for Nokia World '09guestbcb2a7
 
Gridcomputing
GridcomputingGridcomputing
Gridcomputingpchengi
 

Viewers also liked (20)

Unbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichUnbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_Österreich
 
mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"
 
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
 
E Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 PersoenlichkeitE Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 Persoenlichkeit
 
Kelantan
KelantanKelantan
Kelantan
 
Financial Institutions Must Support Their Clients on Twitter
Financial Institutions Must Support Their Clients on TwitterFinancial Institutions Must Support Their Clients on Twitter
Financial Institutions Must Support Their Clients on Twitter
 
Plan de mejora jefatura de sector 06 21 fts4006o
Plan de mejora jefatura de sector 06  21 fts4006oPlan de mejora jefatura de sector 06  21 fts4006o
Plan de mejora jefatura de sector 06 21 fts4006o
 
EdChang - Parallel Algorithms For Mining Large Scale Data
EdChang - Parallel Algorithms For Mining Large Scale DataEdChang - Parallel Algorithms For Mining Large Scale Data
EdChang - Parallel Algorithms For Mining Large Scale Data
 
Uv08 Dcii Tema 3 DiseñO Y Gestion Servicios
Uv08 Dcii Tema 3 DiseñO Y Gestion ServiciosUv08 Dcii Tema 3 DiseñO Y Gestion Servicios
Uv08 Dcii Tema 3 DiseñO Y Gestion Servicios
 
Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
 
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...
Ati flash cards 09, medications affecting fluid, electrolytes, minerals, and ...
 
Analisis Grafico 3 Ejercicios
Analisis Grafico 3 EjerciciosAnalisis Grafico 3 Ejercicios
Analisis Grafico 3 Ejercicios
 
Gestión y diseño de los instrumentos de comunicacion de marketing
Gestión y diseño de los instrumentos de comunicacion de marketingGestión y diseño de los instrumentos de comunicacion de marketing
Gestión y diseño de los instrumentos de comunicacion de marketing
 
Simce 2014
Simce 2014Simce 2014
Simce 2014
 
Salesforce.com Agile Transformation - Agile 2007 Conference
Salesforce.com Agile Transformation - Agile 2007 ConferenceSalesforce.com Agile Transformation - Agile 2007 Conference
Salesforce.com Agile Transformation - Agile 2007 Conference
 
Mini Project- Stepper Motor Control
Mini Project- Stepper Motor ControlMini Project- Stepper Motor Control
Mini Project- Stepper Motor Control
 
Science Interactive Notebook
Science Interactive NotebookScience Interactive Notebook
Science Interactive Notebook
 
Sports MKT 2011 - Daniel Sá
Sports MKT 2011 - Daniel SáSports MKT 2011 - Daniel Sá
Sports MKT 2011 - Daniel Sá
 
Creating Tribal Ideas for Gen C - By Dan Pankraz for Nokia World '09
Creating Tribal Ideas for Gen C  - By Dan Pankraz for Nokia World '09Creating Tribal Ideas for Gen C  - By Dan Pankraz for Nokia World '09
Creating Tribal Ideas for Gen C - By Dan Pankraz for Nokia World '09
 
Gridcomputing
GridcomputingGridcomputing
Gridcomputing
 

More from Dmitry Baranovskiy (15)

JavaScript: enter the dragon
JavaScript: enter the dragonJavaScript: enter the dragon
JavaScript: enter the dragon
 
The Origins of Magic
The Origins of MagicThe Origins of Magic
The Origins of Magic
 
Demystifying Prototypes
Demystifying PrototypesDemystifying Prototypes
Demystifying Prototypes
 
Raphaël
RaphaëlRaphaël
Raphaël
 
Type Recognition
Type RecognitionType Recognition
Type Recognition
 
Raphaël JS Conf
Raphaël JS ConfRaphaël JS Conf
Raphaël JS Conf
 
Obvious Secrets of JavaScript
Obvious Secrets of JavaScriptObvious Secrets of JavaScript
Obvious Secrets of JavaScript
 
Canvas
CanvasCanvas
Canvas
 
SVG
SVGSVG
SVG
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
Raphael
RaphaelRaphael
Raphael
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
 
Typography on the Web
Typography on the WebTypography on the Web
Typography on the Web
 
Microformats—the hidden treasure
Microformats—the hidden treasureMicroformats—the hidden treasure
Microformats—the hidden treasure
 
Advanced JavaScript Techniques
Advanced JavaScript TechniquesAdvanced JavaScript Techniques
Advanced JavaScript Techniques
 

Your JavaScript Library