SlideShare a Scribd company logo
1 of 27
 JavaScript Object.
 JavaScript Functions.
 Self-invoking function.
 JavaScript Prototype.
 Advanced JavaScript.
 What is it ?
 JavaScript objects are dynamic.
 You can add a new property at runtime just by assigning it.
 No need to declare data types.
Continued…
Object
Native Objects Host Objects User Defined
Objects
1. String
2. Number
3. Array
4. Date
5. ……
1. Window
2. Document
3. Forms
4. ……
Defined by
Programmer
 There are three object categories in JavaScript.
Ex: var today = new Date();
var name = new String(“DBG”)
Ex: document.getElementById()
 Two common ways to create objects.
Continued…
JavaScript
Objects
Object Literal
Object
Constructor
• An object literal is a comma-separated list of
name-value pairs wrapped in curly braces.
• When a function is called with the ”new” operator,
the function serves as the constructor .
• Internally, JavaScript creates an Object, and then
calls the constructor function.
 Object Literal Vs Object Constructor
Object Literal Object Constructor
Properties and methods are always public. Properties and methods can be private/public.
When a change is made to an Object Literal it
affects that object across the entire script
When a Constructor function is instantiated
and then a change is made to that instance, it
won't affect any other instances of that
object.
function Mobile(){
this.model = "Galaxy Nexus";
this.type = "Android";
this.IMEI = "234034958";
};
var phone = new Mobile();
var phone = {
model: “Galaxy Nexus”,
type: “Android”,
IMEI: “234034958”
};
 functions are objects.
 functions have properties and methods.
 function can be stored in a variable.
 function serves as the constructor of the object; therefore, there is no need to explicitly define a
constructor method.
Continued…
function mobile(){
// your code….
}
 Type of functions :
Continued…
JavaScript
Functions
Named
Functions
Anonymous
Functions
Named Functions (function declaration) Anonymous functions(function operator)
function makeCall(){
alert(“ To make call”);
}
var makeCall= function(){
alert(“ To make call”);
}
Function Declaration
Function Name
Function Body
variable that stores
returned object
Function Operator
Function Body
Named Functions (function declaration) Anonymous functions(function operator)
function makeCall(){
alert(“ To make call”);
}
var makeCall= function(){
alert(“ To make call”);
}
Function Declaration
Function Name
Function Body
variable that stores
returned object
Function Operator
Function Body
 function declarations vs function operators
Named Functions Anonymous Functions
Named functions are created using the
“function declaration”.
Anonymous functions are created using the
“function operator”.
This function object will be created as the
JavaScript is parsed, before the code is run.
Anonymous functions are created at
runtime.
It can be called before the function
statement declaration.
Anonymous functions don’t have a name
 Self-invoking function means function which invokes itself .
 After the function have been initialized it’s immediately invoked.
 We don’t keep reference to that function, not even to it’s return value.
(function(){
// function body
}();
 “Prototype” is a property belonging only to functions.
 The “prototype” allow you to easily define methods to all instances of a particular object.
 Those type of methods is only stored in the memory once, but every instance of the object
can access it.
Continued…
Mobile
default constructor
model & type private
variable
sendSMS() – privileged function
makeCall – using “prototype”
property to create makeCall
function
Mobile.prototype.makeCall =
function(){}
Objects
var phone1 = new Mobile()
var phone2 = new Mobile()
var phone3 = new Mobile()
stored in memory once .
creating separate
functions for each
object.
 The “constructor” property.
 An object has a built-in property named ”constructor”. It is a reference to the function that
created an object.
Continued…
function Mobile () {
/* your code */
}
{
constructor : Mobile
}
Prototype
Autocreated with function
• When “new Mobile()” is called, the “Mobile.prototype” becomes __proto__ and the
constructor becomes accessible from the object.
• The interpreter creates the new function from the declaration(Together with the function,
and its prototype) .
 Chrome watch expression view of a function
 hasOwnProperty
 All objects have hasOwnProperty method which allows to check if a property belongs to the
object or its prototype.
mobile
function – makeCall hasOwnProperty (‘makeCall’)
True
Continued…
Created an object “Phone”
var phone = new Mobile()
 JavaScript OOPs.
Continued…
Class Defines the characteristics of the Object
Object An Instance of a Class.
Property An Object characteristic, such as “IMEI”.
Methods Object capability, such as “makeCall”.
Constructor A method called at the moment of instantiation.
Inheritance A Class can inherit characteristics from another Class.
Encapsulation A Class defines only the characteristics of the Object, a method defines only
how the method executes.
Polymorphism Different Classes might define the same method or property.
 Class
 In JavaScript, “class” can be created by a function.
 When “new” operator is used, a function serves as a constructor for that class.
 Internally, JavaScript creates an “Object”, and then calls the constructor function.
Class
1. default constructor.
2. local & member variable.
3. public/private functions.
Mobile – Sample Class
1. default constructor.
2. Properties, lets say
(a) IMEI
(b) type
(c) model
3. makeCall & sendSMS are the public
functions.
 Public/Private and Privileged functions
 Private function - A function which can be accessed ONLY inside a class.
var functionName = function(){
/* your code */
})
 Public function – A function which is created by “prototype” property, which cannot access any
private functions or variables.
className.prototype.functionName = function(){
/* your code */
})
this.functionName = function(){
/* your code */
})
 Privileged function – A function which is created inside a class using “this” keyword. It can
access private variables and functions.
 Object, Property, Method & Constructor
Mobile – Sample Class
1. default constructor
2. Properties, lets say
(a) model – private variable
(b) IMEI – public variable
(c) type – private variable.
3. makeCall & sendSMS are the common
functionality of any mobile – public functions
Phone (An instance of mobile class)
1. default constructor
2. (a) type & model – Variable cannot be
accessed from outside.
(b) IMEI – This is a “Property” of mobile
class
3. makeCall & sendSMS are the “methods” of
the mobile class.
“new” keyword is using to
initializing mobile class object
Object
 Inheritance (Prototype-based Inheritance)
 JavaScript supports single class inheritance.
 To create a child class that inherit parent class, we create a Parent class object and assign it to the
Child class.
Mobile (Super Class)
1. default constructor
2. model – Member variable
3. makeCall & sendSMS – public
function
// Super Class
function Mobile(){
/* your code */
}
function Android(){
/* your code*/
}
// inheriting all properties of Mobile class
Android.prototype = new Mobile();
// updating the constructor.
Android.prototype.constructor = Android;
Android (Child Class)
1. default constructor.
2. Using “prototype” property to
inherit all properties and methods
from Mobile
 Encapsulation
 Date hiding is protecting the data form accessing it outside the scope.
Mobile
1. model – Private
Variable
2. getModel – Public
function which will return
the value of model.
Phone
phone.model
Result: undefined
phone.getModel()
Result: The value which is stored in the
IMEI variable.
 Polymorphism
 The word Polymorphism in OOPs means having more than one form.
Mobile
Function: InstallApp()
FYI: It can install any kind
of app.
Android
Function: InstallApp()
FYI: It can install only
Android apps
Windows
Function: InstallApp()
FYI: It can install only
Window apps.
 How JavaScript objects work ?
 Functions are first class objects in JavaScript. “Really, just like any other variable”
 Function operator and function declaration.
 Special feature Hoisting .
 Self invoking function.
 Object Oriented Programming in JavaScript.
 http://www.crockford.com/
 http://www.doctrina.org/
 http://www.robertnyman.com/
 http://www.javascriptissexy.com/
 http://www.javascript.info
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June

More Related Content

What's hot

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
zand3rs
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 

What's hot (20)

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
Prototype
PrototypePrototype
Prototype
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About Javascript
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Advanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScriptAdvanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScript
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 

Similar to Understanding Object Oriented Javascript - Coffee@DBG June

Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
Sopheak Sem
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
Ravi Mone
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
pacatarpit
 

Similar to Understanding Object Oriented Javascript - Coffee@DBG June (20)

Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
 
Javascript closures
Javascript closures Javascript closures
Javascript closures
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryLearn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
The prototype property
The prototype propertyThe prototype property
The prototype property
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
 

More from Deepu S Nath

Greetings & Response - English Communication Training
Greetings & Response - English Communication TrainingGreetings & Response - English Communication Training
Greetings & Response - English Communication Training
Deepu S Nath
 

More from Deepu S Nath (20)

Design Thinking, Critical Thinking & Innovation Design
Design Thinking, Critical Thinking & Innovation DesignDesign Thinking, Critical Thinking & Innovation Design
Design Thinking, Critical Thinking & Innovation Design
 
GTECH ATFG µLearn Framework Intro
GTECH ATFG µLearn Framework IntroGTECH ATFG µLearn Framework Intro
GTECH ATFG µLearn Framework Intro
 
Future of learning - Technology Disruption
Future of learning  - Technology DisruptionFuture of learning  - Technology Disruption
Future of learning - Technology Disruption
 
Decentralized Applications using Ethereum
Decentralized Applications using EthereumDecentralized Applications using Ethereum
Decentralized Applications using Ethereum
 
How machines can take decisions
How machines can take decisionsHow machines can take decisions
How machines can take decisions
 
Artificial Intelligence: An Introduction
 Artificial Intelligence: An Introduction Artificial Intelligence: An Introduction
Artificial Intelligence: An Introduction
 
FAYA PORT 80 Introduction
FAYA PORT 80 IntroductionFAYA PORT 80 Introduction
FAYA PORT 80 Introduction
 
How machines can take decisions
How machines can take decisionsHow machines can take decisions
How machines can take decisions
 
Simplified Introduction to AI
Simplified Introduction to AISimplified Introduction to AI
Simplified Introduction to AI
 
Mining Opportunities of Block Chain and BitCoin
Mining Opportunities of Block Chain and BitCoinMining Opportunities of Block Chain and BitCoin
Mining Opportunities of Block Chain and BitCoin
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
 
Coffee@DBG - TechBites March 2016
Coffee@DBG - TechBites March 2016Coffee@DBG - TechBites March 2016
Coffee@DBG - TechBites March 2016
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScript
 
SEO For Developers
SEO For DevelopersSEO For Developers
SEO For Developers
 
Life Cycle of an App - From Idea to Monetization
Life Cycle of an App - From Idea to Monetization  Life Cycle of an App - From Idea to Monetization
Life Cycle of an App - From Idea to Monetization
 
Uncommon Python - What is special in Python
Uncommon Python -  What is special in PythonUncommon Python -  What is special in Python
Uncommon Python - What is special in Python
 
Coffee@DBG - TechBites Sept 2015
Coffee@DBG - TechBites Sept 2015Coffee@DBG - TechBites Sept 2015
Coffee@DBG - TechBites Sept 2015
 
Techbites July 2015
Techbites July 2015Techbites July 2015
Techbites July 2015
 
Apple Watch - Start Your Developer Engine
Apple Watch -  Start Your Developer EngineApple Watch -  Start Your Developer Engine
Apple Watch - Start Your Developer Engine
 
Greetings & Response - English Communication Training
Greetings & Response - English Communication TrainingGreetings & Response - English Communication Training
Greetings & Response - English Communication Training
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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...
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Understanding Object Oriented Javascript - Coffee@DBG June

  • 1.
  • 2.  JavaScript Object.  JavaScript Functions.  Self-invoking function.  JavaScript Prototype.  Advanced JavaScript.
  • 3.
  • 4.  What is it ?  JavaScript objects are dynamic.  You can add a new property at runtime just by assigning it.  No need to declare data types. Continued… Object Native Objects Host Objects User Defined Objects 1. String 2. Number 3. Array 4. Date 5. …… 1. Window 2. Document 3. Forms 4. …… Defined by Programmer  There are three object categories in JavaScript. Ex: var today = new Date(); var name = new String(“DBG”) Ex: document.getElementById()
  • 5.  Two common ways to create objects. Continued… JavaScript Objects Object Literal Object Constructor • An object literal is a comma-separated list of name-value pairs wrapped in curly braces. • When a function is called with the ”new” operator, the function serves as the constructor . • Internally, JavaScript creates an Object, and then calls the constructor function.
  • 6.  Object Literal Vs Object Constructor Object Literal Object Constructor Properties and methods are always public. Properties and methods can be private/public. When a change is made to an Object Literal it affects that object across the entire script When a Constructor function is instantiated and then a change is made to that instance, it won't affect any other instances of that object. function Mobile(){ this.model = "Galaxy Nexus"; this.type = "Android"; this.IMEI = "234034958"; }; var phone = new Mobile(); var phone = { model: “Galaxy Nexus”, type: “Android”, IMEI: “234034958” };
  • 7.
  • 8.  functions are objects.  functions have properties and methods.  function can be stored in a variable.  function serves as the constructor of the object; therefore, there is no need to explicitly define a constructor method. Continued… function mobile(){ // your code…. }
  • 9.  Type of functions : Continued… JavaScript Functions Named Functions Anonymous Functions Named Functions (function declaration) Anonymous functions(function operator) function makeCall(){ alert(“ To make call”); } var makeCall= function(){ alert(“ To make call”); } Function Declaration Function Name Function Body variable that stores returned object Function Operator Function Body
  • 10. Named Functions (function declaration) Anonymous functions(function operator) function makeCall(){ alert(“ To make call”); } var makeCall= function(){ alert(“ To make call”); } Function Declaration Function Name Function Body variable that stores returned object Function Operator Function Body  function declarations vs function operators Named Functions Anonymous Functions Named functions are created using the “function declaration”. Anonymous functions are created using the “function operator”. This function object will be created as the JavaScript is parsed, before the code is run. Anonymous functions are created at runtime. It can be called before the function statement declaration. Anonymous functions don’t have a name
  • 11.
  • 12.  Self-invoking function means function which invokes itself .  After the function have been initialized it’s immediately invoked.  We don’t keep reference to that function, not even to it’s return value. (function(){ // function body }();
  • 13.
  • 14.  “Prototype” is a property belonging only to functions.  The “prototype” allow you to easily define methods to all instances of a particular object.  Those type of methods is only stored in the memory once, but every instance of the object can access it. Continued… Mobile default constructor model & type private variable sendSMS() – privileged function makeCall – using “prototype” property to create makeCall function Mobile.prototype.makeCall = function(){} Objects var phone1 = new Mobile() var phone2 = new Mobile() var phone3 = new Mobile() stored in memory once . creating separate functions for each object.
  • 15.  The “constructor” property.  An object has a built-in property named ”constructor”. It is a reference to the function that created an object. Continued… function Mobile () { /* your code */ } { constructor : Mobile } Prototype Autocreated with function • When “new Mobile()” is called, the “Mobile.prototype” becomes __proto__ and the constructor becomes accessible from the object. • The interpreter creates the new function from the declaration(Together with the function, and its prototype) .
  • 16.  Chrome watch expression view of a function  hasOwnProperty  All objects have hasOwnProperty method which allows to check if a property belongs to the object or its prototype. mobile function – makeCall hasOwnProperty (‘makeCall’) True Continued… Created an object “Phone” var phone = new Mobile()
  • 17.  JavaScript OOPs. Continued… Class Defines the characteristics of the Object Object An Instance of a Class. Property An Object characteristic, such as “IMEI”. Methods Object capability, such as “makeCall”. Constructor A method called at the moment of instantiation. Inheritance A Class can inherit characteristics from another Class. Encapsulation A Class defines only the characteristics of the Object, a method defines only how the method executes. Polymorphism Different Classes might define the same method or property.
  • 18.
  • 19.  Class  In JavaScript, “class” can be created by a function.  When “new” operator is used, a function serves as a constructor for that class.  Internally, JavaScript creates an “Object”, and then calls the constructor function. Class 1. default constructor. 2. local & member variable. 3. public/private functions. Mobile – Sample Class 1. default constructor. 2. Properties, lets say (a) IMEI (b) type (c) model 3. makeCall & sendSMS are the public functions.
  • 20.  Public/Private and Privileged functions  Private function - A function which can be accessed ONLY inside a class. var functionName = function(){ /* your code */ })  Public function – A function which is created by “prototype” property, which cannot access any private functions or variables. className.prototype.functionName = function(){ /* your code */ }) this.functionName = function(){ /* your code */ })  Privileged function – A function which is created inside a class using “this” keyword. It can access private variables and functions.
  • 21.  Object, Property, Method & Constructor Mobile – Sample Class 1. default constructor 2. Properties, lets say (a) model – private variable (b) IMEI – public variable (c) type – private variable. 3. makeCall & sendSMS are the common functionality of any mobile – public functions Phone (An instance of mobile class) 1. default constructor 2. (a) type & model – Variable cannot be accessed from outside. (b) IMEI – This is a “Property” of mobile class 3. makeCall & sendSMS are the “methods” of the mobile class. “new” keyword is using to initializing mobile class object Object
  • 22.  Inheritance (Prototype-based Inheritance)  JavaScript supports single class inheritance.  To create a child class that inherit parent class, we create a Parent class object and assign it to the Child class. Mobile (Super Class) 1. default constructor 2. model – Member variable 3. makeCall & sendSMS – public function // Super Class function Mobile(){ /* your code */ } function Android(){ /* your code*/ } // inheriting all properties of Mobile class Android.prototype = new Mobile(); // updating the constructor. Android.prototype.constructor = Android; Android (Child Class) 1. default constructor. 2. Using “prototype” property to inherit all properties and methods from Mobile
  • 23.  Encapsulation  Date hiding is protecting the data form accessing it outside the scope. Mobile 1. model – Private Variable 2. getModel – Public function which will return the value of model. Phone phone.model Result: undefined phone.getModel() Result: The value which is stored in the IMEI variable.  Polymorphism  The word Polymorphism in OOPs means having more than one form. Mobile Function: InstallApp() FYI: It can install any kind of app. Android Function: InstallApp() FYI: It can install only Android apps Windows Function: InstallApp() FYI: It can install only Window apps.
  • 24.  How JavaScript objects work ?  Functions are first class objects in JavaScript. “Really, just like any other variable”  Function operator and function declaration.  Special feature Hoisting .  Self invoking function.  Object Oriented Programming in JavaScript.
  • 25.  http://www.crockford.com/  http://www.doctrina.org/  http://www.robertnyman.com/  http://www.javascriptissexy.com/  http://www.javascript.info