SlideShare une entreprise Scribd logo
1  sur  20
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
●

●

Example

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
Call & Apply
●

Example

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]);
Objects
●

●

Example

In JavaScript almost everything is an
Object
Multiple ways to create an Object
○

○

○

○

Object Constructor var
Object Literal
var
Inbuilt Method
var
Constructor function var

obj
obj
obj
obj

=
=
=
=

new Object()
{}
Object.create()
new Person()
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
●

●

o

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.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
●

●

●

Example

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
Closures
●

●

●

Example

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
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
async.parallel([
function(callback) {
callback(null, “data”);
},
function(callback) {
callback(null, “data2”);
}
],
//optional callback
function(err, results) {
//results: [“data”,
“data2”]
});

Example

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/
Thank You !
Manoj Nama
@manoj_nama

Contenu connexe

Tendances

Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript
Dhananjay Kumar
 

Tendances (20)

Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
oojs
oojsoojs
oojs
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
JavaScript objects and functions
JavaScript objects and functionsJavaScript objects and functions
JavaScript objects and functions
 
JavaScript Data Types
JavaScript Data TypesJavaScript Data Types
JavaScript Data Types
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript
 
Running Through Typescript
Running Through TypescriptRunning Through Typescript
Running Through Typescript
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Ruby
RubyRuby
Ruby
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Lightning talk: Kotlin
Lightning talk: KotlinLightning talk: Kotlin
Lightning talk: Kotlin
 
Scala on-android
Scala on-androidScala on-android
Scala on-android
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...
 

Similaire à Introduction to Object Oriented Javascript

Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
Alex Teut
 
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
Thomas Lockney
 

Similaire à Introduction to Object Oriented Javascript (20)

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
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
 
Javascript internals
Javascript internalsJavascript internals
Javascript internals
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C Language
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
 
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
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
 
Scala qq
Scala qqScala qq
Scala qq
 
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...
 
RxSwift
RxSwiftRxSwift
RxSwift
 
Parallel Programming In Java
Parallel Programming In JavaParallel Programming In Java
Parallel Programming In Java
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8
 
Hello Java 8
Hello Java 8Hello Java 8
Hello Java 8
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Introduction to Object Oriented Javascript

  • 1. Object Oriented JavaScript An Introduction Manoj Nama
  • 3. Functions ● ● Functions are first class members of JavaScript They can be used just like variables function someFunction(arg1, arg2) { return arg1 + arg2; }
  • 4. Functions ● ● Example 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
  • 5. Call & Apply ● Example 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]);
  • 6. Objects ● ● Example In JavaScript almost everything is an Object Multiple ways to create an Object ○ ○ ○ ○ Object Constructor var Object Literal var Inbuilt Method var Constructor function var obj obj obj obj = = = = new Object() {} Object.create() new Person()
  • 7. Constructor Function ● Constructor function is similar to the notation of a Class function Person(name, age) { this.name = name; this.age = age; } Example
  • 8. 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
  • 9. Prototypal Chain ● ● o 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.prototype → … → Object.prototype → null
  • 10. Inheritance ● ● ● Inheriting properties and methods Prototypes are used for inheritance Two ways ○ ○ Inherit from Constructor Functions (Class) Inherit from another Objects Example
  • 11. Callbacks ● ● ● Example 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
  • 12. Closures ● ● ● Example 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
  • 13. 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..
  • 14. Callback Hell asyncOp1(function(result) { asyncOp2(function(result1) { asyncOp3(function(result2) { ... asyncOp1476(function(result3) { //phew! got my result }); }); }); });
  • 15. 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
  • 16. Async Flow Control async.parallel([ function(callback) { callback(null, “data”); }, function(callback) { callback(null, “data2”); } ], //optional callback function(err, results) { //results: [“data”, “data2”] }); Example async.waterfall([ function(callback) { callback(null, “data”); }, function(arg1, callback) { //arg1: “data” callback(null, “data2”); } ], //optional callback function(err, result) { //result: “data2” });
  • 17. 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)
  • 18. 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
  • 19. 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/
  • 20. Thank You ! Manoj Nama @manoj_nama