Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
NoSQL Concepts, Redis, MongoDB, CouchDB
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Marketing Mix, SWOT Analysis and Stages of Developing an Idea Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Course Overview Doncho Minkov Telerik Software Academy Technical Trainer
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy

1. Public/Private fields in JavaScript 2. Module pattern 3. Revealing module pattern 4. Revealing prototype pattern 5. Singleton pattern 2

Using the function scope

 Each variable is defined:  In the global scope (Public)  In a function scope (Private) 4 var global = 5; function myFunction() { var private = global; function innerFunction(){ var innerPrivate = private; }}

Live Demo

Hide members

 Pros:  “Modularize” code into re-useable objects  Variables/functions not in global namespace  Expose only public members  Cons:  Not easy to extend  Some complain about debugging 7

8 var module = (function() { //private variables //private functions return { //public members someFunc: function() {…}, anotherFunc: function() {…} };}());  Structure:

9 var controls = (function () { //hidden members //hidden members function formatResult(name, value) { //… } function formatResult(name, value) { //… } //visible members //visible members return { return { Calculator: function (name) { Calculator: function (name) { var result; var result; result = 0; result = 0; this.add = function (x) { //… }; this.add = function (x) { //… }; this.subtract = function (x) { //…}; this.subtract = function (x) { //…}; this.showResult = function () { //… }; this.showResult = function () { //… }; } }; };}()); var calc = new controls.Calculator('First'); calc.add(7);calc.showResult();  Example:

10 var controls = (function () { //hidden members //hidden members function formatResult(name, value) { //… } function formatResult(name, value) { //… } //visible members //visible members return { return { Calculator: function (name) { Calculator: function (name) { var result; var result; result = 0; result = 0; this.add = function (x) { //… }; this.add = function (x) { //… }; this.subtract = function (x) { //…}; this.subtract = function (x) { //…}; this.showResult = function () { //… }; this.showResult = function () { //… }; } }; };}()); var calc = new controls.Calculator('First'); calc.add(7);calc.showResult();  Example: The visible members create closures with them

var controls = (function () { //hidden members //hidden members function formatResult(name, value) { //… } function formatResult(name, value) { //… } //visible members //visible members return { return { Calculator: function (name) { Calculator: function (name) { var result; var result; result = 0; result = 0; this.add = function (x) { //… }; this.add = function (x) { //… }; this.subtract = function (x) { //…}; this.subtract = function (x) { //…}; this.showResult = function () { //… }; this.showResult = function () { //… }; } }; };}()); var calc = new controls.Calculator('First'); calc.add(7);calc.showResult(); 11  Example: Visible function constructor

 Module pattern provides encapsulation of variables and functions  Provides a way to add visibility (public versus private) to members  Each object instance creates new copies of functions in memory 12

Live Demo

Reveal the most interesting members

 Pros:  “Modularize” code into re-useable objects  Variables/functions taken out of global namespace  Expose only visible members  "Cleaner" way to expose members  Easy to change members privacy  Cons:  Not easy to extend  Some complain about debugging  Hard to mock hidden objects for testing 15

16 var module = (function() { //hidden variables //hidden functions return { //visible members //visible members someFunc: referenceToFunction anotherFunc: referenceToOtherFunction };}());  Structure:

17 var controls = (function () { //hidden function //hidden function function formatResult(name, value) { // … } function formatResult(name, value) { // … } var Calculator = (function () { var Calculator = (function () { var Calculator = function (name) { // … }; var Calculator = function (name) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.showResult = function () { // …}; Calculator.prototype.showResult = function () { // …}; return Calculator; return Calculator; }()); }()); return { return { Calculator: Calculator Calculator: Calculator }; };}()); var calc = new controls.Calculator('First');  Example:

18 var controls = (function () { //hidden function //hidden function function formatResult(name, value) { // … } function formatResult(name, value) { // … } var Calculator = (function () { var Calculator = (function () { var Calculator = function (name) { // … }; var Calculator = function (name) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.showResult = function () { // …}; Calculator.prototype.showResult = function () { // …}; return Calculator; return Calculator; }()); }()); return { return { Calculator: Calculator Calculator: Calculator }; };}()); var calc = new controls.Calculator('First');  Example: Create the function constructor hidden

19 var controls = (function () { //hidden function //hidden function function formatResult(name, value) { // … } function formatResult(name, value) { // … } var Calculator = (function () { var Calculator = (function () { var Calculator = function (name) { // … }; var Calculator = function (name) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.add = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.subtract = function (x) { // … }; Calculator.prototype.showResult = function () { // …}; Calculator.prototype.showResult = function () { // …}; return Calculator; return Calculator; }()); }()); return { return { Calculator: Calculator Calculator: Calculator }; };}()); var calc = new controls.Calculator('First');  Example: Expose (reveal) only references to hidden member

 Module pattern provides encapsulation of variables and functions  Provides a way to add visibility (public versus private) to members  Extending objects can be difficult since no prototyping is used 20

Live Demo

Reveal the most interesting members (again)

 Pros:  “Modularize” code into re-useable objects  Variables/functions taken out of global namespace  Expose only public members  Functions are loaded into memory once  Extensible  Cons:  "this" can be tricky  Constructor is separated from prototype 23

24 var Constructor = function () { //constructor defined here } Constructor.prototype = (function() { //hidden variables //hidden functions return { //exposed members //exposed members someFunc: pointerToSomeFunc anotherFunc: pointerToAnotherFunc };}());  Structure:

25 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 () { // … }; formatResult = function (name, value) { // … }; formatResult = function (name, value) { // … }; return { return { add: add, add: add, subtract: subtract, subtract: subtract, showResult: showResult showResult: showResult }; };}()); var calc = new Calculator('First');  Example:

 Module pattern provides encapsulation of variables and functions  Provides a way to add visibility (exposed versus hidden) to members  Provides extension capabilities 26

Live Demo

One object to rule them all!

29 var module = function() { var instance, getInstance; var instance, getInstance; return { return { getInstance: function(){ getInstance: function(){ if(!instance){ if(!instance){ instance = new Instance(); instance = new Instance(); } return instance; return instance; } }; };}();  Structure:

30 var controls = function () { var Calculator, calculatorInstance; var Calculator, calculatorInstance; Calculator = (function () { Calculator = (function () { function Calculator() { //… } function Calculator() { //… } return Calculator; return Calculator; }()); }()); return { return { getCalculator: function () { getCalculator: function () { if (!calculatorInstance) { if (!calculatorInstance) { calculatorInstance = new Calculator(); calculatorInstance = new Calculator(); } return calculatorInstance; return calculatorInstance; } }; };}(); var calculator = controls.getCalculator();  Example:

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 “C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com

Create the Snake game using the Revealing module pattern. Design the game such that it has at least three modules.