Transcript
Page 1: Poo et JavaScript, une notion trop incomprise

POO et JavaScriptUNE NOTION TROP INCOMPRISE

Mathieu ROBIN

Page 2: Poo et JavaScript, une notion trop incomprise

« The World's Most Misunderstood Programming Language »

Douglas Crockford

Page 3: Poo et JavaScript, une notion trop incomprise

• Mathieu ROBIN (@mathrobin)• 25 ans• Ingénieur logiciel chez Companeo• Contributeur Developpez.com, communauté francophone de développeurs• Blogueur

• http://www.mathieurobin.com• http://www.recettesensolo.com

Twitter : @mathrobin Blog : MathieuRobin.com Rédacteur / Correcteur Developpez.com Chroniqueur francophone jQuery, jQuery UI, jQuery Mobile Accessoirement cuisinier

Page 4: Poo et JavaScript, une notion trop incomprise
Page 5: Poo et JavaScript, une notion trop incomprise

Classe

Classe

Objet

Objet

Objet

Objet

Interface

Page 6: Poo et JavaScript, une notion trop incomprise
Page 7: Poo et JavaScript, une notion trop incomprise

Classe

Classe

Objet

Objet

Objet

Objet

Interface

Page 8: Poo et JavaScript, une notion trop incomprise

Objet

Page 9: Poo et JavaScript, une notion trop incomprise

NumberStringArray

FunctionObjectDate

Boolean

Page 10: Poo et JavaScript, une notion trop incomprise

Exceptions

Page 11: Poo et JavaScript, une notion trop incomprise

undefinednullNaN

infinity

Page 12: Poo et JavaScript, une notion trop incomprise

Tout objet a un prototype

Page 13: Poo et JavaScript, une notion trop incomprise

Tout objet peut servir de prototype à un autre objet

Page 14: Poo et JavaScript, une notion trop incomprise

Object est le pèrede tous les objets

Page 15: Poo et JavaScript, une notion trop incomprise

Object.prototype ---> null

Page 16: Poo et JavaScript, une notion trop incomprise

Bravo !

Page 17: Poo et JavaScript, une notion trop incomprise
Page 18: Poo et JavaScript, une notion trop incomprise

var objetNu = new Object();

Page 19: Poo et JavaScript, une notion trop incomprise

objetNu: Objectprototype: Object

constructor: function Object()hasOwnProperty()isPrototypeOf()propertyIsEnumerable()toLocaleString()toString()valueOf()

Page 20: Poo et JavaScript, une notion trop incomprise
Page 21: Poo et JavaScript, une notion trop incomprise

var monLitteral = {};

Page 22: Poo et JavaScript, une notion trop incomprise

var monLitteral = [];

Page 23: Poo et JavaScript, une notion trop incomprise

var monLitteral = {

what : 'ConFoo',

'when' : '2012',

where : 'Montréal',

why : 42,

''and-for'' : 'speaking'

};

Page 24: Poo et JavaScript, une notion trop incomprise

Pas besoin de ' ou de '' pour les clés Vous pouvez les utiliser quand même Exception : ''foo-bar''

Présence d'un caractère « illégal » Utilisation d'un mot réservé

• Ne s'applique plus avec EcmaScript 5

Page 25: Poo et JavaScript, une notion trop incomprise

Accès aux propriétés :

console.log(monLitteral.what); // ConFoo

console.log(monLitteral['when']); // 2012

console.log(monLitteral.who); // undefined

Page 26: Poo et JavaScript, une notion trop incomprise

Modification des propriétés :

monLitteral.what = 'Confoo' ;

monLitteral['when'] = 'Février-Mars 2012' ;

monLitteral.who = 'John Doe' ;

Page 27: Poo et JavaScript, une notion trop incomprise

Suppression d'une propriété :

monLitteral.what = null ;

monLitteral.when = undefined ;

delete monLitteral.who ;

Page 28: Poo et JavaScript, une notion trop incomprise
Page 29: Poo et JavaScript, une notion trop incomprise
Page 30: Poo et JavaScript, une notion trop incomprise

var monObjet = {

what : 'ConFoo',

when : '2012',

where : 'Montréal'

};

monObjet.sayHello = function () {

console.log('Hello ConFoo 2012 !') ;

}

Page 31: Poo et JavaScript, une notion trop incomprise

var sayHello = function () {

console.log('Hello ConFoo !') ;

}

function sayHello () {

console.log('Hello ConFoo !') ;

}

Page 32: Poo et JavaScript, une notion trop incomprise

if ( true ) {

function sayHello () {

console.log('Hello ConFoo !') ;

}

}

else {

function sayHello () {

console.log('Hello world !') ;

}

}

sayHello() ; // Hello world !

Page 33: Poo et JavaScript, une notion trop incomprise
Page 34: Poo et JavaScript, une notion trop incomprise

Tout objet a un prototype

Page 35: Poo et JavaScript, une notion trop incomprise

monObjet.prototype.name = 'John Doe' ;

monObjet.prototype.getName = function () {

return this.name ;

}

Page 36: Poo et JavaScript, une notion trop incomprise

Object

Objet A Objet CObjet B

Objet A1 Objet A2 Objet A3

Page 37: Poo et JavaScript, une notion trop incomprise

Object

Objet A Objet CObjet B

Objet A1 Objet A2 Objet A3

Page 38: Poo et JavaScript, une notion trop incomprise

Object

Objet A Objet CObjet B

Objet A1 Objet A2 Objet A3

Page 39: Poo et JavaScript, une notion trop incomprise

Object

Objet A Objet CObjet B

Objet A1 Objet A2 Objet A3

Page 40: Poo et JavaScript, une notion trop incomprise

static

héritage

privé

Page 41: Poo et JavaScript, une notion trop incomprise

var monObjet = {

what : 'ConFoo',

when : '2012',

where : 'Montréal',

sayHello : function () {

console.log('Hello ConFoo 2012 !') ;

}

};

Page 42: Poo et JavaScript, une notion trop incomprise

var monObjetBis = monObjet ;

Page 43: Poo et JavaScript, une notion trop incomprise

FAUX

Page 44: Poo et JavaScript, une notion trop incomprise

Vous avez copié une référence

Page 45: Poo et JavaScript, une notion trop incomprise

var ObjetPere = function () {console.log('hello');

} ;

ObjetPere.prototype.name = 'Dark Vador' ;

var objetReel = new ObjetPere();

Page 46: Poo et JavaScript, une notion trop incomprise

var objet = {name:'Dark Vador'

} ;var fils = Object.create(objet) ;

Page 47: Poo et JavaScript, une notion trop incomprise

Privé

Page 48: Poo et JavaScript, une notion trop incomprise

function Container () { var secret = 3; function dec () { return secret; } this.getSecret = function () { return dec(); }}var test = new Container() ;console.log( test.secret ) ; //undefinedconsole.log( test.getSecret() ) ; //3

Page 49: Poo et JavaScript, une notion trop incomprise

Crédits PhotosDarin McClure : http://www.flickr.com/photos/darinrmcclure

Geekphysical : http://www.flickr.com/photos/geekphysical

Marc Wathieu : http://www.flickr.com/photos/marcwathieu

Theresa Thompson : http://www.flickr.com/photos/theresasthompson

Esparta Palma : http://www.flickr.com/photos/esparta

Maria Reyes-McDavis : http://www.flickr.com/photos/mariareyesmcdavis

Denis Vahrushev : http://www.flickr.com/photos/dvahrushev

Shadowgate : http://www.flickr.com/photos/shadowgate

Eschipul : http://www.flickr.com/photos/eschipul

Eleaf : http://www.flickr.com/photos/eleaf

Page 50: Poo et JavaScript, une notion trop incomprise

https://joind.in/6084

http://www.slideshare.net/mathrobin/poo-et-java-script-notion-trop-incomprise

Page 51: Poo et JavaScript, une notion trop incomprise

Top Related