|
1 | 1 | <!-- section start --> |
2 | | -<!-- attr: { id:'', class:'slide-title', showInPresentation:true, hasScriptWrapper:true } --> |
3 | | -# Class Methods and Properties |
4 | | -## |
| 2 | +<!-- attr: { class:'slide-title', hasScriptWrapper:true } --> |
| 3 | +# Class methods and Properties |
| 4 | + |
5 | 5 | <article class="signature"> |
6 | 6 | <p class="signature-course">JavaScript OOP</p> |
7 | 7 | <p class="signature-initiative">Telerik Software Academy</p> |
8 | | - <a href="http://academy.telerik.com " class="signature-link">http://academy.telerik.com </a> |
9 | | -</div> |
| 8 | + <a href="http://academy.telerik.com" class="signature-link">http://academy.telerik.com</a> |
| 9 | +</article> |
| 10 | + |
| 11 | + |
| 12 | +<!-- section start --> |
| 13 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true } --> |
| 14 | +# Table of Contents |
| 15 | +- Class methods |
| 16 | +- Properties |
| 17 | + - Getters and Setters |
| 18 | +- JavaScript Prototypes |
| 19 | + |
| 20 | +<!-- section start --> |
| 21 | +<!-- attr: { showInPresentation: true, class: 'slide-section' } --> |
| 22 | +# Class Methods |
| 23 | +## Attach functionality to class instances |
| 24 | + |
| 25 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true } --> |
| 26 | +# Class Methods |
| 27 | +- Methods are functions in the context of a class |
| 28 | +- Classes can define methods with the same syntax as a constructor: |
| 29 | + |
| 30 | +```js |
| 31 | +class Cat { |
| 32 | + constructor(name) { |
| 33 | + this._name = name; |
| 34 | + } |
| 35 | + |
| 36 | + meow() { |
| 37 | + console.log(this.name + ': meow!'); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const mariya = new Cat('Mariya'); |
| 42 | +mariya.meow(); // mariya: meow! |
| 43 | +``` |
| 44 | + |
| 45 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style: 'font-size: 0.9em' } --> |
| 46 | +# Class Methods |
| 47 | +- 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 | + - `this` is a reference to the object the method is called from |
| 50 | + |
| 51 | +```js |
| 52 | +class SelfPrintingCat { |
| 53 | + constructor(name) { |
| 54 | + this._name = name; |
| 55 | + } |
| 56 | + |
| 57 | + printSelf() { |
| 58 | + console.log(this); |
| 59 | + } |
| 60 | +} |
| 61 | +const fluff = new SelfPrintingCat('Fluff'); |
| 62 | +fluff.printSelf(); // { _name: 'Fluff' } |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- section start --> |
| 66 | +<!-- attr: { showInPresentation: true, class: 'slide-section' } --> |
| 67 | +# Getters and setters |
| 68 | +## Control access to fields |
10 | 69 |
|
| 70 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style:'font-size:0.9em' } --> |
| 71 | +# Getters and setters |
| 72 | +- Classes also provide **getter** and **setter** syntax |
| 73 | + - Getters and setters are used like data fields, but actually behave like methods |
| 74 | + - Can be used to encapsulate data and control access to it |
| 75 | + - Getters to get values of fields |
| 76 | + - Setter to change values of fields |
| 77 | + |
| 78 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style:'font-size:0.8em' } --> |
| 79 | +# Getters and setters |
| 80 | + |
| 81 | +```js |
| 82 | +class Person { |
| 83 | + constructor(firstname, lastname) { |
| 84 | + this._firstname = firstname; |
| 85 | + this._lastname = lastname; |
| 86 | + } |
| 87 | + |
| 88 | + get fullname() { |
| 89 | + return this._firstname + ' ' + this._lastname; |
| 90 | + } |
| 91 | + |
| 92 | + set firstname(value) { |
| 93 | + this._firstname = value; |
| 94 | + } |
| 95 | + |
| 96 | + set lastname(value) { |
| 97 | + this._lastname = value; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const peter = new Person('peter', 'anderson'); |
| 102 | +console.log(peter.fullname); |
| 103 | +peter.lastname = 'petrov'; |
| 104 | +console.log(peter.fullname); |
| 105 | +``` |
| 106 | + |
| 107 | +<!-- section start --> |
| 108 | +<!-- attr: { showInPresentation: true, class: 'slide-section' } --> |
| 109 | +# JavaScript Prototypes |
| 110 | +## The true nature of JavaScript |
| 111 | + |
| 112 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style:'font-size:0.9em' } --> |
| 113 | +# Prototypes |
| 114 | +- Class methods, getters and setters aren't directly attached to the objects |
| 115 | + - They're attached to their `prototypes` |
| 116 | + - In JavaScript, objects inherit from other objects |
| 117 | + - Consider the `SelfPrintingCat` example: |
| 118 | + |
| 119 | +```js |
| 120 | +new SelfPrintingCat('Fluff').printSelf(); // { _name: 'Fluff' } |
| 121 | +``` |
| 122 | +- The `printSelf` method is not attached to `fluff` |
| 123 | + |
| 124 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true } --> |
| 125 | +# Prototypes |
| 126 | +- To access the prototype of `fluff`, when can write the following: |
| 127 | + |
| 128 | +```js |
| 129 | +const catProto = SelfPrintingCat.prototype; |
| 130 | +console.log(typeof catProto.printSelf); // function |
| 131 | +``` |
| 132 | + |
| 133 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style:'font-size:0.9em' } --> |
| 134 | +# Prototypes |
| 135 | +- JavaScript prototypes allow us to extend a class dynamically |
| 136 | + - We can make the `SelfPrintingCat` meow |
| 137 | + |
| 138 | +```js |
| 139 | +SelfPrintingCat.prototype.meow = function () { |
| 140 | + console.log(this._name + ': meow'!); |
| 141 | +}; |
| 142 | +``` |
11 | 143 |
|
12 | 144 |
|
13 | 145 | <!-- Questions --> |
14 | 146 | <!-- section start --> |
15 | | -<!-- attr: { hasScriptWrapper:true, class:"slide-questions", id:"questions" } --> |
16 | | -<!-- # Class Methods and Properties |
| 147 | +<!-- attr: { hasScriptWrapper:true, showInPresentation:true, class:"slide-questions", id:"questions" } --> |
| 148 | +<!-- # ES6 Classes and Class Constructors |
17 | 149 | ## Questions? --> |
18 | 150 |
|
19 | | - |
20 | | -<!-- attr: { showInPresentation: true, hasScriptWrapper: true, style:'font-size: 0.9em' } --> |
21 | | -# Free Trainings<br/>@ Telerik Academy |
| 151 | +<!-- attr: { showInPresentation:true, hasScriptWrapper:true, style:'font-size: 0.9em' } --> |
| 152 | +# Free Trainings @ Telerik Academy |
22 | 153 | - "Web Design with HTML 5, CSS 3 and JavaScript" course @ Telerik Academy |
23 | 154 | - [javascript course](http://academy.telerik.com/student-courses/web-design-and-ui/javascript-fundamentals/about) |
24 | 155 | - Telerik Software Academy |
25 | | - - [academy.telerik.com](academy.telerik.com) |
| 156 | + - [academy.telerik.com](http://academy.telerik.com) |
26 | 157 | - Telerik Academy @ Facebook |
27 | | - - [facebook.com/TelerikAcademy](facebook.com/TelerikAcademy) |
| 158 | + - [facebook.com/TelerikAcademy](https://facebook.com/TelerikAcademy) |
28 | 159 | - Telerik Software Academy Forums |
29 | | - - [forums.academy.telerik.com](http://telerikacademy.com/Forum/Home) |
| 160 | + - [forums.academy.telerik.com](https://telerikacademy.com/Forum/Home) |
30 | 161 |
|
31 | | -<!-- <img class="slide-image" showInPresentation="true" src="imgs/pic00.png" style="top:58.18%; left:90.52%; width:16.97%; z-index:-1" /> --> |
| 162 | +<!-- <img class="slide-image" showInPresentation="false" src="imgs/pic00.png" style="top:58.18%; left:90.52%; width:16.97%; z-index:-1" /> --> |
0 commit comments