+ All Categories
Home > Technology > Awesomeness of JavaScript…almost

Awesomeness of JavaScript…almost

Date post: 29-Nov-2014
Category:
Upload: quinton-sheppard
View: 1,152 times
Download: 0 times
Share this document with a friend
Description:
an introduction to Javascript, its gotchas and construction.
66
Awesomeness of JavaScript… almost Twitter: #quintons Github id: quintons ‘The most misunderstood language has become the worlds most popular programming language’ Douglas Crockford – JavaScript architect, yahoo ‘JavaScript like the browsers is starting to move again’ Alex Russell – Software engineer, Chrome team and (ECMA) TC39 representative
Transcript
Page 1: Awesomeness of JavaScript…almost

Awesomeness of JavaScript…almostTwitter: #quintonsGithub id: quintons

‘The most misunderstood language has become the worlds most popular programming language’Douglas Crockford – JavaScript architect, yahoo

‘JavaScript like the browsers is starting to move again’Alex Russell – Software engineer, Chrome team and (ECMA) TC39 representative

Page 2: Awesomeness of JavaScript…almost

Agenda

• In the beginning

• Objects, Functions

• Overview of Scope

• Overview of JS primitives and properties

• Gotchers!

• Questions…?

Page 3: Awesomeness of JavaScript…almost

Agenda…for later

• Inheritance and the class’less language

• Prototypical (differential) inheritance

• Best Practices

• Modulising

• Multithreading with Webworkers

• Frameworks what they can offer

• Pollyfills, and how to use them

• Etc….

Page 4: Awesomeness of JavaScript…almost

In the beginning…

Page 5: Awesomeness of JavaScript…almost

…what about JavaScript?

• Java

• Self

• Scheme

• LiveScript

Page 6: Awesomeness of JavaScript…almost

…what about JavaScript?

• Java

• Self

• Scheme

• LiveScript

• JavaScript

Page 7: Awesomeness of JavaScript…almost

…what about JavaScript?

• Java

• Self

• Scheme

• LiveScript

• JavaScript

• ECMAScript

Page 8: Awesomeness of JavaScript…almost

Current implementations (2012)

• Chrome/V8

• Firefox/SpiderMonkey

• IE10/Chakra

• Safari/SquirrelFish (Webkit browsers)

• Opera/Carakan

Page 9: Awesomeness of JavaScript…almost

JS Engine Performance (2011) – running JSLint

Chrome 10 Chrome 13 Firefox 4 IE9 IE10 Opera 11 Safari 50

0.5

1

1.5

2

2.5

3

Browsers

mill

isec

onds

Datasource: http://javascript.crockford.com/performance.html

Page 10: Awesomeness of JavaScript…almost

JavaScript is everywhere…

• Built into every PC

• Building Mobile apps

• Titainum,

• PhoneGap,

• Sencha Touch,

• jQuery mobile etc…

• Server side – Rhino, NodeJs, Apache Sling, etc…

• Every Browser

Page 11: Awesomeness of JavaScript…almost

Proliferation of JS frameworks…Frameworks using MVC

• Backbone.js

• Spine.js

• Choco.js

• Agility.js

• Jamal.js

• PureMVC

• TrimJunction

• JavaScriptMVC

• SproutCore

• Cappuccino

• Google Web Toolkit (GWT)

• Google Closure

• Ember

• Etc…

Page 12: Awesomeness of JavaScript…almost

Proliferation of JS frameworks…DOM, AJAX, CSS and Utility Frameworks

• Jquery

• Qooxdoo

• ActiveJS

• ExtJS

• Knockout

• Eyeballs

• Sammy

• Spry (Adobe)

• Midori

• MooTools

• GWT (Java)

• YUI

• Prototype

• Etc…

Page 13: Awesomeness of JavaScript…almost

…What about Code?….

Page 14: Awesomeness of JavaScript…almost

Objects, Functions

• Run to completion• No multithreading (almost)

• No type testing

• All objects are mutable

• Classless language – differential inheritance.

• functions and closures – data hiding/private state

Page 15: Awesomeness of JavaScript…almost

Objects, Functions

• Data hiding

Page 16: Awesomeness of JavaScript…almost

Objects, Functions

• Functions are first class• Can be passed as an argument

• Can be returned from a function

• Can be assigned to a variable

• Can be stored in an object or array

• arguments passed by value

• Function objects inherit from ‘Function.prototype’

Page 17: Awesomeness of JavaScript…almost

Objects, Functions

• Function

• Method

• Class

• Constructor

• Module

• Object

Page 18: Awesomeness of JavaScript…almost

Objects, Functions

• Function• Defined with the Function constructor

• A function declaration

• A function expression

Page 19: Awesomeness of JavaScript…almost

Objects, Functions

• Function - A function declaration can be called before its

declaration.

Page 20: Awesomeness of JavaScript…almost

Objects, Functions

• Function - A function declaration can be called before its

declaration.

…Why?….

Page 21: Awesomeness of JavaScript…almost

Objects, Functions

• Function • hoisting of variables.

Page 22: Awesomeness of JavaScript…almost

Objects, Functions

• Example of a first class function

Page 23: Awesomeness of JavaScript…almost

Objects, Functions

• Functions – arguments object• Not a true array

• …for example

Page 24: Awesomeness of JavaScript…almost

Objects, Functions

• Functions – arguments object• arguments only available inside a function

• If too few arguments are passed, the remaining will be

undefined

Page 25: Awesomeness of JavaScript…almost

Objects, Functions

• Functions – arguments

• ES6 (Harmony) proposal

• Little sugar – ES3/5 usage

Page 26: Awesomeness of JavaScript…almost

Objects, Functions

• Everything's an object

• And objects act like maps

Page 27: Awesomeness of JavaScript…almost

Objects, Functions

• Every property has a unique key string associated

to that object.

• Make an instance of an object use the ‘new’

keyword

Page 28: Awesomeness of JavaScript…almost

Objects, Functions

• Constructor

Page 29: Awesomeness of JavaScript…almost

Operators

• Arithmetic

• Comparison

• Logical

• Bitwise

Page 30: Awesomeness of JavaScript…almost

Error Catching/Throwing

• Throw Statement

Page 31: Awesomeness of JavaScript…almost

this

Page 32: Awesomeness of JavaScript…almost

Closures/scope and this

• I do ‘this’ and you do that…• ‘this’ keyword refers to the owner of the object that is

invoked

• ‘this’ allows a method to know what object it is

concerned with

• Without ‘this’ prototypical inheritance would not be

possible.

Page 33: Awesomeness of JavaScript…almost

• I do ‘this’ and you do that…continued• function declaration/expression – ‘this’ assigned to the

global scope

• function as a method – ‘this’ assigned to the object

containing the function

• Inner function - ‘this’ does not get access to the outer

function’s ‘this’

Closures/scope and this

Page 34: Awesomeness of JavaScript…almost

• I do ‘this’ and you do that…continued

Closures/scope and this

Page 35: Awesomeness of JavaScript…almost

• I do ‘this’ and you do that…continued• You can change the context of what the object ‘this’

refers to.

Closures/scope and this

Page 36: Awesomeness of JavaScript…almost

• Only function creates scope, no block scope {}

Closures/scope and this

Page 37: Awesomeness of JavaScript…almost

• ES5 to the rescue!...

Closures/scope and this

Page 38: Awesomeness of JavaScript…almost

Primitives

• Numbers

• Boolean

• String

• Undefined

• Null

Page 39: Awesomeness of JavaScript…almost

Primitives

• Numbers• 1 type, 64 bit floating point (aka ‘double’)

• Associative Law does not hold true – ‘when dealing

only with addition or only with multiplication we get

the same result regardless of the order in which we do

the operations’

Page 40: Awesomeness of JavaScript…almost

Primitives

• Numbers (continued)

• Max number (253) 9007199254740992

• Methods• toExplonential()

• toFixed()

• toLocalString()

• toPrecision()

• toString()

• valueOf()

Page 41: Awesomeness of JavaScript…almost

Primitives

• Numbers (…and more)

• All numbers inherit from Number.prototype

• Numbers first class objects

• A number can be stored in a variable

• A number can be passed as a parameter

• A number can be returned from a function

• A number can be stored in an object

Page 42: Awesomeness of JavaScript…almost

Primitives

• Math object• abs()

• acos()

• asin()

• atan()

• atab2()

• ceil()

• cos()

• exp()

• floor()

• log()

• max()

• min()

• pow()

• random()

• round()

• sin()

• sqrt()

• tan()

Page 43: Awesomeness of JavaScript…almost

Primitives

• parseInt/parseFloat• Converts values into a number

• Converting string to a number stops at first non-

numerical character

• parseFloat() defaults to base 10

Page 44: Awesomeness of JavaScript…almost

Primitives

Boolean

True or False

1 or 0

Page 45: Awesomeness of JavaScript…almost

Primitives

• String• A sequence of 0 or more 16bit Unicode characters

• Strings are immutable

Page 46: Awesomeness of JavaScript…almost

Primitives

• String (…continued)

• String literals can use single or double quotes

• Multilines – evil!

• Converting number to a string

Page 47: Awesomeness of JavaScript…almost

Primitives

• String (…and more)

• Convert string to a number

• String.length

• ‘length’ property is the number of 16bit characters

in a string

• All extended (outside of UCS-2) characters are

counted as length of 2

Page 48: Awesomeness of JavaScript…almost

Primitives

• String methods• charAt()

• charCodeAt()

• concat()

• indexOf()

• lastIndexOf()

• localCompare()

• match()

• replace()

• search()

• slice()

• split()

• substr()

• substring()

• toLocalLowerCase()

• toLocalUpperCase()

• toLowerCase()

• toString()

• toUpperCase()

Page 49: Awesomeness of JavaScript…almost

Primitives

• String methods (…continued)

• trim() ES5

• trimLeft() ES5

• trimRight() ES5

Page 50: Awesomeness of JavaScript…almost

Primitives

• String methods (…continued)

• trim() ES5

• trimLeft() ES5

• trimRight() ES5

…What about ES3?….

Page 51: Awesomeness of JavaScript…almost

Primitives

• String methods (…continued)

• trim() – what about ES3?

Page 52: Awesomeness of JavaScript…almost

Special Type of Object

• Array• Array inherits from ‘Object’

• Length of an array auto incremented

• No type required for each value

• Array.length• Always 1 larger than the highest index

• Looping use a for loop

• Do not use a for…in loop with Arrays

Page 53: Awesomeness of JavaScript…almost

Special Type of Object

• Array• Array inherits from ‘object’

• Length of an array auto incremented

• No type required for each value

• Array.length• Always 1 larger than the highest index

• Looping use a for loop

• Do not use a for…in loop with Arrays

…Why?….

Page 54: Awesomeness of JavaScript…almost

Special Type of Object

• Array (…continued)

• For in loop…

‘Array’ inherits from ‘Object’

Page 55: Awesomeness of JavaScript…almost

Special Type of Object

• Array (…continued)

• Array literals

• An array literal uses []

• It can contain any number of values of any type

• Deleting elements

• Removes the element but leaves a hole

Page 56: Awesomeness of JavaScript…almost

Special Type of Object

• Array (…continued)

• Deleting elements….creates a hole

• Arrays are not true arrays – inherits from ‘Object’

Page 57: Awesomeness of JavaScript…almost

Special Type of Object

• Array (…continued)

• Are you an array?

Page 58: Awesomeness of JavaScript…almost

Special Type of Object

• Date• …..too many methods to list! (48)

• Y2K compliant – was not initially

Page 59: Awesomeness of JavaScript…almost

…Watch out!….Gotchers…

Page 60: Awesomeness of JavaScript…almost

Watch out!

• Accidental collision with keywords used as properties

Page 61: Awesomeness of JavaScript…almost

Watch out!

• For…in

• functions that inherit from prototype are included in

the for..in enumeration

• NaN

• Special number – Not a Number

• NaN is evil

• Use isNaN() to test for NaN

Page 62: Awesomeness of JavaScript…almost

Watch out!

• NaN (…continued)

• NaN === NaN = false

• NaN !== NaN = true

• Silent Errors – automatic semicolon insertion

• Switch statement, will fall through if no break statement is

declared

Page 63: Awesomeness of JavaScript…almost

Watch out!

• Immediately invocable function expression

• Infinity/-Infinity

• 1/0 === Infinity

• -1/0 === -Infinity

• Test for Infinity/-Infinity use isFinite()

Page 64: Awesomeness of JavaScript…almost

Watch out!

• Relying on type coercion is not a good idea.

• Always use ‘===‘ not ‘==‘

Page 65: Awesomeness of JavaScript…almost

Watch out!

• Operators and Concatenation

Page 66: Awesomeness of JavaScript…almost

Until next time…Twitter: #quintonsGithub id: quintons


Recommended