SlideShare a Scribd company logo
1 of 41
Javascript Objects
--Manish Jangir--
Almost Everything Is Object
In Javascript
I’ll prove it at the end of this presentation
What is an Object?
An object is an unordered list of primitive data types (and sometimes reference
data types) that is stored as a series of name-value pairs. Each item in the list is
called a property (functions are called methods).
Think of the above object is a container that’s holding two name-value pairs in it.
Javascript objects are dynamic that we can create, update and delete more
properties even after its initial definition.
Important: The above example is one way of creating objects. But that’s not
the only way of create an object.
Objects Are Non-Primitive (Reference) Data Types
Object are reference data types that means, they don’t contain the value directly
but the memory reference of the value.
In the above snippet, var first is not containing that object directly. Instead the
object is first stored in memory and that memory reference was given to first. So if
we copy the first to second var, the reference was copied not the value.
Any changes in var second, The will be reflected on var first too.
Ways Of Creating Javascript Objects
There are 3 ways to create Javascript Objects:
1. Object Literal - ({})
2. Object Constructor - (new Object())
3. Function Constructor - (new FunctionName())
Object Literal - {}
The most common and the easiest way to create objects is with the object literal:
Object Constructor - new Object()
The second most common way to create objects is with Object constructor. A
constructor is a function used for initializing new objects. Use the new keyword to
call the constructor.
Function Constructor - new FunctionName()
For simple objects that may only be used once for e.g. storing your application
configuration etc, the previous two methods are sufficient. But what if you want to
create more than one object with similar features. A function constructor is just a
simple javascript function that is able to create multiple objects when called with
new operator.
Function Constructor Example
Don’t Return Anything Non-Primitive
It is highly recommended that you don’t return anything non-primitive data type
from the function constructor otherwise the instance will lose its prototypes.
In the above example, we are returning an another object, so our newly created
instance will be converted into that. There is no issue in returning any string,
boolean, number, null or undefined because they are primitive data types.
Don’t Forget To Put “new” Keyword
If you don’t or forget to put the “new” keyword while creating an object instance,
your constructor function will behave just like a simple and pure function. For e.g.
In the above example, we are not creating the object rather simple calling a pure
function. The function did not return anything, so myBook will be undefined here
and so myBook.name is. Infact we are indirectly modifying the global variables
here as the meaning of “this” in the function is window.
Setting and Getting Object Properties
Javascript Objects properties can be set in 3 different ways. Each way has its own
advantage:
1. Dot Notation
2. Bracket Notation
3. Object.defineProperty (Used rarely but very beneficial)
Dot Notation
In the previous examples, we have used the dot notation to assign the properties
to the object. Although dot notation is very fast but too limited as you can only
assign the property names that a valid identifier or string that start from _, $ or
alphabets.
Bracket Notation
Bracket notation is not much faster than dot notation but it accepts anything as a
key name. If you pass a key in quotes, that is taken as it is. But whatever you put
without any quotes as a key name, the engine will try to get the string value of key
first. Its most useful in case of dynamic property names.
Object.defineProperty() (Used in Rare Cases)
Before moving on this, you must have to know the javascript object property
descriptors. As we saw earlier that objects are non-primitive and mutuable data
types so anyone can change the property values anywhere in the code.
What if you don’t want to let anybody to change the value of a particular property
once it is defined?? There are 5 basic property descriptors:
1. Enumerable
2. Writable
3. Configurable
4. Get
5. Set
Object.defineProperty() - Enumerable
Enumereble properties are those which are hidden in a for...loop, JSON.stringify and Object.keys.
Object.defineProperty() - Writable
It helps us to decide whether the property can be modified or not once the object has been defined.
Practical example : You can never change the value of Math.PI in your code. It will always be 3.14
Object.defineProperty() - Configurable
Configure whether the property can be deleted (Using delete keyword) from object or not.
Object.defineProperty() - Configurable
Configure whether the property can be deleted (Using delete keyword) from object or not.
Everything Is Object in JS
Let’s see some example that will prove that how almost every line of your daily JS code contains an
object.
Continued...
Continued...
Native Objects Provided By Browser
There are a lot of native objects provided by the browser and all the data types and sub-types are just
instances of these native objects. Some of them are listed here:
1. Object
2. Function
3. String
4. Number
5. Boolean
6. Date
7. Regxr
8. Error
9. Math
And the list goes on….
Practice
There are a lot of native objects provided by the browser and all the data types and sub-types are just
instances of these native objects. Some of them are listed here:
1. Object
2. Function
3. String
4. Number
5. Boolean
6. Date
7. Regxr
8. Error
9. Math
And the list goes on….
A Typical Example Of Object
“this”
The Most Frustrating Keyword In Javascript Ever
What will be the output of the following code?
Let’s Make It Extremely Simple
To Know “this” Value, Never Look At Function Body
If you are trying to find the value of “this” in a function by seeing its definition again
and again, you won’t ever find it.
Because the value of “this” is absolutely dynamic and changes at different places.
What we have to do to find “this” is look at HOW THE HELL THE FUNCTION
HAS BEEN CALLED
A Javascript Function Is Usually Called In 4 Ways
1. Direct Invocation ( getFullName() )
2. Invoke as an Object Method ( obj.getFullName() )
3. Invoke using .call function prototype ( getFullName.call(null, 2, 3) )
4. Invoke using .apply function prototype ( getFullName.call(null, [2,3]) )
Let's discuss about each one.
Direct Invocation
When a function is called simply by parenthesis, it is called direct invocation and
the value of this will be always global object (“Window” in browser and “Global” in
Node.js). In strict mode, it will be undefined.
Invoke as Object Method
When a method (or function) is called as a property of any object, then the value
of “this” will always be the caller object.
In the left example, we called two object
methods. The most immediate object which is
calling the functions will be “this” there.
For example, in first call, obj is calling getName
so the value of this will be obj.
But in the second call, obj.info is calling the
function, so the “this” will be obj.info there.
Output?
Returned Inner Function (Closure)
.call Method To Invoke A Function
What if you want to override or use your own object as “this” for function? Then
you will have to call a function manually using .call or .apply function prototypes.
In .call the first argument will always be your context object (object that you want
to put at the place of this) and rest of the parameters are your comma separated
function params that you pass in a normal function.
Example:
.apply Method To Invoke A Function
Call and Apply both are absolutely identical to each other but the difference is, in
.call the function params are passed comma separated and in .apply the function
params are passed as an array. See the example:
.bind To Bind A Context To A Function
Bind is a very useful prototype method in JS world. The enables you to bind a
function the particular context and then call it anywhere anyhow, It’s “this” will not
change anyway. Bind always provides a new function not the reference.
Thank You

More Related Content

What's hot

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQueryBobby Bryant
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascriptrelay12
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
JavaScript Beyond jQuery (Jfokus 2013)
JavaScript Beyond jQuery (Jfokus 2013)JavaScript Beyond jQuery (Jfokus 2013)
JavaScript Beyond jQuery (Jfokus 2013)johnwilander
 
JavaScript Best Pratices
JavaScript Best PraticesJavaScript Best Pratices
JavaScript Best PraticesChengHui Weng
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic JavascriptBunlong Van
 

What's hot (19)

Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
JavaScript Beyond jQuery (Jfokus 2013)
JavaScript Beyond jQuery (Jfokus 2013)JavaScript Beyond jQuery (Jfokus 2013)
JavaScript Beyond jQuery (Jfokus 2013)
 
JavaScript Best Pratices
JavaScript Best PraticesJavaScript Best Pratices
JavaScript Best Pratices
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 

Similar to Javascript Objects Deep Dive

Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureManish Jangir
 
Javascript closures
Javascript closures Javascript closures
Javascript closures VNG
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneDeepu S Nath
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About JavascriptManish Jangir
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript WorkshopPamela Fox
 
C questions
C questionsC questions
C questionsparm112
 
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 2Abbas Ajmal
 
GDSC NED Frontend Bootcamp Session 1.pptx
GDSC NED Frontend Bootcamp Session 1.pptxGDSC NED Frontend Bootcamp Session 1.pptx
GDSC NED Frontend Bootcamp Session 1.pptxEhtesham46
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS ImplimentationUsman Mehmood
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfSreeVani74
 

Similar to Javascript Objects Deep Dive (20)

Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Javascript closures
Javascript closures Javascript closures
Javascript closures
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 
About Python
About PythonAbout Python
About Python
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About Javascript
 
Java scriptforjavadev part2a
Java scriptforjavadev part2aJava scriptforjavadev part2a
Java scriptforjavadev part2a
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
C questions
C questionsC questions
C questions
 
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
 
GDSC NED Frontend Bootcamp Session 1.pptx
GDSC NED Frontend Bootcamp Session 1.pptxGDSC NED Frontend Bootcamp Session 1.pptx
GDSC NED Frontend Bootcamp Session 1.pptx
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Doc abap
Doc abapDoc abap
Doc abap
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 

Recently uploaded

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 Nanonetsnaman860154
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
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 MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
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 MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
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 SolutionsEnterprise Knowledge
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Recently uploaded (20)

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
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
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
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
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
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
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
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

Javascript Objects Deep Dive

  • 2. Almost Everything Is Object In Javascript I’ll prove it at the end of this presentation
  • 3. What is an Object? An object is an unordered list of primitive data types (and sometimes reference data types) that is stored as a series of name-value pairs. Each item in the list is called a property (functions are called methods). Think of the above object is a container that’s holding two name-value pairs in it. Javascript objects are dynamic that we can create, update and delete more properties even after its initial definition. Important: The above example is one way of creating objects. But that’s not the only way of create an object.
  • 4. Objects Are Non-Primitive (Reference) Data Types Object are reference data types that means, they don’t contain the value directly but the memory reference of the value. In the above snippet, var first is not containing that object directly. Instead the object is first stored in memory and that memory reference was given to first. So if we copy the first to second var, the reference was copied not the value. Any changes in var second, The will be reflected on var first too.
  • 5. Ways Of Creating Javascript Objects There are 3 ways to create Javascript Objects: 1. Object Literal - ({}) 2. Object Constructor - (new Object()) 3. Function Constructor - (new FunctionName())
  • 6. Object Literal - {} The most common and the easiest way to create objects is with the object literal:
  • 7. Object Constructor - new Object() The second most common way to create objects is with Object constructor. A constructor is a function used for initializing new objects. Use the new keyword to call the constructor.
  • 8. Function Constructor - new FunctionName() For simple objects that may only be used once for e.g. storing your application configuration etc, the previous two methods are sufficient. But what if you want to create more than one object with similar features. A function constructor is just a simple javascript function that is able to create multiple objects when called with new operator.
  • 10. Don’t Return Anything Non-Primitive It is highly recommended that you don’t return anything non-primitive data type from the function constructor otherwise the instance will lose its prototypes. In the above example, we are returning an another object, so our newly created instance will be converted into that. There is no issue in returning any string, boolean, number, null or undefined because they are primitive data types.
  • 11. Don’t Forget To Put “new” Keyword If you don’t or forget to put the “new” keyword while creating an object instance, your constructor function will behave just like a simple and pure function. For e.g. In the above example, we are not creating the object rather simple calling a pure function. The function did not return anything, so myBook will be undefined here and so myBook.name is. Infact we are indirectly modifying the global variables here as the meaning of “this” in the function is window.
  • 12. Setting and Getting Object Properties Javascript Objects properties can be set in 3 different ways. Each way has its own advantage: 1. Dot Notation 2. Bracket Notation 3. Object.defineProperty (Used rarely but very beneficial)
  • 13. Dot Notation In the previous examples, we have used the dot notation to assign the properties to the object. Although dot notation is very fast but too limited as you can only assign the property names that a valid identifier or string that start from _, $ or alphabets.
  • 14. Bracket Notation Bracket notation is not much faster than dot notation but it accepts anything as a key name. If you pass a key in quotes, that is taken as it is. But whatever you put without any quotes as a key name, the engine will try to get the string value of key first. Its most useful in case of dynamic property names.
  • 15. Object.defineProperty() (Used in Rare Cases) Before moving on this, you must have to know the javascript object property descriptors. As we saw earlier that objects are non-primitive and mutuable data types so anyone can change the property values anywhere in the code. What if you don’t want to let anybody to change the value of a particular property once it is defined?? There are 5 basic property descriptors: 1. Enumerable 2. Writable 3. Configurable 4. Get 5. Set
  • 16. Object.defineProperty() - Enumerable Enumereble properties are those which are hidden in a for...loop, JSON.stringify and Object.keys.
  • 17. Object.defineProperty() - Writable It helps us to decide whether the property can be modified or not once the object has been defined. Practical example : You can never change the value of Math.PI in your code. It will always be 3.14
  • 18. Object.defineProperty() - Configurable Configure whether the property can be deleted (Using delete keyword) from object or not.
  • 19. Object.defineProperty() - Configurable Configure whether the property can be deleted (Using delete keyword) from object or not.
  • 20. Everything Is Object in JS Let’s see some example that will prove that how almost every line of your daily JS code contains an object.
  • 23. Native Objects Provided By Browser There are a lot of native objects provided by the browser and all the data types and sub-types are just instances of these native objects. Some of them are listed here: 1. Object 2. Function 3. String 4. Number 5. Boolean 6. Date 7. Regxr 8. Error 9. Math And the list goes on….
  • 24. Practice There are a lot of native objects provided by the browser and all the data types and sub-types are just instances of these native objects. Some of them are listed here: 1. Object 2. Function 3. String 4. Number 5. Boolean 6. Date 7. Regxr 8. Error 9. Math And the list goes on….
  • 25. A Typical Example Of Object
  • 26. “this” The Most Frustrating Keyword In Javascript Ever
  • 27. What will be the output of the following code?
  • 28. Let’s Make It Extremely Simple
  • 29. To Know “this” Value, Never Look At Function Body If you are trying to find the value of “this” in a function by seeing its definition again and again, you won’t ever find it. Because the value of “this” is absolutely dynamic and changes at different places. What we have to do to find “this” is look at HOW THE HELL THE FUNCTION HAS BEEN CALLED
  • 30. A Javascript Function Is Usually Called In 4 Ways 1. Direct Invocation ( getFullName() ) 2. Invoke as an Object Method ( obj.getFullName() ) 3. Invoke using .call function prototype ( getFullName.call(null, 2, 3) ) 4. Invoke using .apply function prototype ( getFullName.call(null, [2,3]) ) Let's discuss about each one.
  • 31. Direct Invocation When a function is called simply by parenthesis, it is called direct invocation and the value of this will be always global object (“Window” in browser and “Global” in Node.js). In strict mode, it will be undefined.
  • 32. Invoke as Object Method When a method (or function) is called as a property of any object, then the value of “this” will always be the caller object.
  • 33. In the left example, we called two object methods. The most immediate object which is calling the functions will be “this” there. For example, in first call, obj is calling getName so the value of this will be obj. But in the second call, obj.info is calling the function, so the “this” will be obj.info there.
  • 34.
  • 35.
  • 38. .call Method To Invoke A Function What if you want to override or use your own object as “this” for function? Then you will have to call a function manually using .call or .apply function prototypes. In .call the first argument will always be your context object (object that you want to put at the place of this) and rest of the parameters are your comma separated function params that you pass in a normal function. Example:
  • 39. .apply Method To Invoke A Function Call and Apply both are absolutely identical to each other but the difference is, in .call the function params are passed comma separated and in .apply the function params are passed as an array. See the example:
  • 40. .bind To Bind A Context To A Function Bind is a very useful prototype method in JS world. The enables you to bind a function the particular context and then call it anywhere anyhow, It’s “this” will not change anyway. Bind always provides a new function not the reference.