SlideShare a Scribd company logo
Object Oriented JavaScript
VIVEK.P.S
http://vivekcek.wordpress.com
Agenda
• Objects in JavaScript
• Functions: Declaration, Expression and
Invocation
• this keyword
• Scopes and Closures
• Object oriented programming
Objects in JavaScript
Objects in JavaScript
• From one side, an object is an associative
array (called hash in some languages).
• It stores key-value pairs
Creating objects
• An empty object (you may also read as empty
associative array) is created with one of two
syntaxes:
• 1. o = new Object()
• 2. o = { } // the same
Object oriented java script
Literal syntax
It is also possible to create nested
objects
Non-existing properties, undefined
• But if the property does not exist,
then undefined is returned
Checking if a key exists
Iterating over keys-values
Object variables are references
• A variable which is assigned to object actually
keeps reference to it. That is, a variable stores
kind-of pointer to real data.
• The variable is a reference, not a value
An Example
Properties and methods
• You can store anything in object. Not just
simple values, but also functions.
Calling methods
• Note the this keyword
inside askName and sayHi. When a function is
called from the object, this becomes a
reference to this object.
The constructor function, “new”
• An object can be created, using
obj = { ..} syntax.
• Another way of creating an object in
JavaScript is to construct it by calling a
function with new directive.
A simple example
A simple example
• A function takes the following steps:
• Create this = {}.
• The function then runs and may change this,
add properties, methods etc.
• The resulting this is returned.
• So, the function constructs an object by
modifying this.
An example with the method
Summary
• Objects are associative arrays with additional features.
– Assign keys with obj[key] = value or obj.name = value
– Remove keys with delete obj.name
– Iterate over keys with for(key in obj), remember iteration order for
string keys is always in definition order, for numeric keys it may
change.
• Properties, which are functions, can be called as obj.method(). They
can refer to the object as this.
• Properties can be assigned and removed any time.
• A function can create new objects when run in constructor mode
as new Func(params).It takes this, which is initially an empty object,
and assigns properties to it. The result is returned (unless the
function has explicit return anotherObject call).
Functions: declarations, expressions
and Invocation
The syntax
• function f(arg1, arg2, ...) {
... code ...
}
Returning a value
• use return statement
If a function does not return anything, it’s result is
considered to be a special value, undefined
• function getNothing() {
// no return
}
Local variables
• variables, defined by var. Such variables are
called local and are only visible inside the
function
Function Declaration
• Function Declarations are parsed at pre-
execution stage, when the browser prepares
to execute the code.
• why the function declared this way can be
called both after and before the definition
Function Expression
Function Expression
• Function Expressions are created when the
execution flow reaches them. As a
consequence, Function Expressions can be
used only after they are executed.
Function is a value
• A function in JavaScript is a regular value. We
could even output it
• a function can be assigned, passed as a
parameter for another function and so on.
Running at place
• Running in place is mostly used when we want
to do the job involving local variables. We
don’t want our local variables to become
global, so wrap the code into a function.
• After the execution, the global namespace is
still clean. That’s a good practice.
Named function expressions
• The syntax is called named function expression
• the name is visible inside the function only
• NFEs exist to allow recursive calls from
anonymous functions
Summary
• Functions in JavaScript are regular values.
They can be assigned, passed around and
called when needed.
• A function which returns nothing actually
returns special value: undefined.
• Use verbs to name functions. Short names are
allowable in two edge cases: a name is used in
the nearest code only, or it is extremely widely
used.
Summary
this
Introduction
• The value of this is dynamic in JavaScript
• It is determined when function is called, not
when it is declared
• Any function may use this. It doesn’t matter if
the function is assigned to the object or not
• The real value of this is evaluated in the call
time anyway, and there are 4 possible cases
First, when called as a method
• If a function is called from the object (either
dot or square bracket will do), this refers to
this object.
• In the example above, func is initially apart
from the object. But when
called john.sayHi() sets this to the object
before dot: john.
Second, when called as a function
• If a function uses this, then it is meant to be
called as a method. A simple func() call is
usually a bug
Third, in new
• When a new function is called, this is
initialized as a new object
Fourth, explicit this
• A function can be called with
explicit this value. This is done out by one of
two methods: call or apply
Call
apply
• The func.apply is same as func.call, but it
accepts an array of arguments instead of a list
• The following two lines are same:
• func.call(john, 'firstName', 'surname')
Summary
Scopes and Closures
Initialization of functions and variables
• In JavaScript, all local variables and functions
are properties of the special internal object,
called LexicalEnvironment
• The top-level LexicalEnvironment in browser
is window. It is also called a global object.
Instantiation of top-level variables
• When the script is going to be executed, there
is a pre-processing stage called variables
instantiation.
• The top-level LexicalEnvironment in browser
is window. It is also called a global object.
• the browser finds function f, creates the
function and stores it as window.f
• Function Declarations are initialized before
the code is executed.
• As a side effect, f can be called before it is
declared (Hoisting)
• Second, the interpreter scans
for var declarations and
creates window properties. Assignments are
not executed at this stage. All variables start
as undefined.
• FunctionDeclarations become ready-to-use
functions. That allows to call a function before
it’s declaration.
• Variables start as undefined.
• All assignments happen later, when the
execution reaches them
Function variables
• When the interpreter is preparing to start
function code execution, before the first line is
run, an empty LexicalEnvironment is created
and populated with arguments, local variables
and nested functions.
• Then the function code runs, eventually
assignments are executed.A variable
assignment internally means that the
corresponding property of
theLexicalEnvironment gets a new value.
Closures
• So variable is a property of
the LexicalEnvironment object
• Here we discuss access to outer variables and
nested functions
Access to outer variables
Nested functions
• Functions can be nested one inside another,
forming a chain of LexicalEnvironments which
can also be called a scope chain
Closures
• Nested function may continue to live after the
outer function has finished:
Object oriented java script
Closure
• The inner function keeps a reference to the
outer LexicalEnvironment.
• The inner function may access variables from it
any time even if the outer function is finished.
• The browser keeps the LexicalEnvironment and
all it’s properties(variables) in memory until there
is an inner function which references it.
• This is called a closure.
[[Scope]] for new Function
• There is an exception to general scope binding
rule. When you create a function using new
Function, it’s[[Scope]] points to window, not
to current LexicalEnvironment.
Summary
• How variables are handled in JavaScript.
• How scopes work.
• What is a closure and how to use it.
• Possible pitfalls and subtles in working with
closures.
• In JavaScript, a variable can be declared after it
has been used (Hoisting).
• In other words; a variable can be used before it
has been declared (Hoisting).
Object Oriented Programming
Prototypal inheritance
• In JavaScript, the inheritance is prototype-
based. That means that there are no classes.
Instead, an object inherits from another
object
Object oriented java script
Object oriented java script
Object oriented java script
Object oriented java script
Object.create, Object.getPrototypeOf
• The __proto__ is a non-standard property,
provided by Firefox/Chrome. In other
browsers the property still exists internally,
but it is hidden
Object oriented java script
The prototype
• There is a good and crossbrowser way of
setting __proto__. It requires the use of
constructor functions.
Object oriented java script
hasOwnProperty
• All objects have hasOwnProperty method
which allows to check if a property belongs to
the object or its prototype.
Summary
• The inheritance is implemented through a
special property __proto__ (named
[[Prototype]] in the specification).
• When a property is accessed, and the
interpreter can’t find it in the object, it follows
the __proto__link and searches it there.
• The value of this for function properties is set
to the object, not its prototype.
• Assignment obj.prop = val and deletion delete
obj.prop
OOP patterns
Module pattern
Revealing Module pattern
var self = this
var self = this

More Related Content

What's hot (20)

JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
Bobby Bryant
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Rangana Sampath
 
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
Lovely Professional University
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
Sehwan Noh
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
Books
BooksBooks
Books
Steven Foster Murray
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
Mark Harrison
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
Lovely Professional University
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
NexThoughts Technologies
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
deannalagason
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
vikram singh
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
Patrick Sheridan
 
JS - Basics
JS - BasicsJS - Basics
JS - Basics
John Fischer
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
Bobby Bryant
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Rangana Sampath
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
Sehwan Noh
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
vikram singh
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
Patrick Sheridan
 

Viewers also liked (6)

Stem student center concept final
Stem student center concept finalStem student center concept final
Stem student center concept final
strongin2001
 
Student Center Overview
Student Center Overview Student Center Overview
Student Center Overview
Mason Stanley
 
Romania london sept 2012 - part2
Romania london sept 2012 - part2Romania london sept 2012 - part2
Romania london sept 2012 - part2
francescosocialcare
 
The Manhattanville Student Center Pavilion
The Manhattanville Student Center PavilionThe Manhattanville Student Center Pavilion
The Manhattanville Student Center Pavilion
tirellig
 
B.Arch Course Portfolio
B.Arch Course PortfolioB.Arch Course Portfolio
B.Arch Course Portfolio
SALMAN_HAROON1
 
Concept study of mahindra united world college,pune and pearl academy of fash...
Concept study of mahindra united world college,pune and pearl academy of fash...Concept study of mahindra united world college,pune and pearl academy of fash...
Concept study of mahindra united world college,pune and pearl academy of fash...
harshita batra
 
Stem student center concept final
Stem student center concept finalStem student center concept final
Stem student center concept final
strongin2001
 
Student Center Overview
Student Center Overview Student Center Overview
Student Center Overview
Mason Stanley
 
Romania london sept 2012 - part2
Romania london sept 2012 - part2Romania london sept 2012 - part2
Romania london sept 2012 - part2
francescosocialcare
 
The Manhattanville Student Center Pavilion
The Manhattanville Student Center PavilionThe Manhattanville Student Center Pavilion
The Manhattanville Student Center Pavilion
tirellig
 
B.Arch Course Portfolio
B.Arch Course PortfolioB.Arch Course Portfolio
B.Arch Course Portfolio
SALMAN_HAROON1
 
Concept study of mahindra united world college,pune and pearl academy of fash...
Concept study of mahindra united world college,pune and pearl academy of fash...Concept study of mahindra united world college,pune and pearl academy of fash...
Concept study of mahindra united world college,pune and pearl academy of fash...
harshita batra
 

Similar to Object oriented java script (20)

Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Functions
FunctionsFunctions
Functions
Gaurav Subham
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
Ivano Malavolta
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
javed75
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
Introduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptxIntroduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
Usman Mehmood
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
LearningTech
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
Assaf Weinberg
 
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
Vijay Kalyan
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closures
HDR1001
 
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
About Python
About PythonAbout Python
About Python
Shao-Chuan Wang
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
Suresh Jayanty
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
javed75
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
Introduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptxIntroduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
Usman Mehmood
 
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
Vijay Kalyan
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closures
HDR1001
 
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 

More from vivek p s (6)

Conversational UI Bot Framework
Conversational UI Bot FrameworkConversational UI Bot Framework
Conversational UI Bot Framework
vivek p s
 
Microsoft Bot Framework
Microsoft Bot FrameworkMicrosoft Bot Framework
Microsoft Bot Framework
vivek p s
 
Azure functions
Azure functionsAzure functions
Azure functions
vivek p s
 
Cloud computing Azure
Cloud computing AzureCloud computing Azure
Cloud computing Azure
vivek p s
 
Surya namskar
Surya namskarSurya namskar
Surya namskar
vivek p s
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
vivek p s
 
Conversational UI Bot Framework
Conversational UI Bot FrameworkConversational UI Bot Framework
Conversational UI Bot Framework
vivek p s
 
Microsoft Bot Framework
Microsoft Bot FrameworkMicrosoft Bot Framework
Microsoft Bot Framework
vivek p s
 
Azure functions
Azure functionsAzure functions
Azure functions
vivek p s
 
Cloud computing Azure
Cloud computing AzureCloud computing Azure
Cloud computing Azure
vivek p s
 
Surya namskar
Surya namskarSurya namskar
Surya namskar
vivek p s
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
vivek p s
 

Recently uploaded (20)

Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
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
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
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
 
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
 
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
 
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
 
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
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
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
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
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
 
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
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
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
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
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
 
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
 
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
 
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
 
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
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
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
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
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
 
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
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 

Object oriented java script

  • 2. Agenda • Objects in JavaScript • Functions: Declaration, Expression and Invocation • this keyword • Scopes and Closures • Object oriented programming
  • 4. Objects in JavaScript • From one side, an object is an associative array (called hash in some languages). • It stores key-value pairs
  • 5. Creating objects • An empty object (you may also read as empty associative array) is created with one of two syntaxes: • 1. o = new Object() • 2. o = { } // the same
  • 8. It is also possible to create nested objects
  • 9. Non-existing properties, undefined • But if the property does not exist, then undefined is returned
  • 10. Checking if a key exists
  • 12. Object variables are references • A variable which is assigned to object actually keeps reference to it. That is, a variable stores kind-of pointer to real data. • The variable is a reference, not a value
  • 14. Properties and methods • You can store anything in object. Not just simple values, but also functions.
  • 15. Calling methods • Note the this keyword inside askName and sayHi. When a function is called from the object, this becomes a reference to this object.
  • 16. The constructor function, “new” • An object can be created, using obj = { ..} syntax. • Another way of creating an object in JavaScript is to construct it by calling a function with new directive.
  • 18. A simple example • A function takes the following steps: • Create this = {}. • The function then runs and may change this, add properties, methods etc. • The resulting this is returned. • So, the function constructs an object by modifying this.
  • 19. An example with the method
  • 20. Summary • Objects are associative arrays with additional features. – Assign keys with obj[key] = value or obj.name = value – Remove keys with delete obj.name – Iterate over keys with for(key in obj), remember iteration order for string keys is always in definition order, for numeric keys it may change. • Properties, which are functions, can be called as obj.method(). They can refer to the object as this. • Properties can be assigned and removed any time. • A function can create new objects when run in constructor mode as new Func(params).It takes this, which is initially an empty object, and assigns properties to it. The result is returned (unless the function has explicit return anotherObject call).
  • 22. The syntax • function f(arg1, arg2, ...) { ... code ... }
  • 23. Returning a value • use return statement If a function does not return anything, it’s result is considered to be a special value, undefined • function getNothing() { // no return }
  • 24. Local variables • variables, defined by var. Such variables are called local and are only visible inside the function
  • 25. Function Declaration • Function Declarations are parsed at pre- execution stage, when the browser prepares to execute the code. • why the function declared this way can be called both after and before the definition
  • 27. Function Expression • Function Expressions are created when the execution flow reaches them. As a consequence, Function Expressions can be used only after they are executed.
  • 28. Function is a value • A function in JavaScript is a regular value. We could even output it • a function can be assigned, passed as a parameter for another function and so on.
  • 29. Running at place • Running in place is mostly used when we want to do the job involving local variables. We don’t want our local variables to become global, so wrap the code into a function. • After the execution, the global namespace is still clean. That’s a good practice.
  • 30. Named function expressions • The syntax is called named function expression • the name is visible inside the function only • NFEs exist to allow recursive calls from anonymous functions
  • 31. Summary • Functions in JavaScript are regular values. They can be assigned, passed around and called when needed. • A function which returns nothing actually returns special value: undefined. • Use verbs to name functions. Short names are allowable in two edge cases: a name is used in the nearest code only, or it is extremely widely used.
  • 33. this
  • 34. Introduction • The value of this is dynamic in JavaScript • It is determined when function is called, not when it is declared • Any function may use this. It doesn’t matter if the function is assigned to the object or not • The real value of this is evaluated in the call time anyway, and there are 4 possible cases
  • 35. First, when called as a method • If a function is called from the object (either dot or square bracket will do), this refers to this object. • In the example above, func is initially apart from the object. But when called john.sayHi() sets this to the object before dot: john.
  • 36. Second, when called as a function • If a function uses this, then it is meant to be called as a method. A simple func() call is usually a bug
  • 37. Third, in new • When a new function is called, this is initialized as a new object
  • 38. Fourth, explicit this • A function can be called with explicit this value. This is done out by one of two methods: call or apply
  • 39. Call
  • 40. apply • The func.apply is same as func.call, but it accepts an array of arguments instead of a list • The following two lines are same: • func.call(john, 'firstName', 'surname')
  • 43. Initialization of functions and variables • In JavaScript, all local variables and functions are properties of the special internal object, called LexicalEnvironment • The top-level LexicalEnvironment in browser is window. It is also called a global object.
  • 44. Instantiation of top-level variables • When the script is going to be executed, there is a pre-processing stage called variables instantiation. • The top-level LexicalEnvironment in browser is window. It is also called a global object.
  • 45. • the browser finds function f, creates the function and stores it as window.f • Function Declarations are initialized before the code is executed. • As a side effect, f can be called before it is declared (Hoisting)
  • 46. • Second, the interpreter scans for var declarations and creates window properties. Assignments are not executed at this stage. All variables start as undefined.
  • 47. • FunctionDeclarations become ready-to-use functions. That allows to call a function before it’s declaration. • Variables start as undefined. • All assignments happen later, when the execution reaches them
  • 48. Function variables • When the interpreter is preparing to start function code execution, before the first line is run, an empty LexicalEnvironment is created and populated with arguments, local variables and nested functions.
  • 49. • Then the function code runs, eventually assignments are executed.A variable assignment internally means that the corresponding property of theLexicalEnvironment gets a new value.
  • 50. Closures • So variable is a property of the LexicalEnvironment object • Here we discuss access to outer variables and nested functions
  • 51. Access to outer variables
  • 52. Nested functions • Functions can be nested one inside another, forming a chain of LexicalEnvironments which can also be called a scope chain
  • 53. Closures • Nested function may continue to live after the outer function has finished:
  • 55. Closure • The inner function keeps a reference to the outer LexicalEnvironment. • The inner function may access variables from it any time even if the outer function is finished. • The browser keeps the LexicalEnvironment and all it’s properties(variables) in memory until there is an inner function which references it. • This is called a closure.
  • 56. [[Scope]] for new Function • There is an exception to general scope binding rule. When you create a function using new Function, it’s[[Scope]] points to window, not to current LexicalEnvironment.
  • 57. Summary • How variables are handled in JavaScript. • How scopes work. • What is a closure and how to use it. • Possible pitfalls and subtles in working with closures. • In JavaScript, a variable can be declared after it has been used (Hoisting). • In other words; a variable can be used before it has been declared (Hoisting).
  • 59. Prototypal inheritance • In JavaScript, the inheritance is prototype- based. That means that there are no classes. Instead, an object inherits from another object
  • 64. Object.create, Object.getPrototypeOf • The __proto__ is a non-standard property, provided by Firefox/Chrome. In other browsers the property still exists internally, but it is hidden
  • 66. The prototype • There is a good and crossbrowser way of setting __proto__. It requires the use of constructor functions.
  • 68. hasOwnProperty • All objects have hasOwnProperty method which allows to check if a property belongs to the object or its prototype.
  • 69. Summary • The inheritance is implemented through a special property __proto__ (named [[Prototype]] in the specification). • When a property is accessed, and the interpreter can’t find it in the object, it follows the __proto__link and searches it there. • The value of this for function properties is set to the object, not its prototype. • Assignment obj.prop = val and deletion delete obj.prop
  • 73. var self = this
  • 74. var self = this