1 of 43

Prototype - 11

2 of 43

PLAN

3 of 43

Objectif

4 of 43

Prototype est un mot clé du langage JS.

Nous allons découvrir son origine, au travers de la création de nouveaux objets.

5 of 43

Mais, commençons par découvrir le "pattern Functional Classes"

ll permet de créer de nouveaux objets.

6 of 43

Pattern : Functional Classes

let Pattern = function( arg ) {�

...

�};

Une fonction

7 of 43

Pattern : Functional Classes

let Pattern = function( arg ) {� let obj = {� ...� };� ...

};

Création d'un objet

8 of 43

Pattern : Functional Classes

let Pattern = function( arg ) {� let obj = {� ...� };� return obj;�};

Renvoie l'objet !

9 of 43

Functional Classes

let Point = function( arg ) {� let obj = {� ...,� };� return obj;�};

Une fonction qui crée et renvoi un objet

10 of 43

Représentation en mémoire

Il est important de prendre en compte la représentation en mémoire.

http://www.pythontutor.com/ est un formidable outil de visualisation.

11 of 43

Exemple : Functional Classes

let Point = function( x ) {� let obj = {� x,� move,� };� return obj;�};

function move () { � this.x++; �}

12 of 43

Création d'objets

let Point = function( x ) {� let obj = {� x,� move,� };� return obj;�};

function move () { � this.x++; �}

let pt1 = Point(1);�let pt2 = Point(2);

13 of 43

Discussion sur la mémoire

Functionnal Pattern est très simple, pas de classe ...

Mais, nous allons examiner l'efficacité de ce pattern en terme de "mémoire".

Pour cela nous allons ajouter des méthodes au pattern.

14 of 43

Ajout de méthodes

let Point = function( x ) {� let obj = {� x,� move,� reset,� back,� };� return obj;�};

function move () { � this.x++; �}�function reset () { � this.x = 0 ; �}�function back () { � this.x-- ; �}

15 of 43

16 of 43

Nous allons tenter d'améliorer le code !

17 of 43

Rappels

Il vous faut maîtriser la délégation :

la chaîne de proto.

Object.create

18 of 43

Réécriture

let Point = function( x ) {� let obj = {� x,� move,� };� return obj;�};

function move () { � this.x++; �}

��

19 of 43

Les fonctions sont des objets

et des variables !

20 of 43

Une fonction est un objet

Point.methods = { �

�};

Point = function{ �

�};

Une fonction est un objet, on peut lui donner une propriété "methods" qui est un objet regroupant des méthodes !

21 of 43

Réécriture

let Point = function( x ) {� let obj = {� x,� move,� };� return obj;�};

Point.methods = { � move : function() {� this.x++;� }�};���

function move () {� this.x++; �}

22 of 43

La délégation

let obj = Object.create( proto );

On utilise object.create du langage pour la création d'un objet et la délégation.

23 of 43

Création

let Point = function( x ) {� let obj = {� x,� move, };� return obj;�};

function move () { � this.x++; �}

let Point = function ( loc ) { � let obj = Object.create( Point.methods ); � obj.x = loc;� return obj;�};�

Point.methods = { � move : function() {� this.x++;� }�};��

24 of 43

Délégation

let Point = function( x ) {� let obj = {� x,� move, };� return obj;�};

function move () { � this.x++; �}

let Point = function ( loc ) { � let obj = Object.create(Point.methods); � obj.x = loc;� return obj;�};�

��

Point.methods = { � move : function() {� this.x++;� }�};

25 of 43

Bilan :

let Point = function ( loc ) { � let obj = Object.create( Point.methods );

obj.x = loc;� return obj;�};��Point.methods = { � move : function() {� this.x++;� }�};���

26 of 43

Bilan :

let pt1 = Point(1);�let pt2 = Point(2);

pas de new avec ce pattern

27 of 43

Renommons

Point.methods par

Point.prototype

28 of 43

prototype = methods

let Point = function ( loc ) { � let obj = Object.create(Point.prototype); � obj.x = loc;� return obj;�};��Point.prototype = { � move : function() {� this.x++;� }�};��

let Point = function ( loc ) { � let obj = Object.create(Point.methods); � obj.x = loc;� return obj;�};

Point.methods = { � move : function() {� this.x++;� }�};

29 of 43

Introduction de prototype

30 of 43

Cas de plusieurs objets créés

let pt1 = Point(1);�let pt2 = Point(2);

31 of 43

Bilan

let pt1 = Point(1);�let pt2 = Point(2);

Un nom qui a du sens !

32 of 43

Ajout de plusieurs fonctions

Les fonctions sont à ajouter au prototype.

33 of 43

let Point = function ( loc ) { � let obj = Object.create(Point.prototype); � obj.x = loc;� return obj;�};

Point.prototype = { � move () {� this.x++;� },� reset () { � this.x = 0 ; � },� back () { � this.x-- ; � }

�};

34 of 43

let pt1 = Point(1);

let pt2 = Point(2);

L'ensemble des méthodes communes

35 of 43

Comparaison

let Point = function( x ) {� let obj = {� x,� move,� reset,� back,� };� return obj;�};�function move () { � this.x++; �}�function reset () { � this.x = 0 ; �}�function back () { � this.x-- ; �}

let Point = function ( loc ) { � let obj = Object.create(Point.prototype); � obj.x = loc;� return obj;�};��Point.prototype = { � move () {� this.x++;� },� reset () { � this.x = 0 ; � },� back () { � this.x-- ; � }�};

36 of 43

Comparaison

37 of 43

Prototype

Regroupe les propriétés communes aux objets

38 of 43

En détail

be cool !

39 of 43

Point : Fonction

[[prototype]]

staticMethod

function()[}

prototype

pt : Point

[[prototype]]

x

100

Point.prototype : Object

[[prototype]]

move

function()[}

constructor

40 of 43

Idée à retenir !

Les objets pointent tous sur le prototype grâce à __proto__

Je ne parle pas d'instance.

41 of 43

Point : Fonction

[[prototype]]

staticMethod

function()[}

prototype

pt : Point

[[prototype]]

x

Point.prototype : Object

[[prototype]]

prototypeMethod

function()[}

constructor

42 of 43

Point : Fonction

[[prototype]]

staticMethod

function()[}

prototype

pt : Point

[[prototype]]

x

Point.prototype : Object

[[prototype]]

prototypeMethod

function()[}

constructor

pt : Point

[[prototype]]

x

pt : Point

[[prototype]]

x

pt : Point

[[prototype]]

x

pt : Point

[[prototype]]

x

43 of 43

Et new dans tout cela ?