vitali-hornik-.pptx
- Количество слайдов: 22
Do you know everything about Java. Script OOP? Vitali Hornik, PMP® Ph. D. in IT (Computer Science)
Procedural Programming vs OOP The main code of procedural programming is constantly processing various situations. The main code of OOP tries to pass responsibility to the executor - system objects.
Your efforts and worries at the project 30 25 20 15 Pct. 10 OOP 5 Month 23 Month 21 Month 19 Month 17 Month 15 Month 13 Month 11 Month 9 Month 7 Month 5 Month 3 Month 1 0 Job satisfaction is inversely proportional to them
…and a new project task will make you totally happy! Like this…
3 major principles of OOP 1. Encapsulation by means of closures 2. Inheritance via prototyping 3. Polymorphism - JS is not a typed language
JS doesn’t have 1. interfaces and abstract classes* 2. final classes 3. protected modifiers 4. static class members
this var obj = { outer. Width : 20 }; function get. Width() { return this. outer. Width; } var a = get. Width (); var b = get. Width. apply(obj); this points to the object in the context of which the code works
Can this be changed? 1. var obj = new Some. Function(…); // the created object 2. obj. public. Function(…. ); // obj 3. some. Function. apply(obj, [arg 1, arg 2, …. ]); // obj 4. some. Function. call(obj, arg 1, arg 2, …. ); // obj
Go ahead!!!
prototype and/or __proto__ function A() {…. } A. prototype – an object with one property «constructor» . A. prototype. constructor – function А A. __proto__ – Function. prototype, the child of Object is the parent of everything. alert(A. __proto__ === A. prototype. constructor. __proto__ ); // true
Object prototype __proto__ Function prototype __proto__ A prototype – specifies the properties of created objects __proto__ – everything has __proto__ , it serves for parent/child connection
new function A(args) {…. } var obj = new A(args); 1. 2. 3. 4. var obj = {}; obj. __proto__ = A. prototype; var new. Constructor = A. apply(obj, [args]); obj = new. Constructor instanceof Object ? new. Constructor : obj; alert(obj. prototype); // undefined alert(obj. __proto__ === A. prototype); // true alert(obj. __proto__ === Object. prototype); // true
A few more words about prototype and __proto__ var obj = new A(); A. __proto__. p 1 = 1; //a property added into constructor alert (obj. p 1); // “undefined” alert (obj. constructor. p 1); // “ 1” obj. __proto__. p 2 = 2; alert(obj. p 2); // “ 2” A. prototype. p 3 = 3; alert(obj. p 3); // “ 3”
…dive cheerfully into the code …
Simple object, singleton var obj = { v : "prop" , AA 1 : function(t) { alert(this. v + t); } }; // "obj" – is "new Object()" that has the keys "v" and "AA 1" obj. AA 2 = function(…){…. . }; // AA 2 key is added to A obj. AA 1(1); obj. AA 2();
Simple class function A() { this. v = 'prop'; return this; } A. prototype. AA 1 = function(){…}; var obj = new A(); // “v” – property of the object // АА 1 – property of the object prototype A. prototype. AA 2 = function(t) { alert(this. v+t); }; obj. AA 2(2); // that’s why a 1. __proto__ === A. prototype works var obj 2 = A(); // obj 2 is a window obj 2. AA 1(1); // which doesn’t have AA 1
Kill the constructor function A() { return this; } A. prototype = { v : 'prop + ' , AA 1 : function(){…. } }; // "A. prototype" turned into "new Object()" // А. prototype. constructor is not A var obj = new A(); A. prototype. AA 2 = function(){…. }; obj. AA 1();
Private members function A() { var p 1 = 1; // visible inside of “A” function private. Function() { p 1=2; } this. v = 'prop'; this. public. Function = function() { private. Function(); alert(this. v+p 1); } } A. prototype. AA 1 = function(t){alert(this. v+t)}; var obj = new A(); obj. v = 'new value '; obj. AA 1(3); obj. public. Function();
Сomplex class var A = function() { var p 1 = 1; function private. Function() { p 1 = 2; } function B() { return 1; } B. prototype. v = 'prop '; B. prototype. public. Function = function() { private. Function(); } return B; } var obj = new ( A() )();
Complex singleton var A = (function() { var p 1 = 1; })(); function B() {} B. prototype. set. P 1 = function(t){p 1 = t; } B. prototype. public. Function = function() { alert(p 1); } return B; var obj 1 = new A(); obj 1. set. P 1(3); var obj 2 = new A(); obj 2. public. Function(); // alert "3"
Inheritance function extend(Child, Parent) {…. } // thx Crockford function A() { …. . } A. prototype. v = 'prop '; A. prototype. AA 1 = function(t){ alert(this. v+t); }; function B() { this. z = 2; B. superclass. constructor. apply(this, arguments); } extend(B, A); var obj = new B(); obj. AA 1();
Contact info: • Technical and project manager • Vitali Hornik, PMP®, Ph. D. in IT (Computer Science) • vhornik@gmail. com
vitali-hornik-.pptx