SlideShare a Scribd company logo
Advanced
Presented by
Humayun Kayesh
Advanced JavaScript
● ‘this’
● Closure
● Prototype
● Module pattern
Agenda
‘this’
‘this’
● A variable with the value of the object that
invokes the function where this is used.
● Refers to an object; that is, the subject in
context
● Not assigned a value until an object invokes the
function where this is defined
How is the Value of ‘this’ Determined?
The value of ‘this’, is based on the context in which the
function is called at runtime.
Confused?!! Let’s see an example...
var foo = 'foo';
var myObject = {
foo: 'I am myObject.foo'
};
var sayFoo = function() {
console.log(this.foo);
};
// give myObject a sayFoo property and have it point to sayFoo function
myObject.sayFoo = sayFoo;
myObject.sayFoo(); // logs 'I am myObject.foo'
sayFoo(); // logs 'foo'
How is the Value of ‘this’ Determined?
‘this’ Inside Nested Functions
Q: What happens to ‘this’ when it is used inside of a
function that is contained inside of another function?
Ans: ‘this’ loses its way and refers to the head object
(window object in browsers), instead of the object within
which the function is defined.
Solution Hint: Using scope to keep track of function context
var myObject = {
myProperty:'I can see the light',
myMethod:function() {
console.log(this.myProperty); //logs 'I can see the light'
var helperFunction = function() {
console.log(this.myProperty); //logs 'Undefined'
}();
}
}
myObject.myMethod(); // invoke myMethod
‘this’ Inside Nested Functions
Closure
Closure
● A Closure is the local variables for a function - kept alive
after the function has returned
● A function having access to the parent scope, even
after the parent function has closed.
● The closure has three scope chains:
o has access to its own scope
o has access to the outer function’s variables and
o has access to the global variables
Example
Example
Practical example
Prototype
Prototype
● A prototype is a collection of properties and
methods that can be automatically attached to
an object when it is created
● Every JavaScript function has a prototype
property
● Used primarily for inheritance
Prototype Code Example
PictureProtoType = {
getSize: function(){ return this.height * this.width; },
fetchThumbnail:function(){ /*...*/ }
};
function Picture(name, height, width){
this.name = name;
this.height = height;
this.width = width;
}
Picture.prototype = PictureProtoType;
var picture = new Picture('MyPhoto.jpg', 600, 750);
picture.getSize();
Prototype Lookups are Dynamic
● You can add properties to the prototype of an
object at any time
● The prototype chain lookup will find the new
property as expected
See example...
Prototype Lookups are Dynamic
PictureProtoType = {
getSize: function(){
return this.height * this.width;
},
fetchThumbnail:function(){ /*...*/ }
};
function Picture(name, height, width){
this.name = name;
this.height = height;
this.width = width;
}
Picture.prototype = PictureProtoType;
var picture = new Picture('MyPhoto.jpg', 600,
750);
picture.getSize();
PictureProtoType.getBlackAndWhite = function()
{
/* code... */
}
picture.getBlackAndWhite();
Extending Native Objects
var keywords = [
"landscape", "tranquil", "green", "vegetation"
] ;
console.log(keywords);
Array.prototype.clear=function(){ this.length=0; }
keywords.clear();
console.log(keywords);
JavaScript Module Pattern
Creating a module
(function () {
// code...
})();
It declares a function, which then calls itself
immediately.
Basic Module
var Module = (function () {
var privateMethod = function () {
// do something
};
})();
Public Method
var Module = (function () {
return {
publicMethod: function () {
// code
}
};
})();
Module.publicMethod();
Anonymous Object Literal Return
var Module = (function () {
var privateMethod = function () {};
return {
publicMethodOne: function () { // I can call `privateMethod()` you know... },
publicMethodtwo: function () { //Code… },
publicMethodThree: function () { // Code… }
};
})();
Revealing Module Pattern
var Module = (function () {
var privateMethod = function () {
// private
};
var someMethod = function () {
// public
};
var anotherMethod = function () {
// public
};
return {
someMethod: someMethod,
anotherMethod: anotherMethod
};
})();
Extending A Module
var Module = (function () {
var privateMethod = function () {
// private
};
var someMethod = function () {
// public
};
var anotherMethod = function () {
// public
};
return {
someMethod: someMethod,
anotherMethod: anotherMethod
};
})();
var ModuleTwo = (function (Module) {
Module.extension = function () {
// another method!
};
return Module;
})(Module || {});
Advantages
● Cleaner approach for developers
● Supports private data
● Less clutter in the global namespace
● Localization of functions and variables through
closures
Q&A…

More Related Content

PPT
Javascript arrays
Hassan Dar
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PDF
JavaScript Programming
Sehwan Noh
 
PDF
Javascript Module Patterns
Nicholas Jansma
 
PPTX
Spring Framework
tola99
 
Javascript arrays
Hassan Dar
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
jQuery for beginners
Arulmurugan Rajaraman
 
JavaScript Promises
L&T Technology Services Limited
 
JavaScript Programming
Sehwan Noh
 
Javascript Module Patterns
Nicholas Jansma
 
Spring Framework
tola99
 

What's hot (20)

PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPTX
Java script
Abhishek Kesharwani
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPT
Javascript
Manav Prasad
 
PPT
Advanced Javascript
Adieu
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PPTX
Express js
Manav Prasad
 
PDF
TypeScript Best Practices
felixbillon
 
PDF
Introduction to java (revised)
Sujit Majety
 
PPTX
Introduction to Angularjs
Manish Shekhawat
 
PPTX
Angularjs PPT
Amit Baghel
 
PPTX
Angular 9
Raja Vishnu
 
PPTX
An Introduction To REST API
Aniruddh Bhilvare
 
PDF
Javascript
Momentum Design Lab
 
PPTX
jQuery PPT
Dominic Arrojado
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPT
Introduction to the Web API
Brad Genereaux
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PPTX
jQuery
Jay Poojara
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Basics of JavaScript
Bala Narayanan
 
Java script
Abhishek Kesharwani
 
Introduction to Javascript
Amit Tyagi
 
Javascript
Manav Prasad
 
Advanced Javascript
Adieu
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
Express js
Manav Prasad
 
TypeScript Best Practices
felixbillon
 
Introduction to java (revised)
Sujit Majety
 
Introduction to Angularjs
Manish Shekhawat
 
Angularjs PPT
Amit Baghel
 
Angular 9
Raja Vishnu
 
An Introduction To REST API
Aniruddh Bhilvare
 
jQuery PPT
Dominic Arrojado
 
Javascript variables and datatypes
Varun C M
 
Introduction to the Web API
Brad Genereaux
 
TypeScript - An Introduction
NexThoughts Technologies
 
jQuery
Jay Poojara
 
Ad

Viewers also liked (15)

PPTX
Advanced JavaScript Concepts
Naresh Kumar
 
PDF
Advanced JavaScript Techniques
Dmitry Baranovskiy
 
PDF
JavaScript: Past, Present, Future
Jungryul Choi
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PDF
Advanced Object-Oriented JavaScript
ecker
 
PPTX
Introduction to JavaScript Programming
Collaboration Technologies
 
PPTX
OOPS in javascript
Vijaya Anand
 
PDF
Google Maps
techugo
 
PPTX
Google maps
Fernando Quilca Charaja
 
PDF
Google Maps API for Android
Maksim Golivkin
 
PPT
Google Maps
Natalia Urrego Ospina
 
PPTX
Google maps street view power point presentation
mireya921
 
PPTX
Google Maps
gp3project
 
PPT
Google Maps Presentation
David Kamerer
 
PPTX
Web page concept final ppt
Sukanya Sen Sharma
 
Advanced JavaScript Concepts
Naresh Kumar
 
Advanced JavaScript Techniques
Dmitry Baranovskiy
 
JavaScript: Past, Present, Future
Jungryul Choi
 
Advanced JavaScript
Stoyan Stefanov
 
Advanced Object-Oriented JavaScript
ecker
 
Introduction to JavaScript Programming
Collaboration Technologies
 
OOPS in javascript
Vijaya Anand
 
Google Maps
techugo
 
Google Maps API for Android
Maksim Golivkin
 
Google maps street view power point presentation
mireya921
 
Google Maps
gp3project
 
Google Maps Presentation
David Kamerer
 
Web page concept final ppt
Sukanya Sen Sharma
 
Ad

Similar to Advanced JavaScript (20)

PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Javascript Design Patterns
Subramanyan Murali
 
PDF
Javascript basic course
Tran Khoa
 
PDF
Advanced javascript
Doeun KOCH
 
KEY
Javascript tid-bits
David Atchley
 
PPTX
Ajaxworld
deannalagason
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PDF
The mighty js_function
timotheeg
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PPT
Javascript quiz. Questions to ask when recruiting developers.
Alberto Naranjo
 
PDF
Uncommon Design Patterns
Stefano Fago
 
PPTX
Ian 20150116 java script oop
LearningTech
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
relay12
 
PPTX
Object Oriented JavaScript
Julie Iskander
 
PDF
Say It With Javascript
Giovanni Scerra ☃
 
PDF
[2015/2016] JavaScript
Ivano Malavolta
 
PPTX
Advance JS and oop
Abuzer Firdousi
 
PDF
Converting your JS library to a jQuery plugin
thehoagie
 
Object Oriented JavaScript
Donald Sipe
 
Javascript Design Patterns
Subramanyan Murali
 
Javascript basic course
Tran Khoa
 
Advanced javascript
Doeun KOCH
 
Javascript tid-bits
David Atchley
 
Ajaxworld
deannalagason
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
The mighty js_function
timotheeg
 
Object Oriented Programming in JavaScript
zand3rs
 
Javascript quiz. Questions to ask when recruiting developers.
Alberto Naranjo
 
Uncommon Design Patterns
Stefano Fago
 
Ian 20150116 java script oop
LearningTech
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
relay12
 
Object Oriented JavaScript
Julie Iskander
 
Say It With Javascript
Giovanni Scerra ☃
 
[2015/2016] JavaScript
Ivano Malavolta
 
Advance JS and oop
Abuzer Firdousi
 
Converting your JS library to a jQuery plugin
thehoagie
 

More from Nascenia IT (20)

PPTX
Exploring DeepSeek A Hands-On Dive & How to Adapt the AI Surge.pptx
Nascenia IT
 
PPTX
AI Tools for Productivity: Exploring Prompt Engineering and Key Features
Nascenia IT
 
PDF
Introduction to basic data analytics tools
Nascenia IT
 
PPTX
Communication workshop in nascenia
Nascenia IT
 
PPTX
The Art of Statistical Deception
Nascenia IT
 
PDF
করোনায় কী করি!
Nascenia IT
 
PPTX
GDPR compliance expectations from the development team
Nascenia IT
 
PPTX
Writing Clean Code
Nascenia IT
 
PPTX
History & Introduction of Neural Network and use of it in Computer Vision
Nascenia IT
 
PPTX
Ruby on Rails: Coding Guideline
Nascenia IT
 
PPTX
iphone 11 new features
Nascenia IT
 
PPTX
Software quality assurance and cyber security
Nascenia IT
 
PPTX
Job Market Scenario For Freshers
Nascenia IT
 
PPTX
Modern Frontend Technologies (BEM, Retina)
Nascenia IT
 
PPTX
CSS for Developers
Nascenia IT
 
PPTX
Big commerce app development
Nascenia IT
 
PPTX
Integrating QuickBooks Desktop with Rails Application
Nascenia IT
 
PPTX
Shopify
Nascenia IT
 
PPTX
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
PPTX
Clean code
Nascenia IT
 
Exploring DeepSeek A Hands-On Dive & How to Adapt the AI Surge.pptx
Nascenia IT
 
AI Tools for Productivity: Exploring Prompt Engineering and Key Features
Nascenia IT
 
Introduction to basic data analytics tools
Nascenia IT
 
Communication workshop in nascenia
Nascenia IT
 
The Art of Statistical Deception
Nascenia IT
 
করোনায় কী করি!
Nascenia IT
 
GDPR compliance expectations from the development team
Nascenia IT
 
Writing Clean Code
Nascenia IT
 
History & Introduction of Neural Network and use of it in Computer Vision
Nascenia IT
 
Ruby on Rails: Coding Guideline
Nascenia IT
 
iphone 11 new features
Nascenia IT
 
Software quality assurance and cyber security
Nascenia IT
 
Job Market Scenario For Freshers
Nascenia IT
 
Modern Frontend Technologies (BEM, Retina)
Nascenia IT
 
CSS for Developers
Nascenia IT
 
Big commerce app development
Nascenia IT
 
Integrating QuickBooks Desktop with Rails Application
Nascenia IT
 
Shopify
Nascenia IT
 
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
Clean code
Nascenia IT
 

Recently uploaded (20)

PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
PPT
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
DOCX
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Rise With SAP partner in Mumbai.........
pts464036
 
PPTX
Audio Editing and it's techniques in computer graphics.pptx
fosterbayirinia3
 
PPTX
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
10 Hidden App Development Costs That Can Sink Your Startup.pptx
Lunar Web Solution
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Rise With SAP partner in Mumbai.........
pts464036
 
Audio Editing and it's techniques in computer graphics.pptx
fosterbayirinia3
 
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
10 Hidden App Development Costs That Can Sink Your Startup.pptx
Lunar Web Solution
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Advanced JavaScript

  • 3. ● ‘this’ ● Closure ● Prototype ● Module pattern Agenda
  • 5. ‘this’ ● A variable with the value of the object that invokes the function where this is used. ● Refers to an object; that is, the subject in context ● Not assigned a value until an object invokes the function where this is defined
  • 6. How is the Value of ‘this’ Determined? The value of ‘this’, is based on the context in which the function is called at runtime. Confused?!! Let’s see an example...
  • 7. var foo = 'foo'; var myObject = { foo: 'I am myObject.foo' }; var sayFoo = function() { console.log(this.foo); }; // give myObject a sayFoo property and have it point to sayFoo function myObject.sayFoo = sayFoo; myObject.sayFoo(); // logs 'I am myObject.foo' sayFoo(); // logs 'foo' How is the Value of ‘this’ Determined?
  • 8. ‘this’ Inside Nested Functions Q: What happens to ‘this’ when it is used inside of a function that is contained inside of another function? Ans: ‘this’ loses its way and refers to the head object (window object in browsers), instead of the object within which the function is defined. Solution Hint: Using scope to keep track of function context
  • 9. var myObject = { myProperty:'I can see the light', myMethod:function() { console.log(this.myProperty); //logs 'I can see the light' var helperFunction = function() { console.log(this.myProperty); //logs 'Undefined' }(); } } myObject.myMethod(); // invoke myMethod ‘this’ Inside Nested Functions
  • 11. Closure ● A Closure is the local variables for a function - kept alive after the function has returned ● A function having access to the parent scope, even after the parent function has closed. ● The closure has three scope chains: o has access to its own scope o has access to the outer function’s variables and o has access to the global variables
  • 16. Prototype ● A prototype is a collection of properties and methods that can be automatically attached to an object when it is created ● Every JavaScript function has a prototype property ● Used primarily for inheritance
  • 17. Prototype Code Example PictureProtoType = { getSize: function(){ return this.height * this.width; }, fetchThumbnail:function(){ /*...*/ } }; function Picture(name, height, width){ this.name = name; this.height = height; this.width = width; } Picture.prototype = PictureProtoType; var picture = new Picture('MyPhoto.jpg', 600, 750); picture.getSize();
  • 18. Prototype Lookups are Dynamic ● You can add properties to the prototype of an object at any time ● The prototype chain lookup will find the new property as expected See example...
  • 19. Prototype Lookups are Dynamic PictureProtoType = { getSize: function(){ return this.height * this.width; }, fetchThumbnail:function(){ /*...*/ } }; function Picture(name, height, width){ this.name = name; this.height = height; this.width = width; } Picture.prototype = PictureProtoType; var picture = new Picture('MyPhoto.jpg', 600, 750); picture.getSize(); PictureProtoType.getBlackAndWhite = function() { /* code... */ } picture.getBlackAndWhite();
  • 20. Extending Native Objects var keywords = [ "landscape", "tranquil", "green", "vegetation" ] ; console.log(keywords); Array.prototype.clear=function(){ this.length=0; } keywords.clear(); console.log(keywords);
  • 22. Creating a module (function () { // code... })(); It declares a function, which then calls itself immediately.
  • 23. Basic Module var Module = (function () { var privateMethod = function () { // do something }; })();
  • 24. Public Method var Module = (function () { return { publicMethod: function () { // code } }; })(); Module.publicMethod();
  • 25. Anonymous Object Literal Return var Module = (function () { var privateMethod = function () {}; return { publicMethodOne: function () { // I can call `privateMethod()` you know... }, publicMethodtwo: function () { //Code… }, publicMethodThree: function () { // Code… } }; })();
  • 26. Revealing Module Pattern var Module = (function () { var privateMethod = function () { // private }; var someMethod = function () { // public }; var anotherMethod = function () { // public }; return { someMethod: someMethod, anotherMethod: anotherMethod }; })();
  • 27. Extending A Module var Module = (function () { var privateMethod = function () { // private }; var someMethod = function () { // public }; var anotherMethod = function () { // public }; return { someMethod: someMethod, anotherMethod: anotherMethod }; })(); var ModuleTwo = (function (Module) { Module.extension = function () { // another method! }; return Module; })(Module || {});
  • 28. Advantages ● Cleaner approach for developers ● Supports private data ● Less clutter in the global namespace ● Localization of functions and variables through closures

Editor's Notes

  • #6: http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/
  • #7: http://code.tutsplus.com/tutorials/fully-understanding-the-codethiscode-keyword--net-21117
  • #12: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
  • #17: A prototype is a collection of properties and methods that can be automatically attached to an object when it is created. http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/ https://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
  • #23: http://toddmotto.com/mastering-the-module-pattern/