JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.

Slides:



Advertisements
Similar presentations
JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Advertisements

 Dimitar Ivanov Introduction to programming with microcontrollers.
C# Advanced Topics Methods, Classes and Objects SoftUni Team Technical Trainers Software University
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Methods Writing and using methods, overloads, ref, out SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Advanced JavaScript Course Introduction SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
Design Patterns: Structural Design Patterns
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Start Your Own Blog Angel Georgiev Part-time Trainer angeru.softuni-friends.org Software University The Culture of Knowledge Sharing.
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Static Members and Namespaces Static Members, Indexers, Operators, Namespaces SoftUni Team Technical Trainers Software University
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
CSS Transitions and Animations Animated HTML Elements SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Object-Oriented Programming Course Introduction Svetlin Nakov Technical Trainer Software University
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Simulating OOP in JavaScript Function Constructor, Prototypes, "this" Object, Classical and Prototypal Model Software University Technical.
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
JavaScript Patterns to Clean Up Your Code Dan Wahlin.
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Team Work and Personal Skills Course Introduction Angel Georgiev Part-time Trainer Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Advanced JavaScript Course Introduction SoftUni Team Technical Trainers Software University
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Asynchronous Programming Writing Asynchronous Code in Java SoftUni Team Technical Trainers Software University
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Mocking tools for easier unit testing
Extending functionality using Collections
Presentation transcript:

JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni Team

2 1.Why we need modules and patterns 2."Prototype" Pattern 3."Module" Pattern 4."Revealing Module" Pattern 5."Revealing Prototype" Pattern 6.Method Chaining Table of Contents

3  Modularity  Easy maintenance  No duplicate function names  Don't pollute the global scope Why we need modules and patterns?

The Prototype Pattern

5  Pros:  “Modularize” code into re-useable objects  Variables / functions are NOT in the global scope  Functions loaded into memory once  Possible to "override" functions through prototyping  Cons:  "this" can be tricky  Constructor separate from prototype definition Prototype Pattern – Pros and Cons

Prototype Pattern - Example var Calculator = function(name) { this.name = name; this.name = name;}; Calculator.prototype = { add: function (x, y) { add: function (x, y) { return (x + y); return (x + y); }} var calc = new Calculator("SoftUniCalc"); calc.add(2, 4);

7  Prototype pattern leverages intrinsic JavaScript functionality  Comprised of a constructor and a prototype  Provides extension capabilities Prototype Pattern – Summary

The "Module" Pattern Hiding Members

9  Pros:  “Modularize” code into re-useable objects  Variables / functions are NOT in the global scope  Expose only public members  Hide internal data and functions  Cons:  Not easy to extend  Some complain about debugging Module Pattern – Pros and Cons

10 Module Pattern: Structure var c alculator = function() { // private variables // private variables // private functions // private functions return { return { // public members // public members }; };};

11 Module Pattern: Example var calculator = function () { function logAction(action) { … } function logAction(action) { … } return { return { add: function (x, y) { add: function (x, y) { logAction('add'); logAction('add'); return (x + y); return (x + y); }, }, multiply: function (x, y) { multiply: function (x, y) { return (x * y); return (x * y); } }; };}; var calc = calculator('First'); calc.add(3, 3); calc.multiply(7, 8); Private members Public members You can call without new

12 Module Pattern: with IIFE var calculator = (function () { function logAction(action) { … } function logAction(action) { … } return { return { add: function (x, y) { add: function (x, y) { logAction('add'); logAction('add'); return (x + y); return (x + y); }, }, multiply: function (x, y) { multiply: function (x, y) { return (x * y); return (x * y); } }; };}()); calculator.add(3, 3); calculator.multiply(7, 8); The visible members create a closure with the private members Private members Public members

13  Module pattern provides encapsulation of variables and functions  Provides a way to add visibility to members  Public versus private members  Each object instance creates new copies of functions in memory Module Pattern – Summary

Module Pattern Live Demo

The Revealing Module Pattern Reveal the Most Interesting Members

16  Pros:  "Modularize" code into re-useable objects  Variables / functions taken out of the global namespace  Expose only visible members  "Cleaner" way to expose public members  Easy to change members privacy  Cons:  Not easy to extend  Some complain about debugging  Hard to mock hidden objects for testing Revealing Module Pattern – Pros and Cons

17 Revealing Module Pattern: Structure var module = sfunction() { // private variables // private variables // private functions // private functions return { return { // public members // public members }; };}; Give only reference to exposed function

18 Revealing Module Pattern – Example var calculator = function () { function logAction(action) { … }; function logAction(action) { … }; function add(x, y) { function add(x, y) { logAction('add'); logAction('add'); return (x + y); return (x + y); } function multiply(x, y) { function multiply(x, y) { return (x * y); return (x * y); } return { return { add: add, add: add, multiply: multiply multiply: multiply }; };}; var calc = calculator(); calc.add(3, 3); Create a function constructor hidden Hidden function Expose (reveal) only public members

19 Revealing Module Pattern – Example var calculator = (function () { function logAction(action) { … }; function logAction(action) { … }; function add(x, y) { function add(x, y) { logAction('add'); logAction('add'); return (x + y); return (x + y); } function multiply(x, y) { function multiply(x, y) { return (x * y); return (x * y); } return { return { add: add, add: add, multiply: multiply multiply: multiply }; };}()); calculator.add(3, 3); calculator.multiply(3, 5); Create a function constructor hidden Hidden function Expose (reveal) only public members

20  Revealing Module Pattern provides encapsulation of variables and functions  Provides a way to add visibility  Public versus private members  Cleaner way to expose public members  Cleaner than Module pattern  Extending objects can be difficult since no prototyping is used Revealing Module Pattern - Summary

Revealing Module Pattern Live Demo

The Revealing Prototype Pattern Reveal the Most Interesting Members through the Object Prototype

23  Pros:  "Modularize" code into re-useable objects  Variables / functions taken out of the global namespace  Expose only public members  Functions are loaded into memory once only  Extensible  Cons:  Using " this " can be tricky  Constructor is separated from the prototype  Can not be used on inheritance Revealing Prototype Pattern – Pros and Cons

24 Revealing Prototype Pattern: Structure var Constructor = function () { // constructor defined here // constructor defined here} Constructor.prototype = (function() { var privateFunc = 5; // hidden variables var privateFunc = 5; // hidden variables function privateFunc() { … } // hidden functions function privateFunc() { … } // hidden functions return { return { someFunc: pointerToSomeFunc someFunc: pointerToSomeFunc anotherFunc: pointerToAnotherFunc anotherFunc: pointerToAnotherFunc }; };}()); Create IIFE for the prototype

25 Revealing Prototype Pattern – Example var Calculator = function (name) { … }; Calculator.prototype = (function () { var add, subtract, showResult, formatResult; var add, subtract, showResult, formatResult; add = function (x) { … }; add = function (x) { … }; subtract = function (x) { … }; subtract = function (x) { … }; showResult = function () { … }; showResult = function () { … }; return { return { add: add, add: add, subtract: subtract, subtract: subtract, showResult: showResult showResult: showResult }; };}()); var calc = new Calculator('First'); We can have hidden data in the prototype Expose only public methods for the prototype

26  Revealing Prototype Pattern provides encapsulation of variables and functions  Provides a way to add visibility  Exposed versus hidden members  Provides extension capabilities Revealing Prototype Pattern – Summary

Revealing Prototype Pattern Live Demo

Augmenting Modules Live Demo

Method Chaining

30  Method chaining is a technique (pattern) that involve calling multiple functions on the same object consecutively  Much cleaner code  The code is easier to understand  No need of temporary variables to save each step of the process JavaScript Method Chaining var doggy = new Dog().setName("Fluffy").setName("Fluffy").setColor("purple").setColor("purple").setGender("male");.setGender("male"); var doggy = new Dog(); doggy.setName("Fluffy");doggy.setColor("purple");doggy.setGender("male");

31 JavaScript Method Chaining – Example var Dog = function() { this._name = 'Fluffy'; this._name = 'Fluffy'; this._color = 'purple'; this._color = 'purple';} Dog.prototype.setName = function(name) { this._name = name; this._name = name; return this; return this;} Dog.prototype.setColor = function(color) { this._color = color; this._color = color; return this; return this;} var doggy = new Dog().setName('Fluffy').setColor('purple'); console.log(doggy); // { _name: 'Fluffy', _color: 'purple' }

Method Chaining Live Demo

? ? ? ? ? ? ? ? ? JavaScript Modules and Patterns

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 34  Attribution: this work may contain portions from  “JavaScript Basics" course by Telerik Academy under CC-BY-NC-SA licenseJavaScript BasicsCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg