1212<!-- section start -->
1313<!-- attr: { showInPresentation:true, hasScriptWrapper:true } -->
1414# Table of Contents
15- - Class methods
16- - Properties
15+ - [ Class methods] ( #class-methods )
16+ - [ Properties] ( #class-properties )
1717 - Getters and Setters
18- - JavaScript Prototypes
18+ - [ Static methods and properties] ( #static )
19+ - [ JavaScript Prototypes] ( #prototypes )
1920
2021<!-- section start -->
21- <!-- attr: { showInPresentation: true, class: 'slide-section' } -->
22+ <!-- attr: { id: 'class-methods', showInPresentation: true, class: 'slide-section' } -->
2223# Class Methods
2324## Attach functionality to class instances
2425
@@ -45,7 +46,7 @@ mariya.meow(); // mariya: meow!
4546<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style: 'font-size: 0.9em' } -->
4647# Class Methods
4748- When a method is called, the following happens
48- - The context of the function is set to the object which it's called from
49+ - The context of the function is set to the class instance which it's called from
4950 - ` this ` is a reference to the object the method is called from
5051
5152``` js
@@ -63,7 +64,7 @@ fluff.printSelf(); // { _name: 'Fluff' }
6364```
6465
6566<!-- section start -->
66- <!-- attr: { showInPresentation: true, class: 'slide-section' } -->
67+ <!-- attr: { id: 'class-properties', showInPresentation: true, class: 'slide-section' } -->
6768# Getters and setters
6869## Control access to fields
6970
@@ -104,8 +105,101 @@ peter.lastname = 'petrov';
104105console .log (peter .fullname );
105106```
106107
108+ <!-- attr: { hasScriptWrapper: true, style: 'font-size: 0.8em' } -->
109+ # Throwing in some validation - _ Example_
110+ - The names of a ` Person ` should always be strings:
111+
112+ ``` js
113+ class Person {
114+ // other code here
115+ get fullname () {
116+ return this ._firstname + ' ' + this ._lastname ;
117+ }
118+
119+ set firstname (value ) {
120+ if (typeof value !== ' string' )
121+ throw new Error (' Name must be string!' );
122+
123+ this ._firstname = value;
124+ }
125+
126+ set lastname (value ) {
127+ if (typeof value !== ' string' )
128+ throw new Error (' Name must be string!' );
129+
130+ this ._lastname = value;
131+ }
132+ }
133+ ```
134+
135+ <!-- attr: { hasScriptWrapper: true, style: 'font-size: 0.9em' } -->
136+ # Throwing in some validation - _ Example_
137+ - When trying to assign a value of type other than string:
138+
139+ ``` js
140+ const ivan = new Person (' ivan' , ' ivanov' );
141+ ivan .lastname = {}; // Error!
142+ ```
143+
144+ - Assigning a string value works as expected:
145+
146+ ``` js
147+ const maria = new Person (' maria' , ' petrova' );
148+ maria .lastname = ' ivanova' ;
149+ console .log (maria);
150+ // will log { _firstname: 'maria', _lastname: 'ivanova' }
151+ ```
152+
107153<!-- section start -->
108- <!-- attr: { showInPresentation: true, class: 'slide-section' } -->
154+ <!-- attr: { id: 'static', class: 'slide-section', showInPresentation: true } -->
155+ # Static methods and properties
156+ ## Non-instance methods and properties
157+
158+ <!-- attr: { hasScriptWrapper: true, style: 'font-size: 0.9em' } -->
159+ # Static methods and properties
160+ - ` Static methods and properties ` are class methods or properties that are ** not associated with any particular instance of the class**
161+ - ` Static methods and properties ` are defined with the keyword ` static `
162+ - ` Static methods and properties ` do not have access to a class instance via the ` this ` keyword
163+ - ` Static methods and properties ` are called from the class function
164+
165+ <!-- attr: { hasScriptWrapper: true, style: 'font-size: 0.8em' } -->
166+ # Defining static methods and properties
167+ - Consider the following class that represents points in 2D:
168+
169+ ``` js
170+ class Point2D {
171+ constructor (x , y ) {
172+ this ._x = x;
173+ this ._y = y;
174+ }
175+
176+ static get ZERO () {
177+ return new Point2D (0 , 0 );
178+ }
179+
180+ static getDistance (pointA , pointB ) {
181+ const sqrDistance =
182+ (pointA ._x - pointB ._x ) * (pointA . _x - pointB ._x ) +
183+ (pointA ._y - pointB ._y ) * (pointA . _y - pointB ._y );
184+ return Math .sqrt (sqrDistance);
185+ }
186+ }
187+ ```
188+
189+ # Using static methods and properties
190+ - After defining the static properties and methods, we can use them via the ` class function ` :
191+
192+ ``` js
193+ console .log (Point2D .ZERO ); // { _x: 0, _y: 0 }
194+ const pointA = new Point2D (3 , 4 ),
195+ pointB = new Point2D (3 , 5 ),
196+ distanceAB = Point2D .getDistance (pointA, pointB);
197+
198+ console .log (distanceAB); // 1
199+ ```
200+
201+ <!-- section start -->
202+ <!-- attr: { id: 'prototypes', showInPresentation: true, class: 'slide-section' } -->
109203# JavaScript Prototypes
110204## The true nature of JavaScript
111205
@@ -141,6 +235,12 @@ SelfPrintingCat.prototype.meow = function () {
141235};
142236```
143237
238+ - Now we can use ` meow ` as an instance method:
239+
240+ ``` js
241+ new SelfPrintingCat (' Tom' ).meow ();
242+ ```
243+
144244
145245<!-- Questions -->
146246<!-- section start -->
0 commit comments