Download - JavaScript Secrets

Transcript
Page 1: JavaScript Secrets

JAVASCRIPT SECRETS

Cleaner Code/ Faster Apps

Wednesday, March 30, 2011

Page 2: JavaScript Secrets

• CTO, Modus Create.

• RIA UI/UX design

• High-end consulting

• Training / workshops

• Architectural guidance

• Application architecture

• Specialize in Sencha technologies.

About me

Wednesday, March 30, 2011

Page 3: JavaScript Secrets

Wednesday, March 30, 2011

Page 4: JavaScript Secrets

Wednesday, March 30, 2011

Page 5: JavaScript Secrets

Wednesday, March 30, 2011

Page 6: JavaScript Secrets

My books

Wednesday, March 30, 2011

Page 7: JavaScript Secrets

AGENDA •Discuss some of the secrets behind JavaScript

Wednesday, March 30, 2011

Page 8: JavaScript Secrets

• Advance use of JavaScript was limited until ~ 2005

• Field validation

•Mouse cursor trails

• Right click preventers

• Popup storms

• Lots of books written

•Many taught bad practices

1995-2005

Wednesday, March 30, 2011

Page 9: JavaScript Secrets

Today?• JavaScript frameworks in use more

•Do more, quicker!

•Way better debug tools

• Firebug

•Webkit inspector

• IE8 JS debug console (IE9 is a little better)

• HTML5 is gaining popularity

• Flash is (somewhat) threatened

Wednesday, March 30, 2011

Page 10: JavaScript Secrets

It’s a JavaScript world!

Wednesday, March 30, 2011

Page 11: JavaScript Secrets

Arithmetic Operators

Wednesday, March 30, 2011

Page 12: JavaScript Secrets

Arithmetic

• Arithmetic operators

• + (add/concatenate)

• - (subtract)

• * (multiplication)

• / (division)

• % (modulus/remainder)

Wednesday, March 30, 2011

Page 13: JavaScript Secrets

Arithmetic

Wednesday, March 30, 2011

Page 14: JavaScript Secrets

Remember • The + operator has a dual purpose

• Addition and concatenation

Wednesday, March 30, 2011

Page 15: JavaScript Secrets

Operator coercion• For -, *, / operators,

• JavaScript will try to convert strings into numbers.

Wednesday, March 30, 2011

Page 16: JavaScript Secrets

•When at all possible, try to perform math using numbers instead of strings.

•This will help reduce the chance of errors.

Wednesday, March 30, 2011

Page 17: JavaScript Secrets

Comparison Operators

Wednesday, March 30, 2011

Page 18: JavaScript Secrets

Comparison operators with if/then

•Most of us use if and then to control logic branches.

• There is a hidden danger with == and !=

• Sometimes the result of an expression test can lead to unexpected code behavior

Wednesday, March 30, 2011

Page 19: JavaScript Secrets

Take the following....

Wednesday, March 30, 2011

Page 20: JavaScript Secrets

Meet “Falsy” and “Truthy”

• Because JavaScript tries to coerce values, expressions using

• == and !=

• Expressions are boiled down to “Falsy” and “Truthy”

• There should be no room for “Falsy” and “Truthy” in your code.

Wednesday, March 30, 2011

Page 21: JavaScript Secrets

Seriously?

Wednesday, March 30, 2011

Page 22: JavaScript Secrets

== and !=are EVIL

Do not use them!

Do not use them!

Takeaway:

Wednesday, March 30, 2011

Page 23: JavaScript Secrets

Instead...

• Use === and !==

• They are not evil :)

• Get expected results every time

• both === and !== test for value and data type

•No coercion takes place

Wednesday, March 30, 2011

Page 24: JavaScript Secrets

Fight the evil.

All are false!Wednesday, March 30, 2011

Page 25: JavaScript Secrets

Hoisting

Wednesday, March 30, 2011

Page 26: JavaScript Secrets

Hoisting

• A mechanism for setting and creating things in a function when that function is executed.

•When a function is executed, two passes are actually made on the function by the JavaScript interpreter.

• It can lead to much pain when dealing with function statements.

Wednesday, March 30, 2011

Page 27: JavaScript Secrets

How a function really is interpreted at execution time

Wednesday, March 30, 2011

Page 28: JavaScript Secrets

How a function really is interpreted at execution time

Wednesday, March 30, 2011

Page 29: JavaScript Secrets

Interpretation case 2

Wednesday, March 30, 2011

Page 30: JavaScript Secrets

More on hoisting

•Due to hoisting,

• function expressions

• have their reference name created first and are assigned at execution time.

• function statements

• have their reference name created and are assigned before execution time

Wednesday, March 30, 2011

Page 31: JavaScript Secrets

Function statement vs. expression

Wednesday, March 30, 2011

Page 32: JavaScript Secrets

Function statement hoisting

Wednesday, March 30, 2011

Page 33: JavaScript Secrets

Function statement hoisting

Wednesday, March 30, 2011

Page 34: JavaScript Secrets

This should be impossible

Wednesday, March 30, 2011

Page 35: JavaScript Secrets

Know hoisting....

• According to the hoisting rules, the second jump function should have been created

• Firefox actually honors the conditional statement - what?!

• Some browsers follow these rules and some don’t

• This is because the language definition doesn’t really tell you what it’s supposed to do, so there are different implementations.

• Coding this way can lead to unpredictable behavior of your

Wednesday, March 30, 2011

Page 36: JavaScript Secrets

Avoid function statements!

Wednesday, March 30, 2011

Page 37: JavaScript Secrets

JS optimizations

Wednesday, March 30, 2011

Page 38: JavaScript Secrets

Asynchronous script tags

• For HTML5 enabled browsers

• Set async=‘async’ in your script tags.

• <script type=‘text/javascript’ src=’/myfile.js’ async=‘async’></script>

•Will allow JavaScript files to be non-blocking

Wednesday, March 30, 2011

Page 39: JavaScript Secrets

Deferred execution

• Set defer=‘defer’ in your script tags.

• <script type=‘text/javascript’ src=’/myfile.js’ defer=‘defer’></script>

•Will allow JavaScript code in those files to execute after the page has loaded.

•Does not work in all browsers :(

Wednesday, March 30, 2011

Page 40: JavaScript Secrets

Minification

Wednesday, March 30, 2011

Page 41: JavaScript Secrets

Facts about minification

• Reduce file size sent to the browser

• Increase interpretation speed of JS files by the browser

• Some Tools:

• Yui-Compressor

• Google Closure

• Packer

Wednesday, March 30, 2011

Page 42: JavaScript Secrets

A simple JS File

• 183 Bytes

Wednesday, March 30, 2011

Page 43: JavaScript Secrets

Minified

• 103 Bytes

• 44% savings

Wednesday, March 30, 2011

Page 44: JavaScript Secrets

A better JS file

• 184 Bytes

Why is it better!?!?Wednesday, March 30, 2011

Page 45: JavaScript Secrets

‘Better code’ === ‘more savings’

• 95 Bytes

• 49% Savings

Wednesday, March 30, 2011

Page 46: JavaScript Secrets

Loops

Wednesday, March 30, 2011

Page 47: JavaScript Secrets

Loops

Minifier can't claim space

Namespace lookups costly

Wednesday, March 30, 2011

Page 48: JavaScript Secrets

Faster loops

Minifier friendly

Faster LookupNo initializer

Wednesday, March 30, 2011

Page 49: JavaScript Secrets

Better reference usage

Wednesday, March 30, 2011

Page 50: JavaScript Secrets

Less than optimal lookups

Wednesday, March 30, 2011

Page 51: JavaScript Secrets

Less than optimal lookups

Wednesday, March 30, 2011

Page 52: JavaScript Secrets

Optimal lookups

Wednesday, March 30, 2011

Page 53: JavaScript Secrets

Recap

• + operators will concatenate strings, while -, *, / will coerce values

• Avoid “Truthy” and “Falsy” by using === and !==

• Use async and defer enabled Script tags when possible.

• Use function expressions

Wednesday, March 30, 2011

Page 54: JavaScript Secrets

Recap

•Minify your code when possible

•Develop “Minifier-friendly” code

• Create optimized loops

• Reduce namespace lookups by using local references

Wednesday, March 30, 2011

Page 55: JavaScript Secrets

Thanks!!!Questions?

Twitter : @_jdgMobile: 301-785-3030

Web: moduscreate.comEmail: [email protected]

Wednesday, March 30, 2011


Top Related