43
Fork JS improvements November 17th, 2011

Fork 3.0 JS improvements

Embed Size (px)

Citation preview

Page 1: Fork 3.0 JS improvements

Fork JS improvementsNovember 17th, 2011

Page 2: Fork 3.0 JS improvements

Summary

Page 3: Fork 3.0 JS improvements

‣ General code cleanup

Summary

Page 4: Fork 3.0 JS improvements

‣ General code cleanup

‣ Variables

Summary

Page 5: Fork 3.0 JS improvements

‣ General code cleanup

‣ Variables

‣ Event binding

Summary

Page 6: Fork 3.0 JS improvements

‣ General code cleanup

‣ Variables

‣ Event binding

‣ jQuery 1.7

Summary

Page 7: Fork 3.0 JS improvements

General code cleanup

Page 8: Fork 3.0 JS improvements

General code cleanup

Oldif(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend ={ // init something like a constructor init: function() { // call functions },

// end eoo: true;}

$(document).ready(jsFrontend.init);

Page 9: Fork 3.0 JS improvements

General code cleanup

New

var jsFrontend ={ // init something like a constructor init: function() { // call functions }}

$(jsFrontend.init);

Page 10: Fork 3.0 JS improvements

General code cleanup

Example

Page 11: Fork 3.0 JS improvements

General code cleanup

Style :(

Page 12: Fork 3.0 JS improvements

General code cleanup

Style :)

Page 13: Fork 3.0 JS improvements

Variables

Page 14: Fork 3.0 JS improvements

Scope

Variables

Page 15: Fork 3.0 JS improvements

Scope is determined by where a variable is declared, and in some cases whether the var keyword is used.

Scope

Variables

Page 16: Fork 3.0 JS improvements

Scope: global

Variables

Page 17: Fork 3.0 JS improvements

Can be referenced anywhere in the document but must be:

Scope: global

Variables

Page 18: Fork 3.0 JS improvements

Can be referenced anywhere in the document but must be:

‣ Declared outside a function, with or without the var keyword

Scope: global

Variables

Page 19: Fork 3.0 JS improvements

Can be referenced anywhere in the document but must be:

‣ Declared outside a function, with or without the var keyword

‣ Declared inside a function, without the var keyword, but only once the function is called

Scope: global

Variables

Page 20: Fork 3.0 JS improvements

Scope: local

Variables

Page 21: Fork 3.0 JS improvements

Can only be referenced within the function it is declared but must be:

Scope: local

Variables

Page 22: Fork 3.0 JS improvements

Can only be referenced within the function it is declared but must be:

‣ Declared inside a function, with the var keyword

Scope: local

Variables

Page 23: Fork 3.0 JS improvements

For speed and maintenance

Why use variables?

Variables

Page 24: Fork 3.0 JS improvements

Example 1

Variables

var value = 5; // gobal since we're in the global scope, outside any function

jsFrontend.faq.feedback ={ init: function() { // variables var number = 3; // local (we're already inside this anonymous function) result = 100; // global once this function is called

var $header = $('#header'); // local (we're already inside this anonymous function) $navigation = $('#navigation'); // global once this function is called

// bind click event $header.on('click', function() { var color = 'blue'; // local endResult = 'green'; // global once this function is called

var $body = $('body'); // local $frame = $('#frame'); // global once this function is called

number++; // this variable is declared outside of this function's scope, // so changes will affect not only this function's scope, // but the declaring function's scope // which means that this variable is "re-used" every time }); }}

Page 25: Fork 3.0 JS improvements

Example 2

Variables

Page 26: Fork 3.0 JS improvements

Event binding

Page 27: Fork 3.0 JS improvements

Event binding

Old

Page 28: Fork 3.0 JS improvements

Event binding

New

Page 29: Fork 3.0 JS improvements

jQuery 1.7

Page 30: Fork 3.0 JS improvements

http://blog.jquery.com/2011/11/03/jquery-1-7-released/

Changes

jQuery 1.7

Page 31: Fork 3.0 JS improvements

‣ New event API .on() and .off() used for bind, live and delegate

Changes

jQuery 1.7

Page 32: Fork 3.0 JS improvements

jQuery 1.7

Bind

Page 33: Fork 3.0 JS improvements

jQuery 1.7

Bind

Page 34: Fork 3.0 JS improvements

jQuery 1.7

Live

Page 35: Fork 3.0 JS improvements

jQuery 1.7

Live

Page 36: Fork 3.0 JS improvements

jQuery 1.7

Delegate

Page 37: Fork 3.0 JS improvements

jQuery 1.7

Delegate

Page 38: Fork 3.0 JS improvements

‣ New event API: .on() and .off() used for bind, live and delegate

‣ Better HTML5 support (footer, header, section, ...)

Changes

jQuery 1.7

Page 39: Fork 3.0 JS improvements

‣ New event API: .on() and .off() used for bind, live and delegate

‣ Better HTML5 support (footer, header, section, ...)

‣ event.layerX and event.layerY

Changes

jQuery 1.7

Page 40: Fork 3.0 JS improvements

‣ New event API: .on() and .off() used for bind, live and delegate

‣ Better HTML5 support (footer, header, section, ...)

‣ event.layerX and event.layerY

‣ $.isNaN() replaced with $.isNumeric()

Changes

jQuery 1.7

Page 41: Fork 3.0 JS improvements

‣ New event API: .on() and .off() used for bind, live and delegate

‣ Better HTML5 support (footer, header, section, ...)

‣ event.layerX and event.layerY

‣ $.isNaN() replaced with $.isNumeric()

‣ $.event.proxy() use $.proxy() instead

Changes

jQuery 1.7

Page 42: Fork 3.0 JS improvements

Questions?