SlideShare une entreprise Scribd logo
1  sur  22
Vitali Hornik, PMP®
Ph.D. in IT (Computer Science)
Do you know everything about
JavaScript 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.
Procedural Programming vs OOP
0
5
10
15
20
25
30
Month1
Month3
Month5
Month7
Month9
Month11
Month13
Month15
Month17
Month19
Month21
Month23
Month25
Pct.
OOP
Your efforts and worries at the project
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 
1. interfaces and abstract classes*
2. final classes
3. protected modifiers
4. static class members
JS doesn’t have
this
var obj = { outerWidth : 20 };
function getWidth() { return this.outerWidth; }
var a = getWidth ();
var b = getWidth.apply(obj);
this points to the object in the context of which the code works
Can this be changed?
1. var obj = new SomeFunction(…); // the created object
2. obj.publicFunction(….); // obj
3. someFunction.apply(obj,[arg1,arg2,….]); // obj
4. someFunction.call(obj, arg1,arg2,….); // obj
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.
Object is the parent of everything.
alert(A.__proto__ === A.prototype.constructor.__proto__ ); // true
A
Function
Object
prototype
__proto__
prototype
__proto__
prototype
prototype – specifies the properties of created objects
__proto__ – everything has __proto__ , it serves for parent/child
connection
function A(args) {….}
var obj = new A(args);
1. var obj = {};
2. obj.__proto__ = A.prototype;
3. var newConstructor = A.apply(obj, [args]);
4. obj = newConstructor instanceof Object ? newConstructor : obj;
alert(obj.prototype); // undefined
alert(obj.__proto__ === A.prototype); // true
alert(obj.__proto__.__proto__ === Object.prototype); // true
new
var obj = new A();
A.__proto__.p1 = 1; //a property added into constructor
alert (obj.p1); // “undefined”
alert (obj.constructor. p1); // “1”
obj.__proto__.p2 = 2;
alert(obj.p2); // “2”
A.prototype.p3 = 3;
alert(obj.p3); // “3”
A few more words about prototype and __proto__
…dive cheerfully into the code …
Simple object, singleton
var obj =
{
v : "prop"
, AA1 : function(t)
{
alert(this.v + t);
}
}; // "obj" – is "new Object()" that has the keys "v" and "AA1"
obj.AA2 = function(…){…..}; // AA2 key is added to A
obj.AA1(1);
obj.AA2();
Simple class
function A() { this.v = 'prop'; return this; }
A.prototype.AA1 = function(){…};
var obj = new A();
// “v” – property of the obj object
// АА1 – property of the object prototype
A.prototype.AA2 = function(t) { alert(this.v+t); };
obj.AA2(2); // that’s why a1.__proto__ === A.prototype
works
var obj2 = A(); // obj2 is a window
obj2.AA1(1); // which doesn’t have AA1
function A() { return this; }
A.prototype = {
v : 'prop + '
, AA1 : function(){….}
};
// "A.prototype" turned into "new Object()"
// А.prototype.constructor is not A
var obj = new A();
A.prototype.AA2 = function(){….};
obj.AA1();
Kill the constructor
Private members
function A()
{
var p1 = 1; // visible inside of “A”
function privateFunction() { p1=2; }
this.v = 'prop';
this.publicFunction = function()
{
privateFunction();
alert(this.v+p1);
}
}
A.prototype.AA1 = function(t){alert(this.v+t)};
var obj = new A();
obj.v = 'new value ';
obj.AA1(3);
obj.publicFunction();
var A = function()
{
var p1 = 1;
function privateFunction() { p1 = 2; }
function B() { return 1; }
B.prototype.v = 'prop ';
B.prototype.publicFunction = function()
{
privateFunction();
}
return B;
}
var obj = new ( A() )();
Сomplex class
var A = (function()
{
var p1 = 1;
function B() {}
B.prototype.setP1 = function(t){p1 = t; }
B.prototype.publicFunction = function() { alert(p1); }
return B;
})();
var obj1 = new A();
obj1.setP1(3);
var obj2 = new A();
obj2.publicFunction(); // alert "3"
Complex singleton
function extend(Child, Parent) {….} // thx Crockford
function A() { ….. }
A.prototype.v = 'prop ';
A.prototype.AA1 = 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.AA1();
Inheritance
Contact info:
• Technical and project manager
• Vitali Hornik, PMP®, Ph.D. in IT (Computer Science)
• vhornik@gmail.com

Contenu connexe

Tendances

11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 
What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 
storage class
storage classstorage class
storage class
student
 

Tendances (20)

virtual function
virtual functionvirtual function
virtual function
 
Check the output of the following code then recode it to eliminate fu
 Check the output of the following code then recode it to eliminate fu Check the output of the following code then recode it to eliminate fu
Check the output of the following code then recode it to eliminate fu
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes
Storage classesStorage classes
Storage classes
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
oop Lecture 6
oop Lecture 6oop Lecture 6
oop Lecture 6
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.net
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Demoiselle 2.0 no JavaOne Brasil 2010
Demoiselle 2.0 no JavaOne Brasil 2010Demoiselle 2.0 no JavaOne Brasil 2010
Demoiselle 2.0 no JavaOne Brasil 2010
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Storage classes
Storage classesStorage classes
Storage classes
 
Function C++
Function C++ Function C++
Function C++
 
Exception
ExceptionException
Exception
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
storage class
storage classstorage class
storage class
 
Storage class
Storage classStorage class
Storage class
 

En vedette

PayPal интеграция. Запрещенная лекция 18+
PayPal интеграция. Запрещенная лекция 18+PayPal интеграция. Запрещенная лекция 18+
PayPal интеграция. Запрещенная лекция 18+
XB Software, Ltd.
 

En vedette (10)

Files and JavaScript
Files and JavaScriptFiles and JavaScript
Files and JavaScript
 
PayPal интеграция. Запрещенная лекция 18+
PayPal интеграция. Запрещенная лекция 18+PayPal интеграция. Запрещенная лекция 18+
PayPal интеграция. Запрещенная лекция 18+
 
How to become Famo.us
How to become Famo.usHow to become Famo.us
How to become Famo.us
 
Cобытия в JavaScript
Cобытия в JavaScriptCобытия в JavaScript
Cобытия в JavaScript
 
Game physics in JavaScript
Game physics in JavaScriptGame physics in JavaScript
Game physics in JavaScript
 
Постоянная сборка фронтенда – автоматизация конвейера
Постоянная сборка фронтенда – автоматизация конвейераПостоянная сборка фронтенда – автоматизация конвейера
Постоянная сборка фронтенда – автоматизация конвейера
 
Production Ready Javascript With Grunt
Production Ready Javascript With GruntProduction Ready Javascript With Grunt
Production Ready Javascript With Grunt
 
Files and JS
Files and JSFiles and JS
Files and JS
 
Всё ли ты знаешь о JavaScript ООП?
Всё ли ты знаешь о JavaScript ООП?Всё ли ты знаешь о JavaScript ООП?
Всё ли ты знаешь о JavaScript ООП?
 
Flexbox - верстка без float'ов by Dmitry Radyno
Flexbox - верстка без float'ов by Dmitry RadynoFlexbox - верстка без float'ов by Dmitry Radyno
Flexbox - верстка без float'ов by Dmitry Radyno
 

Similaire à 4front en

JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
Java script object model
Java script object modelJava script object model
Java script object model
James Hsieh
 

Similaire à 4front en (20)

Javascript
JavascriptJavascript
Javascript
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
[A 3]Javascript oop for xpages developers - public
[A 3]Javascript oop for xpages developers - public[A 3]Javascript oop for xpages developers - public
[A 3]Javascript oop for xpages developers - public
 
Let's JavaScript
Let's JavaScriptLet's JavaScript
Let's JavaScript
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
Java script object model
Java script object modelJava script object model
Java script object model
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Javascript closures
Javascript closures Javascript closures
Javascript closures
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
JavaScript Essentials
JavaScript EssentialsJavaScript Essentials
JavaScript Essentials
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 

Dernier

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

4front en

  • 1. Vitali Hornik, PMP® Ph.D. in IT (Computer Science) Do you know everything about JavaScript OOP?
  • 2. 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. Procedural Programming vs OOP
  • 4. …and a new project task will make you totally happy! Like this…
  • 5. 3 major principles of OOP 1. Encapsulation by means of closures 2. Inheritance via prototyping 3. Polymorphism - JS is not a typed language 
  • 6. 1. interfaces and abstract classes* 2. final classes 3. protected modifiers 4. static class members JS doesn’t have
  • 7. this var obj = { outerWidth : 20 }; function getWidth() { return this.outerWidth; } var a = getWidth (); var b = getWidth.apply(obj); this points to the object in the context of which the code works
  • 8. Can this be changed? 1. var obj = new SomeFunction(…); // the created object 2. obj.publicFunction(….); // obj 3. someFunction.apply(obj,[arg1,arg2,….]); // obj 4. someFunction.call(obj, arg1,arg2,….); // obj
  • 9.
  • 10. 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. Object is the parent of everything. alert(A.__proto__ === A.prototype.constructor.__proto__ ); // true
  • 11. A Function Object prototype __proto__ prototype __proto__ prototype prototype – specifies the properties of created objects __proto__ – everything has __proto__ , it serves for parent/child connection
  • 12. function A(args) {….} var obj = new A(args); 1. var obj = {}; 2. obj.__proto__ = A.prototype; 3. var newConstructor = A.apply(obj, [args]); 4. obj = newConstructor instanceof Object ? newConstructor : obj; alert(obj.prototype); // undefined alert(obj.__proto__ === A.prototype); // true alert(obj.__proto__.__proto__ === Object.prototype); // true new
  • 13. var obj = new A(); A.__proto__.p1 = 1; //a property added into constructor alert (obj.p1); // “undefined” alert (obj.constructor. p1); // “1” obj.__proto__.p2 = 2; alert(obj.p2); // “2” A.prototype.p3 = 3; alert(obj.p3); // “3” A few more words about prototype and __proto__
  • 14. …dive cheerfully into the code …
  • 15. Simple object, singleton var obj = { v : "prop" , AA1 : function(t) { alert(this.v + t); } }; // "obj" – is "new Object()" that has the keys "v" and "AA1" obj.AA2 = function(…){…..}; // AA2 key is added to A obj.AA1(1); obj.AA2();
  • 16. Simple class function A() { this.v = 'prop'; return this; } A.prototype.AA1 = function(){…}; var obj = new A(); // “v” – property of the obj object // АА1 – property of the object prototype A.prototype.AA2 = function(t) { alert(this.v+t); }; obj.AA2(2); // that’s why a1.__proto__ === A.prototype works var obj2 = A(); // obj2 is a window obj2.AA1(1); // which doesn’t have AA1
  • 17. function A() { return this; } A.prototype = { v : 'prop + ' , AA1 : function(){….} }; // "A.prototype" turned into "new Object()" // А.prototype.constructor is not A var obj = new A(); A.prototype.AA2 = function(){….}; obj.AA1(); Kill the constructor
  • 18. Private members function A() { var p1 = 1; // visible inside of “A” function privateFunction() { p1=2; } this.v = 'prop'; this.publicFunction = function() { privateFunction(); alert(this.v+p1); } } A.prototype.AA1 = function(t){alert(this.v+t)}; var obj = new A(); obj.v = 'new value '; obj.AA1(3); obj.publicFunction();
  • 19. var A = function() { var p1 = 1; function privateFunction() { p1 = 2; } function B() { return 1; } B.prototype.v = 'prop '; B.prototype.publicFunction = function() { privateFunction(); } return B; } var obj = new ( A() )(); Сomplex class
  • 20. var A = (function() { var p1 = 1; function B() {} B.prototype.setP1 = function(t){p1 = t; } B.prototype.publicFunction = function() { alert(p1); } return B; })(); var obj1 = new A(); obj1.setP1(3); var obj2 = new A(); obj2.publicFunction(); // alert "3" Complex singleton
  • 21. function extend(Child, Parent) {….} // thx Crockford function A() { ….. } A.prototype.v = 'prop '; A.prototype.AA1 = 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.AA1(); Inheritance
  • 22. Contact info: • Technical and project manager • Vitali Hornik, PMP®, Ph.D. in IT (Computer Science) • vhornik@gmail.com