javascript, do you speak it!

37
JavaScript Do you speak it! Tuesday, August 2, 2011

Upload: victor-porof

Post on 07-Nov-2014

3.180 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Javascript, Do you speak it!

JavaScriptDo you speak it!

Tuesday, August 2, 2011

Page 2: Javascript, Do you speak it!

!"#$%&'(&'&)**+,&-*.%/012%*3%*4&5#663*78(9::;#%7/<=$&>:"#$%&'(&'&)*

Tuesday, August 2, 2011

Page 3: Javascript, Do you speak it!

“The world’s most

misunderstood

programming language”

Tuesday, August 2, 2011

Page 4: Javascript, Do you speak it!

We’ll talk about

why JavaScript is the most misunderstood programming language

Object Oriented Programming in JavaScript

function scope, closures, circular references

making the web a little faster

Tuesday, August 2, 2011

Page 5: Javascript, Do you speak it!

JavaScript

aka Mocha

aka LiveScript

aka JScript aka ECMAScript

Tuesday, August 2, 2011

Page 6: Javascript, Do you speak it!

Java... Script?

somehow related to Java?

maybe a subset?

less capable interpreted version of Java?

Tuesday, August 2, 2011

Page 7: Javascript, Do you speak it!

Java... Script?

developed by Netscape in 1997 (vs. 3.0 in 1999)

Java prefix intended to create confusion

JavaScript is not interpreted Java

Tuesday, August 2, 2011

Page 8: Javascript, Do you speak it!

Design errors

overloading “+” means both addition and string concatenation with type conversion

error-prone “with” statement

reserved word policies are much too strict

semicolon insertion was “a huge mistake”

1 + “1” = ?1 + +”1” = ?

Tuesday, August 2, 2011

Page 9: Javascript, Do you speak it!

Bad implementations

earlier implementations were buggy

embedded horribly in browsers

did not respect the ECMA standard

Tuesday, August 2, 2011

Page 10: Javascript, Do you speak it!

Evolution

first versions of JavaScript were quite weak

no exception handling, inner functions

in its present form, it’s a full OOP language

ECMAScript 5.1

Tuesday, August 2, 2011

Page 11: Javascript, Do you speak it!

Overview

types and operators, core objects, methods

does not have classes, but this functionality is accomplished with object prototypes

functions are objects (and can be passed around as parameters)

Tuesday, August 2, 2011

Page 12: Javascript, Do you speak it!

(Loosely) TypesNumber

String

Boolean

Object (Function, Array, Date, RegExp)

Null

Undefined

typeofinstanceof

Tuesday, August 2, 2011

Page 13: Javascript, Do you speak it!

Lisp in C’s clothing

C-like syntax (curly braces, for statement...)

appears to be a procedural language

it’s actually a functional language!

Tuesday, August 2, 2011

Page 14: Javascript, Do you speak it!

Haskell vs. JSpalindr :: [Int] -> Boolpalindr [] = Truepalindr [x] = Truepalindr xs = (head xs == last xs) && palindr (tail (init xs))

function palindr(xs) { var i, len = xs.length; for (i = 0; i < len / 2; i++) if (xs[i] !== xs[len - i - 1]) return false;

return true;}

Tuesday, August 2, 2011

Page 15: Javascript, Do you speak it!

Objects

JavaScript is fundamentally about objects

usually implemented as hashtables

objects are created using constructors

use a namespace for them (don’t modify objects you don’t own)

use JS sugar: var obj = {}; and var arr = [];

Tuesday, August 2, 2011

Page 16: Javascript, Do you speak it!

OOP

polymorphism

encapsulation

inheritance

abstractization

class Dog { public string name = “”; public int yearsOld = 0;

void bark(int times); void bark(float volume); void sleep();}

class Pudel : Dog { public int cuteness = 5;}

Dog pinky = new Dog();Dog leyla = new Pudel();

Tuesday, August 2, 2011

Page 17: Javascript, Do you speak it!

Objects in JavaScriptMyNamespace.Dog = function(){ this.name = “”; this.yearsOld = 0;};

MyNamespace.Dog.prototype = { bark: function(param){ }, sleep: function(){ }};

var pinky = new Dog();var leyla = new Dog();leyla.cuteness = 5;

Tuesday, August 2, 2011

Page 18: Javascript, Do you speak it!

Objects in JavaScriptMyNamespace.Dog = function(){ this.name = “”; this.yearsOld = 0;};

MyNamespace.Dog.prototype = { bark: function(param){ }, sleep: function(){ }};

var pinky = new Dog();var leyla = new Dog();leyla.cuteness = 5;

no private members?

Tuesday, August 2, 2011

Page 19: Javascript, Do you speak it!

Closuresfunction foo(a, b){ function bar() { return a + b; }

return bar();}

function foo2(a, b){ function bar(c) { return a + b + c; }

return bar;}

var res1 = foo(5, 2);var res2 = foo2(5, 2);

var res3 = res2(3);

Tuesday, August 2, 2011

Page 20: Javascript, Do you speak it!

Private membersMyNamespace.Dog = function(){ var name = “”; var yearsOld = 0;

this.bark = function(param){ if (“Number” === typeof param) { } else { } }; this.sleep = function(){ };};

var pinky = new Dog();

Tuesday, August 2, 2011

Page 21: Javascript, Do you speak it!

Private membersMyNamespace.Dog = function(paramName, paramYearsOld){ var name = paramName; var yearsOld = paramYearsOld;

this.bark = function(param){ }; this.sleep = function(){ };

get nrYearsOld(){ return yearsOld; };};

var pinky = new Dog();console.log(pinky.nrYearsOld);

Tuesday, August 2, 2011

Page 22: Javascript, Do you speak it!

Why is JavaScript slow?

Bad compilation?

Tuesday, August 2, 2011

Page 23: Javascript, Do you speak it!

Why is JavaScript slow?

No compilation!*

Tuesday, August 2, 2011

Page 24: Javascript, Do you speak it!

Why is JavaScript slow?

No compilation!*

*yes, interpreters are a lot smarter these days, but..

Tuesday, August 2, 2011

Page 25: Javascript, Do you speak it!

Scope chainsfunction scope1(){ var elem2;

function scope2(){ var elem2; function scope3(){ var elem3; var func = window.alert;

return { something: elem4, somethingElse: elem5 } } }}

Tuesday, August 2, 2011

Page 26: Javascript, Do you speak it!

Memory leaks

function leakMemory(){ var elem = somethingThatOccupiesLotsOfMemory; function inner(){ }; return inner;}

var res1 = leakMemory();var res2 = leakMemory();

Tuesday, August 2, 2011

Page 27: Javascript, Do you speak it!

Circular references

function leakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ };}

Tuesday, August 2, 2011

Page 28: Javascript, Do you speak it!

Circular references

function dontLeakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ };

elem = null;}

Tuesday, August 2, 2011

Page 29: Javascript, Do you speak it!

Circular references

function leakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ };

return elem;}

Tuesday, August 2, 2011

Page 30: Javascript, Do you speak it!

Circular references

function dontLeakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ }; try { return elem; } finally { elem = null; }}

Tuesday, August 2, 2011

Page 31: Javascript, Do you speak it!

Scope bindingfunction scopeBinding(){ this.elem = {}

function addMemberToElem(){ this.elem.newProperty = 5; // fail };

addMemberToElem();}

Tuesday, August 2, 2011

Page 32: Javascript, Do you speak it!

Scope bindingfunction scopeBinding(){ this.elem = {};

function addMemberToElem(){ this.elem.newProperty = 5; };

addMemberToElem.call(this);}

Tuesday, August 2, 2011

Page 33: Javascript, Do you speak it!

Scope bindingfunction scopeBinding(){ this.elem = {}

function addMemberToElem(propertyValue){ this.elem.newProperty = propertyValue; };

addMemberToElem.call(this, 5);}

Tuesday, August 2, 2011

Page 34: Javascript, Do you speak it!

Scope bindingfunction scopeBinding(){ this.elem = {}

var addMemberToElem = function(propertyValue){ this.elem.newProperty = propertyValue; }.bind(this);

addMemberToElem(5);}

Tuesday, August 2, 2011

Page 35: Javascript, Do you speak it!

“But why should I do OOP with JavaScript?”

Tuesday, August 2, 2011

Page 36: Javascript, Do you speak it!

“But why should I do OOP with JavaScript?”

Tuesday, August 2, 2011

Page 37: Javascript, Do you speak it!

?Tuesday, August 2, 2011