introduction à javascript

51
Javascript Les fondamentaux Jean-pierre VINCENT (indépendant) Pierre lagarde (Microsoft)

Upload: microsoft

Post on 07-Jul-2015

262 views

Category:

Technology


0 download

DESCRIPTION

JavaScript est désormais omniprésent et rend possible l'écriture d'applications complexes et riches. Mais il est souvent mal connu des développeurs orientés objets classiques comme ceux pratiquant le C#, le Java ou le PHP. Cette session passera donc en revue les bases du langage JavaScript, ses spécificités comme les portées, les closures, le this différent de ce que vous pouvez connaître. Bref, vous verrez qu'il y a des pièges à éviter et qu'il ne faut pas négliger JavaScript. Il est très puissant mais potentiellement différent de ce vous connaissez déjà. Venez apprendre à le respecter avant de le maitriser!

TRANSCRIPT

Page 1: Introduction à JavaScript

Javascript

Les fondamentaux

Jean-pierre VINCENT (indépendant) Pierre lagarde (Microsoft)

Page 2: Introduction à JavaScript

Qui ça ?Jean-pierre VINCENT

braincracking.org (veille techno)@theystolemynick

12 ans de WebConsultant, formateur, expertise

Page 3: Introduction à JavaScript
Page 4: Introduction à JavaScript

Pourquoi Javascript ?

Page 5: Introduction à JavaScript

Présent partout●Navigateur●Jeux Web (remplace Flash)●Mobile (Phonegap ...)●Télévisions●Serveur (Node.js, ...)●Software (Chromeless, ...)●OS (JoliCloud, WebOS...)●Windows 8 !

Page 6: Introduction à JavaScript

Mauvaise réputation

Page 7: Introduction à JavaScript

Mauvaise réputation

parseInt('06'); // 6parseInt('08'); // 0

wftjs.com

Page 8: Introduction à JavaScript

Mauvaise réputation

typeof NaN // numberNaN === NaN // false

typeof null // objectnull instanceof Object // false

wftjs.com

Page 9: Introduction à JavaScript

Mauvaise réputation

(1.7976931348623157e+308 ===1.7976931348623158e+308 )// true!

alert(111111111111111111111); // alerts 111111111111111110000

9999999999999999 //=> 10000000000000000

wftjs.com

Page 10: Introduction à JavaScript

APIs● DOM (window.document)

● Node

● WinRT

● ...

Page 11: Introduction à JavaScript

Compliqué ?

Page 12: Introduction à JavaScript

Différent !

Page 13: Introduction à JavaScript

Historique court● Né pendant la guerre (95)● En quelques semaines● Influence Erlang, Lisp, Python, Askell

IE et Netscape d'accord pourEcmaScript 3

Page 14: Introduction à JavaScript

Evolution

●EcmaScript 5●Harmony●EcmaScript.Next●EcmaScript.Next.Next

Page 15: Introduction à JavaScript

En attendant ...

EcmaScript 3

Page 16: Introduction à JavaScript

Objectifs

●Portée des variables (var + function)

●Contexte (this)

●Function()

Page 17: Introduction à JavaScript

Portée des variables

1 closure = 1 portée

Closure = function() {PORTÉE

}

Page 18: Introduction à JavaScript

Portée des variablestest1 = function() {

var x = 1;

console.log(x); // 1}();console.log(x); // undefined

Page 19: Introduction à JavaScript

Portée des variablestest1 = function() {

var x = 1;test2 = function() {var x = 2;

console.log(x); // 2}();console.log(x); // ?

}();console.log(x); // undefined

Page 20: Introduction à JavaScript

Portée des variablestest1 = function() {

var x = 1;test2 = function() {var x = 2;

console.log(x); // 2}();console.log(x); // 1

}();console.log(x); // undefined

Page 21: Introduction à JavaScript

Portée des variablestest1 = function() {

var x = 1;test2 = function() {var x = 2;test3 = function() {console.log(x); // 2}();console.log(x); // 2}();console.log(x); // 1

}();console.log(x); // undefined

Page 22: Introduction à JavaScript

Démo « porté et boucle »

Page 23: Introduction à JavaScript

Morales

1 Toujours utiliser var

Page 24: Introduction à JavaScript

Application pratique

Démo « module pattern »

Page 25: Introduction à JavaScript

Créer un scope1/3 : fonction anonyme

function() {var privateVariable = true;console.log( privateVariable );

}

Page 26: Introduction à JavaScript

Créer un scope1/3 : fonction anonyme

function() {var privateVariable = true;console.log( privateVariable );

}=> parse error

Page 27: Introduction à JavaScript

Créer un scope2/3 : parler gentiment au parser

( function() {var privateVariable = true;console.log( privateVariable );

})=> rien

Page 28: Introduction à JavaScript

Créer un scope3/3 : auto-exécution

( function() {var privateVariable = true;console.log( privateVariable );

})()=> true

Page 29: Introduction à JavaScript

Application pratique

(function() {var privateVariable = true;window.init = function() {

console.log( privateVariable );}

}())

init(); // trueconsole.log(privateVariable);//undefined

Page 30: Introduction à JavaScript

Morales

1 Toujours utiliser var2 Utiliser le pattern module

Page 31: Introduction à JavaScript

Contexte

this = contexte d'exécution

Page 32: Introduction à JavaScript

Contexte

Démo « contexte »

Page 33: Introduction à JavaScript

Contexte - DOMmyClass = function() {

this.id = 'myClass';this.getId = function() {

console.log(this.id);};

};

myObject = new myClass();document.body.onclick = myObject.getId;// document.body.id

Page 34: Introduction à JavaScript

Contexte – fix portéemyClass = function() {

this.id = 'myClass';var me = this;this.getId = function() {

console.log(me.id);

};};myObject = myClass();

document.body.onclick = myObject.action;// 'myClass'

Page 35: Introduction à JavaScript

Contexte – fix portée

Démo du fix

Page 36: Introduction à JavaScript

Morales

1 Toujours utiliser var2 Utiliser le pattern module3 Verrouiller le contexte

Page 37: Introduction à JavaScript

Function()Déclarations● function action() {}

●action = function() {}

Page 38: Introduction à JavaScript

Function()Déclarations● function action() {}

●action = function() {}

●Exécution action();

Page 39: Introduction à JavaScript

Function()

Un petit jeu ?

Page 40: Introduction à JavaScript

function action()Le hoisting, c'est pratique

action(); // true

function action() { console.log(true);}

Page 41: Introduction à JavaScript

function action()Le hoisting, c'est dangereuxif( 1 === 1) { function action() { console.log('1 === 1'); }} else { function action() { console.log('1 !== 1'); }}action(); // 1 !== 1

Page 42: Introduction à JavaScript

function action()Implicitement sensible à la portée

(function() {function action() {

console.log('action');}

}())action(); // undefined

Page 43: Introduction à JavaScript

var action = function()

Plus explicite donc moins dangereux

Page 44: Introduction à JavaScript

Morales

1 Toujours utiliser var2 Utiliser le pattern module3 Verrouiller le contexte4 déclarer ses fonction avec var action = function() { }

Page 45: Introduction à JavaScript

Avant la Démo « mise en pratique »

● Utilisation des écrasements de fonction

Page 46: Introduction à JavaScript

Utilisation des écrasements de fonction

var bind = function( el, ev, callback) { if(document.body.attachEvent){

el.attachEvent('on'+ev, callback); } else {

el.addEventListener( ev, callback);}

};

Le test est fait à chaque exécution

Page 47: Introduction à JavaScript

return function()var bind =function( el, ev, callback) { if(document.body.attachEvent) return function(el, ev, callback) {element.attachEvent('on'+event, callback); }; else return function(el, ev, callback) {

el.addEventListener( ev, callback); };}();

Page 48: Introduction à JavaScript

Démo « mise en pratique »

Problème : écouter la liste d'éléments

Page 49: Introduction à JavaScript

Teasing : 17h30 amphi Havane

OOJS, bonnes pratiques...

Page 50: Introduction à JavaScript

Conclusion

Javascript est différent, apprenez le

Page 51: Introduction à JavaScript

Questions ?Jean-pierre VINCENTbraincracking.org@theystolemynick

RDV maintenant :stand Generative Objects(N° 55 à côté HP)