SlideShare una empresa de Scribd logo
1 de 20
Object Oriented
JavaScript
An Introduction
- Manoj Nama
Object Oriented
JavaScript
An Introduction
- Manoj Nama
Agenda
● Functions
● Objects
● Prototypal Inheritance
● Callbacks & Closures
● Async Programming
● Some Exercises
Functions
● Functions are first class members of JavaScript
● They can be used just like variables
function someFunction(arg1, arg2) {
return arg1 + arg2;
}
Functions
● JavaScript has Function scope, i.e only Functions define
the scope and nothing else
● A function has access to all the variables and methods in
the scope in which it is defined
Example
Call & Apply
● Call/Apply both are used to call a function with the
ability to change the this reference
● Only difference between the two is syntax
○ Call takes arguments as a list
functionName.call(obj, arg1, arg2);
○ Apply takes an array of Arguments
functionName.apply(obj, [arg1, arg2]);
Example
Objects
● In JavaScript almost everything is an Object
● Multiple ways to create an Object
○ Object Constructor var obj = new Object()
○ Object Literal var obj = {}
○ Inbuilt Method var obj = Object.create()
○ Constructor function var obj = new Person()
Example
Constructor Function
● Constructor function is similar to the notation of a Class
function Person(name, age) {
this.name = name;
this.age = age;
}
Example
Prototypes
● Objects inheriting from other Objects
● Prototype is an object used to construct new objects
● we can assign properties to prototypes to inherit them
Prototypes are used with Constructor Functions
Prototypal Chain
● All Objects inherit from Object class
● If certain property is not available on current object, it is
looked on prototype, then Parent’s prototype and so on …
until the property or null is found
o → o.prototype → … → Object.prototype → null
Inheritance
● Inheriting properties and methods
● Prototypes are used for inheritance
● Two ways
○ Inherit from Constructor Functions (Class)
○ Inherit from another Objects
Example
Callbacks
● Callbacks are basically functions passed on as arguments
to another operation
● This allows us to cope with Asynchronous nature of
JavaScript
● We don’t have to block the browser for results
Example
Closures
● Very important due to Async nature of JavaScript
● Closures are basically functions which capture the
surrounding scope
● The variables remain bound to the scope even when the
initiator returns
Example
Async Programming
● Callbacks really help in maintaining the sanity in
Asynchronous operations
● But, what if there are huge no of async operations
depending on each other, nested inside each other..
● This is referred to as Callback hell..
Callback Hell
asyncOp1(function(result) {
asyncOp2(function(result1) {
asyncOp3(function(result2) {
...
asyncOp1476(function(result3) {
//phew! got my result
});
});
});
});
Async Flow Control
● Callback hell can be avoided by controlling the program
flow
● Async.JS is an excellent library to control the callback flow
● Promises Pattern can be very useful with Async
Operations
Async Flow Control Example
async.parallel([
function(callback) {
callback(null, “data”);
},
function(callback) {
callback(null, “data2”);
}
],
//optional callback
function(err, results) {
//results: [“data”, “data2”]
});
async.waterfall([
function(callback) {
callback(null, “data”);
},
function(arg1, callback) {
//arg1: “data”
callback(null, “data2”);
}
],
//optional callback
function(err, result) {
//result: “data2”
});
Tips & Tricks
● use + to convert expressions to a number
○ +new Date() gives Timestamp
● use !! to convert any expression to a boolean
● Append array to another array
○ a = [1,2,3]; b= [4,5,6]
○ Array.prototype.push.apply(a,b)
Exercises
• Add a loop() method to the Prototype of Array
• Implement basic Inheritance with an example of Employee
• print numbers 1..5, such that every number is printed
after 500ms
Links to Examples
• Functions: http://jsfiddle.net/manoj_nama/GE59y/
• Call & Apply: http://jsfiddle.net/manoj_nama/6tLx5/1/
• Objects: http://jsfiddle.net/manoj_nama/Ma9EQ/
• Constructor Function:
http://jsfiddle.net/manoj_nama/3Ly4V/1/
• Inheritance: http://jsfiddle.net/manoj_nama/YLUHw/1/
• Callbacks: http://jsfiddle.net/manoj_nama/9vqEf/1/
• Closures: http://jsfiddle.net/manoj_nama/H6nE2/1/
• Promises: http://jsfiddle.net/manoj_nama/R6ZV2/

Más contenido relacionado

La actualidad más candente

TAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptTAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptValeri Karpov
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with javaLiviu Tudor
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2Chris Farrell
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQueryBobby Bryant
 
Modern Java Features
Modern Java Features Modern Java Features
Modern Java Features Florian Hopf
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2matifch
 
Building Reactive System with Akka
Building  Reactive System with AkkaBuilding  Reactive System with Akka
Building Reactive System with AkkaLam Nguyen
 
Reactive cocoa 101
Reactive cocoa 101Reactive cocoa 101
Reactive cocoa 101Hai Feng Kao
 
Callable and runnable objects in ruby
Callable and runnable objects in rubyCallable and runnable objects in ruby
Callable and runnable objects in rubyRahul Bajaj
 
Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?Kamil Kucharski
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for IntermediatesAnkit Agrawal
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartChen Fisher
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81Isaac Liao
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loopsthirumurugan133
 

La actualidad más candente (20)

TAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptTAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScript
 
oojs
oojsoojs
oojs
 
Closures
ClosuresClosures
Closures
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Modern Java Features
Modern Java Features Modern Java Features
Modern Java Features
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2
 
Building Reactive System with Akka
Building  Reactive System with AkkaBuilding  Reactive System with Akka
Building Reactive System with Akka
 
Reactive cocoa 101
Reactive cocoa 101Reactive cocoa 101
Reactive cocoa 101
 
Callable and runnable objects in ruby
Callable and runnable objects in rubyCallable and runnable objects in ruby
Callable and runnable objects in ruby
 
Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
MCRL2
MCRL2MCRL2
MCRL2
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
 
Storage class
Storage classStorage class
Storage class
 
FRP
FRPFRP
FRP
 
Welcome to rx java2
Welcome to rx java2Welcome to rx java2
Welcome to rx java2
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loops
 

Destacado

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Why history web 1
Why history web 1Why history web 1
Why history web 1ccb21
 
Presentation to WUG on promoting social media in state government (updated)
Presentation to WUG on promoting social media in state government (updated)Presentation to WUG on promoting social media in state government (updated)
Presentation to WUG on promoting social media in state government (updated)Rob Quigley
 
Network Layered Models Rroosend
Network Layered Models RroosendNetwork Layered Models Rroosend
Network Layered Models Rroosendrroosend
 
FUKUYAMA BASE WORKSHOP vol05 theme
FUKUYAMA BASE WORKSHOP vol05 themeFUKUYAMA BASE WORKSHOP vol05 theme
FUKUYAMA BASE WORKSHOP vol05 themenoteproject
 
Computer class thingy
Computer class thingyComputer class thingy
Computer class thingyjshrable
 
C:\fakepath\統一發票真偉大─林 蘭
C:\fakepath\統一發票真偉大─林   蘭C:\fakepath\統一發票真偉大─林   蘭
C:\fakepath\統一發票真偉大─林 蘭linda lin
 
User interview wahl_upa
User interview wahl_upaUser interview wahl_upa
User interview wahl_upaKatherine Wahl
 
ECM with SharePoint - SPSOzarks
ECM with SharePoint - SPSOzarksECM with SharePoint - SPSOzarks
ECM with SharePoint - SPSOzarksAndrew Parmenter
 
Year stuff
Year stuffYear stuff
Year stuffLauren
 
Behavioural Marketing. Connecting the big 4 of web, email, social & mobile
Behavioural Marketing.  Connecting the big 4 of web, email, social & mobile Behavioural Marketing.  Connecting the big 4 of web, email, social & mobile
Behavioural Marketing. Connecting the big 4 of web, email, social & mobile John Watton
 
Practicing Agile in Offshore Environment
Practicing Agile in Offshore EnvironmentPracticing Agile in Offshore Environment
Practicing Agile in Offshore EnvironmentTO THE NEW | Technology
 
Lavish Magazine by Frances
Lavish Magazine by FrancesLavish Magazine by Frances
Lavish Magazine by Francesfrannymarie
 
dgsghsjkldhçgs
dgsghsjkldhçgsdgsghsjkldhçgs
dgsghsjkldhçgscephas3
 
2013: What to Expect and How to Succeed in the Year of the Buyer
2013: What to Expect and How to Succeed in the Year of the Buyer2013: What to Expect and How to Succeed in the Year of the Buyer
2013: What to Expect and How to Succeed in the Year of the BuyerJohn Watton
 

Destacado (20)

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Why history web 1
Why history web 1Why history web 1
Why history web 1
 
Presentation to WUG on promoting social media in state government (updated)
Presentation to WUG on promoting social media in state government (updated)Presentation to WUG on promoting social media in state government (updated)
Presentation to WUG on promoting social media in state government (updated)
 
Network Layered Models Rroosend
Network Layered Models RroosendNetwork Layered Models Rroosend
Network Layered Models Rroosend
 
FUKUYAMA BASE WORKSHOP vol05 theme
FUKUYAMA BASE WORKSHOP vol05 themeFUKUYAMA BASE WORKSHOP vol05 theme
FUKUYAMA BASE WORKSHOP vol05 theme
 
Computer class thingy
Computer class thingyComputer class thingy
Computer class thingy
 
Advanced guide to Quartz plugin
Advanced guide to Quartz pluginAdvanced guide to Quartz plugin
Advanced guide to Quartz plugin
 
Test on tabloids
Test on tabloidsTest on tabloids
Test on tabloids
 
4 planning section
4 planning section4 planning section
4 planning section
 
C:\fakepath\統一發票真偉大─林 蘭
C:\fakepath\統一發票真偉大─林   蘭C:\fakepath\統一發票真偉大─林   蘭
C:\fakepath\統一發票真偉大─林 蘭
 
User interview wahl_upa
User interview wahl_upaUser interview wahl_upa
User interview wahl_upa
 
ECM with SharePoint - SPSOzarks
ECM with SharePoint - SPSOzarksECM with SharePoint - SPSOzarks
ECM with SharePoint - SPSOzarks
 
R0003
R0003R0003
R0003
 
Year stuff
Year stuffYear stuff
Year stuff
 
Behavioural Marketing. Connecting the big 4 of web, email, social & mobile
Behavioural Marketing.  Connecting the big 4 of web, email, social & mobile Behavioural Marketing.  Connecting the big 4 of web, email, social & mobile
Behavioural Marketing. Connecting the big 4 of web, email, social & mobile
 
Practicing Agile in Offshore Environment
Practicing Agile in Offshore EnvironmentPracticing Agile in Offshore Environment
Practicing Agile in Offshore Environment
 
Lavish Magazine by Frances
Lavish Magazine by FrancesLavish Magazine by Frances
Lavish Magazine by Frances
 
Data Binding in Grails
Data Binding in GrailsData Binding in Grails
Data Binding in Grails
 
dgsghsjkldhçgs
dgsghsjkldhçgsdgsghsjkldhçgs
dgsghsjkldhçgs
 
2013: What to Expect and How to Succeed in the Year of the Buyer
2013: What to Expect and How to Succeed in the Year of the Buyer2013: What to Expect and How to Succeed in the Year of the Buyer
2013: What to Expect and How to Succeed in the Year of the Buyer
 

Similar a An introduction to Object Oriented JavaScript

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_scriptVijay Kalyan
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classesVijay Kalyan
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to FunctionsCharles Russell
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced FunctionsWebStackAcademy
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014Thomas Lockney
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageChobodiDamsaraniPadm
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 
Functional programming
Functional programmingFunctional programming
Functional programmingijcd
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
Java 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen ColebourneJava 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen ColebourneJAXLondon_Conference
 
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...funcs, func expressions, closure, returning funcs, recursion, the stack -goph...
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...sangam biradar
 

Similar a An introduction to Object Oriented JavaScript (20)

Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014
 
Scala qq
Scala qqScala qq
Scala qq
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C Language
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen ColebourneJava 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen Colebourne
 
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...funcs, func expressions, closure, returning funcs, recursion, the stack -goph...
funcs, func expressions, closure, returning funcs, recursion, the stack -goph...
 

Más de TO THE NEW | Technology

10 Best Node.js Practices you Need to Know!
10 Best Node.js Practices you Need to Know!10 Best Node.js Practices you Need to Know!
10 Best Node.js Practices you Need to Know!TO THE NEW | Technology
 
10 Pragmatic UX techniques for building smarter products:
10 Pragmatic UX techniques for building smarter products:10 Pragmatic UX techniques for building smarter products:
10 Pragmatic UX techniques for building smarter products:TO THE NEW | Technology
 
12 Key points which make Swift more effective than Objective C
12 Key points which make Swift more effective than Objective C12 Key points which make Swift more effective than Objective C
12 Key points which make Swift more effective than Objective CTO THE NEW | Technology
 
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape SoftwareTO THE NEW | Technology
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlTO THE NEW | Technology
 
BigData Search Simplified with ElasticSearch
BigData Search Simplified with ElasticSearchBigData Search Simplified with ElasticSearch
BigData Search Simplified with ElasticSearchTO THE NEW | Technology
 

Más de TO THE NEW | Technology (20)

10 Best Node.js Practices you Need to Know!
10 Best Node.js Practices you Need to Know!10 Best Node.js Practices you Need to Know!
10 Best Node.js Practices you Need to Know!
 
10 Pragmatic UX techniques for building smarter products:
10 Pragmatic UX techniques for building smarter products:10 Pragmatic UX techniques for building smarter products:
10 Pragmatic UX techniques for building smarter products:
 
12 Key points which make Swift more effective than Objective C
12 Key points which make Swift more effective than Objective C12 Key points which make Swift more effective than Objective C
12 Key points which make Swift more effective than Objective C
 
Gulp - The Streaming Build System
Gulp - The Streaming Build SystemGulp - The Streaming Build System
Gulp - The Streaming Build System
 
Grails Spring Boot
Grails Spring BootGrails Spring Boot
Grails Spring Boot
 
AWS Elastic Beanstalk
AWS Elastic BeanstalkAWS Elastic Beanstalk
AWS Elastic Beanstalk
 
Content migration to AEM
Content migration to AEMContent migration to AEM
Content migration to AEM
 
AWS CodeDeploy
AWS CodeDeployAWS CodeDeploy
AWS CodeDeploy
 
Big Data Expertise
Big Data ExpertiseBig Data Expertise
Big Data Expertise
 
Object Oriented JavaScript - II
Object Oriented JavaScript - IIObject Oriented JavaScript - II
Object Oriented JavaScript - II
 
MongoDb and NoSQL
MongoDb and NoSQLMongoDb and NoSQL
MongoDb and NoSQL
 
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software
(AWS) Auto Scaling : Evening Session by Amazon and IntelliGrape Software
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
 
Cloud Formation
Cloud FormationCloud Formation
Cloud Formation
 
BigData Search Simplified with ElasticSearch
BigData Search Simplified with ElasticSearchBigData Search Simplified with ElasticSearch
BigData Search Simplified with ElasticSearch
 
JULY IN GRAILS
JULY IN GRAILSJULY IN GRAILS
JULY IN GRAILS
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Getting groovier-with-vertx
Getting groovier-with-vertxGetting groovier-with-vertx
Getting groovier-with-vertx
 
Introduction to Kanban
Introduction to KanbanIntroduction to Kanban
Introduction to Kanban
 
Introduction to Heroku
Introduction to HerokuIntroduction to Heroku
Introduction to Heroku
 

Último

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 

Último (20)

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 

An introduction to Object Oriented JavaScript

  • 3. Agenda ● Functions ● Objects ● Prototypal Inheritance ● Callbacks & Closures ● Async Programming ● Some Exercises
  • 4. Functions ● Functions are first class members of JavaScript ● They can be used just like variables function someFunction(arg1, arg2) { return arg1 + arg2; }
  • 5. Functions ● JavaScript has Function scope, i.e only Functions define the scope and nothing else ● A function has access to all the variables and methods in the scope in which it is defined Example
  • 6. Call & Apply ● Call/Apply both are used to call a function with the ability to change the this reference ● Only difference between the two is syntax ○ Call takes arguments as a list functionName.call(obj, arg1, arg2); ○ Apply takes an array of Arguments functionName.apply(obj, [arg1, arg2]); Example
  • 7. Objects ● In JavaScript almost everything is an Object ● Multiple ways to create an Object ○ Object Constructor var obj = new Object() ○ Object Literal var obj = {} ○ Inbuilt Method var obj = Object.create() ○ Constructor function var obj = new Person() Example
  • 8. Constructor Function ● Constructor function is similar to the notation of a Class function Person(name, age) { this.name = name; this.age = age; } Example
  • 9. Prototypes ● Objects inheriting from other Objects ● Prototype is an object used to construct new objects ● we can assign properties to prototypes to inherit them Prototypes are used with Constructor Functions
  • 10. Prototypal Chain ● All Objects inherit from Object class ● If certain property is not available on current object, it is looked on prototype, then Parent’s prototype and so on … until the property or null is found o → o.prototype → … → Object.prototype → null
  • 11. Inheritance ● Inheriting properties and methods ● Prototypes are used for inheritance ● Two ways ○ Inherit from Constructor Functions (Class) ○ Inherit from another Objects Example
  • 12. Callbacks ● Callbacks are basically functions passed on as arguments to another operation ● This allows us to cope with Asynchronous nature of JavaScript ● We don’t have to block the browser for results Example
  • 13. Closures ● Very important due to Async nature of JavaScript ● Closures are basically functions which capture the surrounding scope ● The variables remain bound to the scope even when the initiator returns Example
  • 14. Async Programming ● Callbacks really help in maintaining the sanity in Asynchronous operations ● But, what if there are huge no of async operations depending on each other, nested inside each other.. ● This is referred to as Callback hell..
  • 15. Callback Hell asyncOp1(function(result) { asyncOp2(function(result1) { asyncOp3(function(result2) { ... asyncOp1476(function(result3) { //phew! got my result }); }); }); });
  • 16. Async Flow Control ● Callback hell can be avoided by controlling the program flow ● Async.JS is an excellent library to control the callback flow ● Promises Pattern can be very useful with Async Operations
  • 17. Async Flow Control Example async.parallel([ function(callback) { callback(null, “data”); }, function(callback) { callback(null, “data2”); } ], //optional callback function(err, results) { //results: [“data”, “data2”] }); async.waterfall([ function(callback) { callback(null, “data”); }, function(arg1, callback) { //arg1: “data” callback(null, “data2”); } ], //optional callback function(err, result) { //result: “data2” });
  • 18. Tips & Tricks ● use + to convert expressions to a number ○ +new Date() gives Timestamp ● use !! to convert any expression to a boolean ● Append array to another array ○ a = [1,2,3]; b= [4,5,6] ○ Array.prototype.push.apply(a,b)
  • 19. Exercises • Add a loop() method to the Prototype of Array • Implement basic Inheritance with an example of Employee • print numbers 1..5, such that every number is printed after 500ms
  • 20. Links to Examples • Functions: http://jsfiddle.net/manoj_nama/GE59y/ • Call & Apply: http://jsfiddle.net/manoj_nama/6tLx5/1/ • Objects: http://jsfiddle.net/manoj_nama/Ma9EQ/ • Constructor Function: http://jsfiddle.net/manoj_nama/3Ly4V/1/ • Inheritance: http://jsfiddle.net/manoj_nama/YLUHw/1/ • Callbacks: http://jsfiddle.net/manoj_nama/9vqEf/1/ • Closures: http://jsfiddle.net/manoj_nama/H6nE2/1/ • Promises: http://jsfiddle.net/manoj_nama/R6ZV2/