SlideShare a Scribd company logo
1 of 25
Download to read offline
Javascript basic course

 Values and practices change
everyday, but principle does not.
What we're learning today?

● Javascript introduction (5 mins)
● Basic concepts (20 mins)
● OOP in Javascript (20 mins)
Introduction

● Original created and used in Netscape.
● A prototype based scripting language.
● Was formalized in the ECMAScript
  language standard.
● Using commonly in web, and also for
  other server side/desktop applications.
● JavaScript™ is a registered trademark of
  Oracle corporation.
Basic concepts of Javascript

 ● Variable scope
 ● What is this?
 ● Closure
 ● Prototype
Variable scope
  Global                            Local

    ● Declared outside functions.     ● Declared inside function
    ● Can be accessed everywhere.     ● Can be accessed only inside the
                                        function.
What is this?

All javascript code is executed in an execution context.
 ● Global code runs in Global execution context.
 ● Invocation of a function runs in a associated execution
    context.

this = object for getting the current execution context.
Closure
When you return a function => you're creating a closure.


A closure is a special object that combines two things:
 ● Function
 ● Binding local variables at the time that the function was
   created.
Closure example-1
 function add(x) {
    return function(y) {
        return x + y;
    };
 }
 var add5 = add(5);
 var result = add5(3);
 console.log(result);

 Guess the result?
Closure example-2 (Infamous loop)
 function addLinks() {
    for(var i=0, link; i< 5; i++) {
       link = document.createElement("a");
       link.innerHTML = "Link " + i;
       link.onclick = function() {
            alert(i);
       };
    }
 }
 window.onload = addLinks();
Closure example-2 (Infamous loop-fixed)
function addLinks() {
   for(var i=0, link; i< 5; i++) {
      link = document.createElement("a");
      link.innerHTML = "Link " + i;
      link.onclick = function(num) {
            return function() {
               alert(num);
            };
      } (i);
   }
}
window.onload = addLinks();
Prototype
● A object is used as pattern to create other objects.
● When a function A is created:
   ○ Function A contains "prototype property" that
     associated to A.prototype object.
   ○ The A.prototype object contains a "constructor
     property" that links back to A function.
Prototype 

function People() {

}
Prototype 

function People() {

}

var steve = new People()

People.prototype object is
used as reference for
method look up and
construct the new object.
OOP in Javascript
● Private, public, privilege
● Inheritance
● Modularization with namespace
Private

Private variable      Private function

function People() {   function People() {
  var name;             //declaration-w1
                        var sayHello = function()
}                       {
                        ...
                        }
                        //declaration-w2
                        function sayHello() {
                         ...
                        }
                      }
                      Private function can access to private
                      variable or global variable only
Public

Public variable       Public function

function People() {   function People() {
  this.name = null;
                      }
}
                      People.prototype.sayHi = function() {
                      ...
                      }

                      Public functions cannot access to
                      private variables/functions.
Privilege functions

Declaration                Usage

function People() {         ● To introduce private
 this.sayHi = function()      variable/functions to public function.
 {
 ...
 }

}
Private-public-privilege access matrix


                    Private func   Privilege func   Public func


 Private var/func   YES            YES              NO




 Public var/func    NO             YES              YES




 Privilege func     NO             YES              YES
Javascript inheritance

function People() {

}

function Woman() {

}

//Make Woman inherits from People
Woman.prototype = new People();
Woman.prototype.constructor = Woman;
Javascript inheritance - prototype chain
Javascript inheritance - call super method

function People() {
}
People.prototype.sayHi = function() {
...
}

function Woman() {

}

Woman.prototype = new People();
Woman.prototype.constructor = Woman;
Woman.prototype.sayHi = function() {
  People.prototype.sayHi.call(this);
  ...
}
Methods look up in the prototype chain

1. An object's methods (like all properties) are found by
   walking up the object's prototype chain.
2. The entries in the prototype chain are references to ordinary
   objects.
3. Object.create() or the new operator creates objects with
   a prototype chain.
Modularization with namespace

Namespace helps you organize your code better and limits bugs
caused by "name violation".

pyco = pyco || {};
pyco.app = pyco.app || {}

=> you have namespace: pyco.app
Now add functions to your pyco.app namespace:

pyco.app.Menu = new function() {
...
}
...
References:

● https://developer.mozilla.org/en/JavaScript/Guide/Closures
● https://developer.mozilla.
  org/en/JavaScript/Guide/Inheritance_constructor_prototype
● http://robertnyman.com/2008/10/09/explaining-javascript-
  scope-and-closures/
● http://javascript.crockford.com/prototypal.html
● http://www.crockford.com/javascript/private.html
● High performance Javascript book - Nicholas.C.Zakas.
Question?

More Related Content

What's hot

Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Java best practices
Java best practicesJava best practices
Java best practicesRay Toal
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basicsmsemenistyi
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Functional solid
Functional solidFunctional solid
Functional solidMatt Stine
 
Lombok Features
Lombok FeaturesLombok Features
Lombok Features文峰 眭
 

What's hot (20)

Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Java best practices
Java best practicesJava best practices
Java best practices
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Functional solid
Functional solidFunctional solid
Functional solid
 
Lombok Features
Lombok FeaturesLombok Features
Lombok Features
 

Viewers also liked

Viewers also liked (6)

Lifi (Light Fidility)
Lifi (Light Fidility)Lifi (Light Fidility)
Lifi (Light Fidility)
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
JakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From BasicJakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From Basic
 

Similar to Javascript basic course

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptLeonardo Borges
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design PatternsStefano Fago
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascriptrelay12
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptMichael Girouard
 
Java design patterns
Java design patternsJava design patterns
Java design patternsShawn Brito
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 

Similar to Javascript basic course (20)

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
6976.ppt
6976.ppt6976.ppt
6976.ppt
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
Enterprise js pratices
Enterprise js praticesEnterprise js pratices
Enterprise js pratices
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Javascript basic course

  • 1. Javascript basic course Values and practices change everyday, but principle does not.
  • 2. What we're learning today? ● Javascript introduction (5 mins) ● Basic concepts (20 mins) ● OOP in Javascript (20 mins)
  • 3. Introduction ● Original created and used in Netscape. ● A prototype based scripting language. ● Was formalized in the ECMAScript language standard. ● Using commonly in web, and also for other server side/desktop applications. ● JavaScript™ is a registered trademark of Oracle corporation.
  • 4. Basic concepts of Javascript ● Variable scope ● What is this? ● Closure ● Prototype
  • 5. Variable scope Global Local ● Declared outside functions. ● Declared inside function ● Can be accessed everywhere. ● Can be accessed only inside the function.
  • 6. What is this? All javascript code is executed in an execution context. ● Global code runs in Global execution context. ● Invocation of a function runs in a associated execution context. this = object for getting the current execution context.
  • 7. Closure When you return a function => you're creating a closure. A closure is a special object that combines two things: ● Function ● Binding local variables at the time that the function was created.
  • 8. Closure example-1 function add(x) { return function(y) { return x + y; }; } var add5 = add(5); var result = add5(3); console.log(result); Guess the result?
  • 9. Closure example-2 (Infamous loop) function addLinks() { for(var i=0, link; i< 5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function() { alert(i); }; } } window.onload = addLinks();
  • 10. Closure example-2 (Infamous loop-fixed) function addLinks() { for(var i=0, link; i< 5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function(num) { return function() { alert(num); }; } (i); } } window.onload = addLinks();
  • 11. Prototype ● A object is used as pattern to create other objects. ● When a function A is created: ○ Function A contains "prototype property" that associated to A.prototype object. ○ The A.prototype object contains a "constructor property" that links back to A function.
  • 13. Prototype  function People() { } var steve = new People() People.prototype object is used as reference for method look up and construct the new object.
  • 14. OOP in Javascript ● Private, public, privilege ● Inheritance ● Modularization with namespace
  • 15. Private Private variable Private function function People() { function People() { var name; //declaration-w1 var sayHello = function() } { ... } //declaration-w2 function sayHello() { ... } } Private function can access to private variable or global variable only
  • 16. Public Public variable Public function function People() { function People() { this.name = null; } } People.prototype.sayHi = function() { ... } Public functions cannot access to private variables/functions.
  • 17. Privilege functions Declaration Usage function People() { ● To introduce private this.sayHi = function() variable/functions to public function. { ... } }
  • 18. Private-public-privilege access matrix Private func Privilege func Public func Private var/func YES YES NO Public var/func NO YES YES Privilege func NO YES YES
  • 19. Javascript inheritance function People() { } function Woman() { } //Make Woman inherits from People Woman.prototype = new People(); Woman.prototype.constructor = Woman;
  • 20. Javascript inheritance - prototype chain
  • 21. Javascript inheritance - call super method function People() { } People.prototype.sayHi = function() { ... } function Woman() { } Woman.prototype = new People(); Woman.prototype.constructor = Woman; Woman.prototype.sayHi = function() { People.prototype.sayHi.call(this); ... }
  • 22. Methods look up in the prototype chain 1. An object's methods (like all properties) are found by walking up the object's prototype chain. 2. The entries in the prototype chain are references to ordinary objects. 3. Object.create() or the new operator creates objects with a prototype chain.
  • 23. Modularization with namespace Namespace helps you organize your code better and limits bugs caused by "name violation". pyco = pyco || {}; pyco.app = pyco.app || {} => you have namespace: pyco.app Now add functions to your pyco.app namespace: pyco.app.Menu = new function() { ... } ...
  • 24. References: ● https://developer.mozilla.org/en/JavaScript/Guide/Closures ● https://developer.mozilla. org/en/JavaScript/Guide/Inheritance_constructor_prototype ● http://robertnyman.com/2008/10/09/explaining-javascript- scope-and-closures/ ● http://javascript.crockford.com/prototypal.html ● http://www.crockford.com/javascript/private.html ● High performance Javascript book - Nicholas.C.Zakas.