SlideShare a Scribd company logo
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 (20)

PPTX
JSON: The Basics
Jeff Fox
 
PDF
Java I/O
Jussi Pohjolainen
 
PPTX
[Final] ReactJS presentation
洪 鹏发
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPS
Wrapper class
kamal kotecha
 
PPT
Oops concepts in php
CPD INDIA
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
PPT
Javascript arrays
Hassan Dar
 
PPTX
Javascript
Sun Technlogies
 
ODP
Python Modules
Nitin Reddy Katkam
 
PDF
Java Serialization
imypraz
 
PPTX
Selenium Locators
Satyam Pandey
 
PPSX
Php and MySQL
Tiji Thomas
 
PPTX
Type casting in java
Farooq Baloch
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPT
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
PPSX
Java String class
DrRajeshreeKhande
 
PPTX
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 
JSON: The Basics
Jeff Fox
 
[Final] ReactJS presentation
洪 鹏发
 
jQuery for beginners
Arulmurugan Rajaraman
 
Wrapper class
kamal kotecha
 
Oops concepts in php
CPD INDIA
 
Methods in Java
Jussi Pohjolainen
 
JavaScript: Variables and Functions
Jussi Pohjolainen
 
Javascript arrays
Hassan Dar
 
Javascript
Sun Technlogies
 
Python Modules
Nitin Reddy Katkam
 
Java Serialization
imypraz
 
Selenium Locators
Satyam Pandey
 
Php and MySQL
Tiji Thomas
 
Type casting in java
Farooq Baloch
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Java String class
DrRajeshreeKhande
 
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 

Viewers also liked (6)

PPT
Basic Javascript
Bunlong Van
 
PPTX
Lifi (Light Fidility)
Vishal Mittal
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPTX
JakartaJS - How I Learn Javascript From Basic
Irfan Maulana
 
Basic Javascript
Bunlong Van
 
Lifi (Light Fidility)
Vishal Mittal
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
JavaScript - An Introduction
Manvendra Singh
 
JakartaJS - How I Learn Javascript From Basic
Irfan Maulana
 
Ad

Similar to Javascript basic course (20)

PPTX
Ajaxworld
deannalagason
 
KEY
Javascript tid-bits
David Atchley
 
PDF
The many facets of code reuse in JavaScript
Leonardo Borges
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Let's JavaScript
Paweł Dorofiejczyk
 
PDF
Javascript
Aditya Gaur
 
PDF
JavaScript Core
François Sarradin
 
PPT
eXo SEA - JavaScript Introduction Training
Hoat Le
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
KEY
Exciting JavaScript - Part I
Eugene Lazutkin
 
PDF
JavaScript Primer
Daniel Cousineau
 
PPTX
JavaScript OOPS Implimentation
Usman Mehmood
 
PDF
Scalable JavaScript
Ynon Perek
 
PPT
Advanced JavaScript
Fu Cheng
 
PPTX
Object Oriented Javascript part2
Usman Mehmood
 
PDF
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
PPTX
Advanced JavaScript
Nascenia IT
 
KEY
The JavaScript Programming Primer
Mike Wilcox
 
PPT
JavaScript - Programming Languages course
yoavrubin
 
Ajaxworld
deannalagason
 
Javascript tid-bits
David Atchley
 
The many facets of code reuse in JavaScript
Leonardo Borges
 
Object Oriented JavaScript
Donald Sipe
 
Let's JavaScript
Paweł Dorofiejczyk
 
Javascript
Aditya Gaur
 
JavaScript Core
François Sarradin
 
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Exciting JavaScript - Part I
Eugene Lazutkin
 
JavaScript Primer
Daniel Cousineau
 
JavaScript OOPS Implimentation
Usman Mehmood
 
Scalable JavaScript
Ynon Perek
 
Advanced JavaScript
Fu Cheng
 
Object Oriented Javascript part2
Usman Mehmood
 
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
Advanced JavaScript
Nascenia IT
 
The JavaScript Programming Primer
Mike Wilcox
 
JavaScript - Programming Languages course
yoavrubin
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 

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.