SlideShare a Scribd company logo
OBJECT ORIENTED JAVASCRIPT
Getting Started
Created By :
Usman Mehmood
Senior software Engineer
Systems Limited
97-Aziz Avenue Canal Bank, Gulberg V, Lahore, Pakistan
Office: +92-42- 3577 5582 - 85 EXT: 245
VOIP: + 1 732 994 6492 - 94 EXT: 245 Fax: +92 423 587 7238
Email: usman.tufail@systemsltd.com
Website: http://www.systemsltd.com
OVERVIEW
 JavaScript is an object oriented dynamic language;
it has types and operators, core objects, and
methods. Its syntax comes from the Java and C
languages, so many structures from those
languages apply to JavaScript as well. One of the
key differences is that JavaScript does not have
classes; instead, the class functionality is
accomplished by object prototypes. The other main
difference is that functions are objects
LET'S START OFF BY LOOKING AT THE BUILDING
BLOCK OF ANY LANGUAGE: THE TYPES.
JAVASCRIPT PROGRAMS MANIPULATE VALUES, AND
THOSE VALUES ALL BELONG TO A TYPE.
JAVASCRIPT'S TYPES ARE:
 oh, and Undefined and Null, which are slightly odd.
And Arrays, which are a special kind of object.
And Dates and Regular Expressions, which are
objects that you get for free. And to be technically
accurate, functions are just a special type of object.
So the type diagram looks more like this:
CORE OBJECTS
 JavaScript has several objects included in its core;
for example, there are objects like Math, Object,
Array, and String. The example below shows how to
use the Math object to get a random number by
using its random() method.
 you can look here for all core objects
NOTE
 Every object in JavaScript is an instance of the
Object and therefore inherits all its properties and
methods.
CUSTOM OBJECTS
The Class:
JavaScript is a prototype-based language which contains no class statement,
such as is found in C++ or Java. This is sometimes confusing for programmers
accustomed to languages with a class statement. Instead, JavaScript uses
functions as classes. Defining a class is as easy as defining a function. In the
example below we define a new class called Person.
THE OBJECT (CLASS INSTANCE)
To create a new instance of an object obj we use the
statement new obj, assigning the result (which is of type obj) to a
variable to access it later. In the example below we define a
class named Person and we create two instances
(person1 and person2).
Please also see Object.create for a new
and alternative instantiation method.
THE CONSTRUCTOR
 The constructor is called at the moment of instantiation (the moment when the
object instance is created). The constructor is a method of the class. In
JavaScript, the function serves as the constructor of the object; therefore,
there is no need to explicitly define a constructor method. Every action
declared in the class gets executed at the time of instantiation. In the example
below, the constructor of the class Person displays an alert when a Person is
instantiated.
THE PROPERTY (OBJECT ATTRIBUTE)
 Properties are variables contained in the class; every instance
of the object has those properties. Properties should be set in
the prototype property of the class (function) so that
inheritance works correctly.
 Working with properties from within the class is done by the
keyword this, which refers to the current object. Accessing
(reading or writing) a property outside of the class is done with
the syntax: InstanceName.Property; this is the same syntax
used by C++, Java, and a number of other languages. (Inside
the class the syntax this.Property is used to get or set the
property's value.)
IN THE EXAMPLE BELOW WE DEFINE
THE GENDER PROPERTY FOR THE PERSON CLASS AND WE
DEFINE IT AT INSTANTIATION
THE METHODS
 Methods follow the same logic as properties; the
difference is that they are functions and they are
defined as functions. Calling a method is similar to
accessing a property, but you add () at the end of
the method name, possibly with arguments. To
define a method, assign a function to a named
property of the class's prototype property; the name
that the function is assigned to is the name that the
method is called by on the object.
SCOPE OF VARIABLES PUBLIC/PRIVATE
 private variables are declared with the 'var' keyword
inside the object, and can only be accessed by private
functions and privileged methods.
 private functions are declared inline inside the object's
constructor (or alternatively may be defined
via var functionName=function(){...}) and may only be
called by privileged methods (including the object's
constructor).
SCOPE OF VARIABLES PUBLIC/PRIVATE
 privileged methods are declared
with this.methodName=function(){...} and may invoked by code
external to the object.
 public properties are declared with this.variableName and may
be read/written from outside the object.
 public methods are defined
by Classname.prototype.methodName = function(){...} and may
be called from outside the object.
 prototype properties are defined
by Classname.prototype.propertyName = someValue
 static properties are defined by Classname.propertyName =
someValue
PUBLIC MEMBERS
 The members of an object are all public members. Any
function can access, modify, or delete those members, or add
new members. There are two main ways of putting members
in a new object:
 In the constructor
 This technique is usually used to initialize public instance
variables. The constructor's this variable is used to add
members to the object.
http://jsfiddle.net/usmantufail/Ybd7v/6/
PUBLIC VARIABLES
 In the prototype
 This technique is usually used to add public methods. When a
member is sought and it isn't found in the object itself, then it is
taken from the object's constructor's prototype member. The
prototype mechanism is used for inheritance. It also conserves
memory. To add a method to all objects made by a constructor,
add a function to the constructor's prototype:
PRIVATE MEMBERS
 Private members are made by the constructor. Ordinary vars and
parameters of the constructor becomes the private members.
 They are attached to the object, but they are not accessible to the
outside, nor are they accessible to the object's own public methods. They
are accessible to private methods. Private methods are inner functions of
the constructor.
PRIVILEGED METHOD
 A privileged method is able to access the private
variables and methods, and is itself accessible to
the public methods and the outside. It is possible to
delete or replace a privileged method, but it is not
possible to alter it, or to force it to give up its
secrets.
 Privileged methods are assigned with this within the
constructor.
 http://jsfiddle.net/usmantufail/gXgT7/1/
 http://jsfiddle.net/usmantufail/Ybd7v/7/
PROTOTYPE-BASED PROGRAMMING
 Prototype-based programming is a style of object-
oriented programming in which classes are not
present, and behavior reuse (known as inheritance
in class-based languages) is accomplished through
a process of decorating existing objects which
serve as prototypes. This model is also known as
class-less, prototype-oriented, or instance-based
programming.
IN THE EXAMPLE BELOW WE DEFINE AND USE THE
METHOD SAYHELLO() FOR THE PERSON CLASS.
http://jsfiddle.net/usmantufail/9ufnw/
INHERITANCE
 Inheritance is a way to create a class as a
specialized version of one or more classes
(JavaScript only supports single class inheritance).
The specialized class is commonly called the child,
and the other class is commonly called the parent.
In JavaScript you do this by assigning an instance
of the parent class to the child class, and then
specializing it. In modern browsers you can also
use Object.create to implement inheritance.
IMPORTANT
 JavaScript does not detect the child
class prototype.constructor see Core JavaScript 1.5
Reference:Global
Objects:Object:prototype property, so we must state
that manually.
IN THE EXAMPLE BELOW, WE DEFINE THE CLASS STUDENT AS A CHILD
CLASS OF PERSON. THEN WE REDEFINE THE SAYHELLO() METHOD
AND ADD THESAYGOODBYE() METHOD.
Click here to open code
WHAT IS NAMESPACING?
 In many programming languages, namespacing is a technique
employed to avoid collisions with other objects or variables in
the global namespace. They're also extremely useful for
helping organize blocks of functionality in your application into
easily manageable groups that can be uniquely identified.
http://jsfiddle.net/usmantufail/BDRM4/
CREATING OBJECTS
 http://jsfiddle.net/mehmoodusman786/JLQFY/
INNER FUNCTIONS
 JavaScript function declarations are allowed inside
other functions. An important detail of nested
functions in JavaScript is that they can access
variables in their parent function's scope:
 http://jsfiddle.net/usmantufail/GFQha/2/
CLOSURES
 A closure is a function defined within another scope
that has access to all the variables within the outer
scope
 Closures are a strange concept to get to grips with,
but once this core concept is understood they’re
relatively easy to understand: a closure is created
when a developer accesses variables outside of the
immediate lexical scope.
http://jsfiddle.net/usmantufail/95PTq/1/
http://jsfiddle.net/usmantufail/95PTq/3/
MEMORY LEAKS
 An unfortunate side effect of closures is that they make it trivially easy to leak
memory in Internet Explorer. JavaScript is a garbage collected language — objects
are allocated memory upon their creation and that memory is reclaimed by the
browser when no references to an object remain. Objects provided by the host
environment are handled by that environment.
 A memory leak in IE occurs any time a circular reference is formed between a
JavaScript object and a native object. Consider the following:
The circular reference formed above creates a memory leak; IE will not
free the memory used by el and o until the browser is completely
restarted.
MODULE PATTERN
 http://jsfiddle.net/usmantufail/Emqt2/
ASSIGNMENT
 How to avoid memory leaks give some examples
and work around
 Create a reusable JavaScript class using Singleton
module pattern use public private members
 What is Unobtrusive JavaScript.
 Create a class calculator using OOP concepts.
 Create a class scientific calculator with Min 4
additional functions and all functions must be
parameter less you are not allowed to declare
parameters in functions but need to use parameters
passed.
Ad

More Related Content

What's hot (19)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
University of Potsdam
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
Sherihan Anver
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Singsys Pte Ltd
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
Professional Guru
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
parag
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
Sherihan Anver
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
parag
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 

Viewers also liked (9)

Introduction to whats new in css3
Introduction to whats new in css3Introduction to whats new in css3
Introduction to whats new in css3
Usman Mehmood
 
Recap of first day
Recap of first dayRecap of first day
Recap of first day
United Nations Office to Support the International Decade for Action 'Water for Life' 2005-2015
 
Interviews
InterviewsInterviews
Interviews
guestadc8bc4
 
My Favorite Items
My Favorite ItemsMy Favorite Items
My Favorite Items
guestadc8bc4
 
My Favorite Gift
My Favorite GiftMy Favorite Gift
My Favorite Gift
guestadc8bc4
 
Water, energy, wastewater & waste in Casablanca by Saïd Chadli, Director of I...
Water, energy, wastewater & waste in Casablanca by Saïd Chadli, Director of I...Water, energy, wastewater & waste in Casablanca by Saïd Chadli, Director of I...
Water, energy, wastewater & waste in Casablanca by Saïd Chadli, Director of I...
United Nations Office to Support the International Decade for Action 'Water for Life' 2005-2015
 
Recap from day 2 and overview of day 3, by Josefina Maestu, director UNW-DPAC
Recap from day 2 and overview of day 3, by Josefina Maestu, director UNW-DPACRecap from day 2 and overview of day 3, by Josefina Maestu, director UNW-DPAC
Recap from day 2 and overview of day 3, by Josefina Maestu, director UNW-DPAC
United Nations Office to Support the International Decade for Action 'Water for Life' 2005-2015
 
Seo Over view
Seo Over viewSeo Over view
Seo Over view
Usman Mehmood
 
Presentacion De CóRdoba
Presentacion De CóRdobaPresentacion De CóRdoba
Presentacion De CóRdoba
nrometti
 
Ad

Similar to Object Oriented Javascript part2 (20)

Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
Usman Mehmood
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
mrxyz19
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
Sudip Simkhada
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
Kuntal Bhowmick
 
Java notes
Java notesJava notes
Java notes
Upasana Talukdar
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
Oops
OopsOops
Oops
Sankar Balasubramanian
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Ayes Chinmay
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
biswajit2015
 
python.pptx
python.pptxpython.pptx
python.pptx
Dhanushrajucm
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
FerryKemperman
 
My c++
My c++My c++
My c++
snathick
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
Usman Mehmood
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
mrxyz19
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
Kuntal Bhowmick
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Ayes Chinmay
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
biswajit2015
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
FerryKemperman
 
Ad

Recently uploaded (20)

Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 

Object Oriented Javascript part2

  • 1. OBJECT ORIENTED JAVASCRIPT Getting Started Created By : Usman Mehmood Senior software Engineer Systems Limited 97-Aziz Avenue Canal Bank, Gulberg V, Lahore, Pakistan Office: +92-42- 3577 5582 - 85 EXT: 245 VOIP: + 1 732 994 6492 - 94 EXT: 245 Fax: +92 423 587 7238 Email: [email protected] Website: http://www.systemsltd.com
  • 2. OVERVIEW  JavaScript is an object oriented dynamic language; it has types and operators, core objects, and methods. Its syntax comes from the Java and C languages, so many structures from those languages apply to JavaScript as well. One of the key differences is that JavaScript does not have classes; instead, the class functionality is accomplished by object prototypes. The other main difference is that functions are objects
  • 3. LET'S START OFF BY LOOKING AT THE BUILDING BLOCK OF ANY LANGUAGE: THE TYPES. JAVASCRIPT PROGRAMS MANIPULATE VALUES, AND THOSE VALUES ALL BELONG TO A TYPE. JAVASCRIPT'S TYPES ARE:
  • 4.  oh, and Undefined and Null, which are slightly odd. And Arrays, which are a special kind of object. And Dates and Regular Expressions, which are objects that you get for free. And to be technically accurate, functions are just a special type of object. So the type diagram looks more like this:
  • 5. CORE OBJECTS  JavaScript has several objects included in its core; for example, there are objects like Math, Object, Array, and String. The example below shows how to use the Math object to get a random number by using its random() method.  you can look here for all core objects
  • 6. NOTE  Every object in JavaScript is an instance of the Object and therefore inherits all its properties and methods.
  • 7. CUSTOM OBJECTS The Class: JavaScript is a prototype-based language which contains no class statement, such as is found in C++ or Java. This is sometimes confusing for programmers accustomed to languages with a class statement. Instead, JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.
  • 8. THE OBJECT (CLASS INSTANCE) To create a new instance of an object obj we use the statement new obj, assigning the result (which is of type obj) to a variable to access it later. In the example below we define a class named Person and we create two instances (person1 and person2). Please also see Object.create for a new and alternative instantiation method.
  • 9. THE CONSTRUCTOR  The constructor is called at the moment of instantiation (the moment when the object instance is created). The constructor is a method of the class. In JavaScript, the function serves as the constructor of the object; therefore, there is no need to explicitly define a constructor method. Every action declared in the class gets executed at the time of instantiation. In the example below, the constructor of the class Person displays an alert when a Person is instantiated.
  • 10. THE PROPERTY (OBJECT ATTRIBUTE)  Properties are variables contained in the class; every instance of the object has those properties. Properties should be set in the prototype property of the class (function) so that inheritance works correctly.  Working with properties from within the class is done by the keyword this, which refers to the current object. Accessing (reading or writing) a property outside of the class is done with the syntax: InstanceName.Property; this is the same syntax used by C++, Java, and a number of other languages. (Inside the class the syntax this.Property is used to get or set the property's value.)
  • 11. IN THE EXAMPLE BELOW WE DEFINE THE GENDER PROPERTY FOR THE PERSON CLASS AND WE DEFINE IT AT INSTANTIATION
  • 12. THE METHODS  Methods follow the same logic as properties; the difference is that they are functions and they are defined as functions. Calling a method is similar to accessing a property, but you add () at the end of the method name, possibly with arguments. To define a method, assign a function to a named property of the class's prototype property; the name that the function is assigned to is the name that the method is called by on the object.
  • 13. SCOPE OF VARIABLES PUBLIC/PRIVATE  private variables are declared with the 'var' keyword inside the object, and can only be accessed by private functions and privileged methods.  private functions are declared inline inside the object's constructor (or alternatively may be defined via var functionName=function(){...}) and may only be called by privileged methods (including the object's constructor).
  • 14. SCOPE OF VARIABLES PUBLIC/PRIVATE  privileged methods are declared with this.methodName=function(){...} and may invoked by code external to the object.  public properties are declared with this.variableName and may be read/written from outside the object.  public methods are defined by Classname.prototype.methodName = function(){...} and may be called from outside the object.  prototype properties are defined by Classname.prototype.propertyName = someValue  static properties are defined by Classname.propertyName = someValue
  • 15. PUBLIC MEMBERS  The members of an object are all public members. Any function can access, modify, or delete those members, or add new members. There are two main ways of putting members in a new object:  In the constructor  This technique is usually used to initialize public instance variables. The constructor's this variable is used to add members to the object. http://jsfiddle.net/usmantufail/Ybd7v/6/
  • 16. PUBLIC VARIABLES  In the prototype  This technique is usually used to add public methods. When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's prototype member. The prototype mechanism is used for inheritance. It also conserves memory. To add a method to all objects made by a constructor, add a function to the constructor's prototype:
  • 17. PRIVATE MEMBERS  Private members are made by the constructor. Ordinary vars and parameters of the constructor becomes the private members.  They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object's own public methods. They are accessible to private methods. Private methods are inner functions of the constructor.
  • 18. PRIVILEGED METHOD  A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.  Privileged methods are assigned with this within the constructor.  http://jsfiddle.net/usmantufail/gXgT7/1/  http://jsfiddle.net/usmantufail/Ybd7v/7/
  • 19. PROTOTYPE-BASED PROGRAMMING  Prototype-based programming is a style of object- oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished through a process of decorating existing objects which serve as prototypes. This model is also known as class-less, prototype-oriented, or instance-based programming.
  • 20. IN THE EXAMPLE BELOW WE DEFINE AND USE THE METHOD SAYHELLO() FOR THE PERSON CLASS. http://jsfiddle.net/usmantufail/9ufnw/
  • 21. INHERITANCE  Inheritance is a way to create a class as a specialized version of one or more classes (JavaScript only supports single class inheritance). The specialized class is commonly called the child, and the other class is commonly called the parent. In JavaScript you do this by assigning an instance of the parent class to the child class, and then specializing it. In modern browsers you can also use Object.create to implement inheritance.
  • 22. IMPORTANT  JavaScript does not detect the child class prototype.constructor see Core JavaScript 1.5 Reference:Global Objects:Object:prototype property, so we must state that manually.
  • 23. IN THE EXAMPLE BELOW, WE DEFINE THE CLASS STUDENT AS A CHILD CLASS OF PERSON. THEN WE REDEFINE THE SAYHELLO() METHOD AND ADD THESAYGOODBYE() METHOD. Click here to open code
  • 24. WHAT IS NAMESPACING?  In many programming languages, namespacing is a technique employed to avoid collisions with other objects or variables in the global namespace. They're also extremely useful for helping organize blocks of functionality in your application into easily manageable groups that can be uniquely identified. http://jsfiddle.net/usmantufail/BDRM4/
  • 26. INNER FUNCTIONS  JavaScript function declarations are allowed inside other functions. An important detail of nested functions in JavaScript is that they can access variables in their parent function's scope:  http://jsfiddle.net/usmantufail/GFQha/2/
  • 27. CLOSURES  A closure is a function defined within another scope that has access to all the variables within the outer scope  Closures are a strange concept to get to grips with, but once this core concept is understood they’re relatively easy to understand: a closure is created when a developer accesses variables outside of the immediate lexical scope. http://jsfiddle.net/usmantufail/95PTq/1/ http://jsfiddle.net/usmantufail/95PTq/3/
  • 28. MEMORY LEAKS  An unfortunate side effect of closures is that they make it trivially easy to leak memory in Internet Explorer. JavaScript is a garbage collected language — objects are allocated memory upon their creation and that memory is reclaimed by the browser when no references to an object remain. Objects provided by the host environment are handled by that environment.  A memory leak in IE occurs any time a circular reference is formed between a JavaScript object and a native object. Consider the following: The circular reference formed above creates a memory leak; IE will not free the memory used by el and o until the browser is completely restarted.
  • 30. ASSIGNMENT  How to avoid memory leaks give some examples and work around  Create a reusable JavaScript class using Singleton module pattern use public private members  What is Unobtrusive JavaScript.  Create a class calculator using OOP concepts.  Create a class scientific calculator with Min 4 additional functions and all functions must be parameter less you are not allowed to declare parameters in functions but need to use parameters passed.