Slide 1

Slide 1 text

JavaScript for PHP developers

Slide 2

Slide 2 text

Daniel Laxar @dlaxar

Slide 3

Slide 3 text

JavaScript for PHP developers

Slide 4

Slide 4 text

Let’s be honest…

Slide 5

Slide 5 text

JavaScript is weird.

Slide 6

Slide 6 text

What this talk is about Showing ways to deal with JS

Slide 7

Slide 7 text

What this talk isn’t about Make you like JS Basics of JS Make you a JS Pro

Slide 8

Slide 8 text

Today’s Agenda Facts Code Structures Gottchas node.js

Slide 9

Slide 9 text

JavaScript developed in 10 days first seen in Netscape works without jQuery Client Side scripting language runs in a VM incompatible as hell

Slide 10

Slide 10 text

PHP
 JS obj->prop class::prop
 obj.prop thing.prop

Slide 11

Slide 11 text

Code Structures IFFY Namespaces Objects Clojures

Slide 12

Slide 12 text

IFFY Immediately Invoked Function Expression (IFFY - wait… what?)

Slide 13

Slide 13 text

Why do that? Avoid polluting global space Enables private variables/functions

Slide 14

Slide 14 text

IFFY (function() { doStuffAndSoOn(); })(); Function Expression
 = Anonymous Function Definition Brackets Function Invocation
 = Invoke (Brackets = Function)

Slide 15

Slide 15 text

IFFY in the wild #1 (function(window, document, undefined) {'use strict’; /* Angular.js defined here */ })(window, document);

Slide 16

Slide 16 text

IFFY#1 - the point in doing so var undefined = ‘defined’; /* evil code */
 var o = {};
 if(o.prop === undefined) { /* dead code */ }
 
 (function(undefined) {
 var noPollution = ‘solar-energy’; /* not leaked globally */
 
 if(o.prop === undefined) { /* works */ } })();

Slide 17

Slide 17 text

IFFY in the wild #2 (function( global, factory ) { factory(/* … */); })(
 typeof window !== "undefined" ? window : this,
 function( window, noGlobal ) { /* jQ defined here */ }
 );

Slide 18

Slide 18 text

Namespaces \Human\Brain\Memory vs \Computer\Mainboard\Memory Human.Brain.Memory vs Computer.Mainboard.Memory

Slide 19

Slide 19 text

Namespaces // sane (i.e. with namespace) window.Human = window.Human || {}; window.Human.Brain = window.Human.Brain || {}; window.Human.Brain.Memory = something; // or window.Human.Brain.Memory = window.Human.Brain.Memory || {}; // global (i.e. no namespace)
 window.Memory = something;

Slide 20

Slide 20 text

Namespace in the wild #1 goog.require('goog.dom'); function sayHi() {
 var newHeader = goog.dom.createDom('h1', 
 {'style': 'background-color:#EEE'},
 'Hello world!'); goog.dom.appendChild(document.body, newHeader); }

Slide 21

Slide 21 text

Namespace in the wild #2 d3.map = function(object) {
 var map = new d3_Map();
 
 /* … bla foo */
 };

Slide 22

Slide 22 text

Objects i.e. Classes

Slide 23

Slide 23 text

Objects “Classes” are Functions The Functions are the Constructors Every Object has a Prototype (i.e. is an Instance of a Prototype) A Prototype is the recursive list of methods an object has

Slide 24

Slide 24 text

Objects var Car = function() { /*this is the constructor*/ }; Car.prototype.drive = function() { /* a ‘method’ */ }; var Ferrari = function() { /* this is another constructor */ }; Ferrari.prototype = new Car(); /* Ferrari inherits Car */

Slide 25

Slide 25 text

Closures i.e. doing some magic with variable scopes

Slide 26

Slide 26 text

(function Outer(g) {
 var string = ‘Hello, World’;
 function Inner() {}
 Inner.prototype.getPrivate = function() { return string; };
 g.Inner = Inner;
 })(window); (new Inner()).getPrivate();

Slide 27

Slide 27 text

Gottchas JavaScript doesn’t like you. Face it.

Slide 28

Slide 28 text

var a = 2;
 function test() {
 console.log(a);
 var a = 5;
 console.log(a);
 }
 test(); // Output?

Slide 29

Slide 29 text

var a = 2;
 function test() {
 var a = undefined; 
 console.log(a);
 a = 5;
 console.log(a);
 }
 test(); // Output?

Slide 30

Slide 30 text

Hoisting var a = 2;
 function test() {
 var a = undefined; 
 console.log(a);
 a = 5;
 console.log(a);
 }
 test(); // Output: undefined 5

Slide 31

Slide 31 text

var a = [“a”, “b”, “c”];
 
 for(var i = 0; i < a.length; i++) {
 setTimeout(function() { console.log(a[i]) }, 10);
 } // Output?

Slide 32

Slide 32 text

var a = [“a”, “b”, “c”];
 
 var i = 0;
 for(; i < a.length; i++) {} console.log(a[i]); console.log(a[i]); console.log(a[i]); // Output?

Slide 33

Slide 33 text

var a = [“a”, “b”, “c”];
 
 var i = 0;
 for(; i < a.length; i++) {} console.log(a[i]); console.log(a[i]); console.log(a[i]); // Output: c c c Context

Slide 34

Slide 34 text

node.js server side JS Google’s V8 going to kill PHP in 2012 not the first server side JS

Slide 35

Slide 35 text

node.js

Slide 36

Slide 36 text

node.js Slideshows suck.

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

JavaScript for PHP developers

Slide 39

Slide 39 text

JavaScript

Slide 40

Slide 40 text

JavaScript return this;