SlideShare a Scribd company logo
1 of 15
Advanced JavaScript Techniques
@hoatle – eXo Platform
Agenda

OOP (Object Oriented Programming)

Scope

Closure

Context (this, arguments...)
OOP

Object = data structure = properties + methods
var myPresent = {
slides: 30,
currentSlide: 17,
previousSlide: function() {
this.current--;
},
nextSlide: function() {
this.current++;
}
}
myPresent.slides; //30
myPresent.currentSlide; //17
myPresent.previousSlide();
myPresent.currentSlide; //16
myPresent.nextSlide();
OOP
3 primary goals of oop:

Encapsulation

Polymorphism

Inheritance
Encapsulation:

Hiding properties (private):
myPresent.slides; //not available
myPresent.currentSlide; //not available

Accessing by methods only:
myPresent.getSlides();
myPresent.getCurrentSlide();
myPresent.nextSlide();
myPresent.previousSlide();
Polymorphism
Ability of one type, A, to appear as and be used
like another type, B
function getPresent(type) {
if (type === 'special') {
return new SpecialPresent();
} else if (type === 'normal')) {
return new NormalPresent();
} else {
throw new Error('type for Present is not specified');
}
}
var mySpecialPresent = getPresent('special');
var myNormalPresent = getPresent('normal');
methods can be called: getSlides(), getCurrentSlide(); previous(); next();
Inheritance
code reuse (sub class)
var Present = Class.extend({
init: function() {
this.slides = 30;
this.currentSlide = 17;
},
getSlides: function() {
return this.slides;
},
getCurrentSlide: function() {
return this.currentSlide;
},
previous: function() {
this.currentSlide--;
},
next: function() {
this.currentSlide++;
}
});
var SpecialPresent = Present.extend({
init: function() {
this._super();
this.specialPresent = 21;
},
getSpecialPresent: function() {
return this.specialPresent;
}
});
var specialPresent = new SpecialPresent();
specialPresent.getSlides(); //30
specialPresent.getSpecialPresent(); //21
speicalPresent.next();
specialPresent.getCurrentSlide(); //18
Simple inheritance by John Resig
Psedoclassical vs Prototypal school
function Present() {
var slides = 30,
currentSlide = 17;
this.getSlides = function() {
return slides;
}
this.getCurrentSlide = function() {
return currentSlide;
}
this.setCurrentSlide = function(i) {
currentSlide = i;
}
}
Present.prototype.previous = function() {
this.setCurrentSlide((this.getCurrentSlide())--);
}
Present.prototype.next = function() {
this.setCurrentSlide((this.getCurrentSlide())++);
}
var myPresent = new Present();
myPresent.next();
myPresent.getSlides();
Psedoclassical vs Prototypal school
var Present = (function() {
var slides = 30,
currentSlide = 17;
return {
getSlides: function() {
return slides;
},
getCurrentSlide: function() {
return currentSlide;
},
previous: function() {
currentSlide--;
},
nextd: function() {
currentSlide++;
}
};
})();
function clone(obj) {
var F = function() {};
F.prototype = obj;
return new F();
}
var myPresent = clone(Present);
myPresent.next();
myPresent.getSlides();
Psedoclassical vs Prototypal school

Google Closure lib

Not really classical
util js version 2?

Raphaël

truly natural js: object
prototypal inheritance
“I have been writing JavaScript for 8 years now, and I have 
never once found need to use an uber function. The super idea 
is fairly important in the classical pattern, but it appears to be 
unnecessary in the prototypal and functional patterns. I now 
see my early attempts to support the classical model in 
JavaScript as a mistake.” ­ Douglas Crockford
Scope

Avoid global scope

Use namespace

Use function wrapper
Avoid global scope
function test() {
i = 3;
}
test();
alert(i) //3
function test() {
var i = 3;
}
test();
alert(i); //undefined
function Present() {
}
//what if other lib also define Present ??function?
(function() {
function Present() {
//implement here
}
//expose
window.mylib = window.mylib || {};
window.mylib.Present = Present;
})();
Closure

Inner function can access outer function and
variable after the outer function executed

Function scope
Context

this

arguments
Thanks for your attention!
Questions?

More Related Content

What's hot

Tarea De Scilab By Sebastian Vasquez
Tarea De Scilab By Sebastian VasquezTarea De Scilab By Sebastian Vasquez
Tarea De Scilab By Sebastian VasquezSebastian Vasquez
 
What is python
What is pythonWhat is python
What is pythonEU Edge
 
software-vulnerability-detectionPresentation
software-vulnerability-detectionPresentationsoftware-vulnerability-detectionPresentation
software-vulnerability-detectionPresentationClaude Goubet
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab CompletoRicardo Grandas
 
Robot Motion Source code
Robot Motion Source codeRobot Motion Source code
Robot Motion Source codeBrian Goggins
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานknang
 

What's hot (9)

Tarea De Scilab By Sebastian Vasquez
Tarea De Scilab By Sebastian VasquezTarea De Scilab By Sebastian Vasquez
Tarea De Scilab By Sebastian Vasquez
 
What is python
What is pythonWhat is python
What is python
 
software-vulnerability-detectionPresentation
software-vulnerability-detectionPresentationsoftware-vulnerability-detectionPresentation
software-vulnerability-detectionPresentation
 
Reactive x
Reactive xReactive x
Reactive x
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab Completo
 
Taller De Scilab
Taller De ScilabTaller De Scilab
Taller De Scilab
 
Robot Motion Source code
Robot Motion Source codeRobot Motion Source code
Robot Motion Source code
 
Trabajo Scilab
Trabajo ScilabTrabajo Scilab
Trabajo Scilab
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 

Viewers also liked

How Craigslist Works
How Craigslist WorksHow Craigslist Works
How Craigslist Worksguest511afe
 
Barchart.com Relaunch
Barchart.com RelaunchBarchart.com Relaunch
Barchart.com Relaunchnzurek
 
პრეზენტაცია
პრეზენტაციაპრეზენტაცია
პრეზენტაციაguestf61bbbc
 
Being john malkovich no vi
Being john malkovich no viBeing john malkovich no vi
Being john malkovich no viKa Hui
 
Penerjemahan Teks Teknologi Informasi
Penerjemahan Teks Teknologi InformasiPenerjemahan Teks Teknologi Informasi
Penerjemahan Teks Teknologi InformasiBahtera
 
One week job india album 28 jobs 28 weeks 28 states - jubanashwa mishra
One week job india album   28 jobs 28 weeks 28 states - jubanashwa mishraOne week job india album   28 jobs 28 weeks 28 states - jubanashwa mishra
One week job india album 28 jobs 28 weeks 28 states - jubanashwa mishraJubanashwa Mishra
 
SheSpeaks 2014 Predictions Study
SheSpeaks 2014 Predictions StudySheSpeaks 2014 Predictions Study
SheSpeaks 2014 Predictions StudySheSpeaks Inc.
 
Tools for a whole range of Scholarly Activities (at DH2015)
Tools for a whole range of Scholarly Activities (at DH2015)Tools for a whole range of Scholarly Activities (at DH2015)
Tools for a whole range of Scholarly Activities (at DH2015)John Bradley
 
Data Science in Cardiac Sciences
Data Science in Cardiac SciencesData Science in Cardiac Sciences
Data Science in Cardiac SciencesRobert Chen
 
Why to get started with 3 d printing in
Why to get started with 3 d printing inWhy to get started with 3 d printing in
Why to get started with 3 d printing inMemorial University
 

Viewers also liked (20)

Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Tobacco Cessation: Accept The Challenge
Tobacco Cessation: Accept The ChallengeTobacco Cessation: Accept The Challenge
Tobacco Cessation: Accept The Challenge
 
How Craigslist Works
How Craigslist WorksHow Craigslist Works
How Craigslist Works
 
Barchart.com Relaunch
Barchart.com RelaunchBarchart.com Relaunch
Barchart.com Relaunch
 
პრეზენტაცია
პრეზენტაციაპრეზენტაცია
პრეზენტაცია
 
Amazonda
AmazondaAmazonda
Amazonda
 
Being john malkovich no vi
Being john malkovich no viBeing john malkovich no vi
Being john malkovich no vi
 
Penerjemahan Teks Teknologi Informasi
Penerjemahan Teks Teknologi InformasiPenerjemahan Teks Teknologi Informasi
Penerjemahan Teks Teknologi Informasi
 
One week job india
One week job indiaOne week job india
One week job india
 
весна
веснавесна
весна
 
Scotia report oct 12
Scotia report oct 12Scotia report oct 12
Scotia report oct 12
 
PhD project
PhD projectPhD project
PhD project
 
Tech presentation
Tech presentationTech presentation
Tech presentation
 
One week job india album 28 jobs 28 weeks 28 states - jubanashwa mishra
One week job india album   28 jobs 28 weeks 28 states - jubanashwa mishraOne week job india album   28 jobs 28 weeks 28 states - jubanashwa mishra
One week job india album 28 jobs 28 weeks 28 states - jubanashwa mishra
 
SheSpeaks 2014 Predictions Study
SheSpeaks 2014 Predictions StudySheSpeaks 2014 Predictions Study
SheSpeaks 2014 Predictions Study
 
Tools for a whole range of Scholarly Activities (at DH2015)
Tools for a whole range of Scholarly Activities (at DH2015)Tools for a whole range of Scholarly Activities (at DH2015)
Tools for a whole range of Scholarly Activities (at DH2015)
 
Data Science in Cardiac Sciences
Data Science in Cardiac SciencesData Science in Cardiac Sciences
Data Science in Cardiac Sciences
 
Why to get started with 3 d printing in
Why to get started with 3 d printing inWhy to get started with 3 d printing in
Why to get started with 3 d printing in
 
RPforEUH2031
RPforEUH2031RPforEUH2031
RPforEUH2031
 
Shabnam
ShabnamShabnam
Shabnam
 

Similar to Advanced Javascript Techniques

Engineering JavaScript
Engineering JavaScriptEngineering JavaScript
Engineering JavaScriptJim Purbrick
 
Joose - JavaScript Meta Object System
Joose - JavaScript Meta Object SystemJoose - JavaScript Meta Object System
Joose - JavaScript Meta Object Systemmalteubl
 
Joose @jsconf
Joose @jsconfJoose @jsconf
Joose @jsconfmalteubl
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxAtikur Rahman
 
Lazy Loading Because I'm Lazy
Lazy Loading Because I'm LazyLazy Loading Because I'm Lazy
Lazy Loading Because I'm LazyJohnathan Leppert
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24Joachim Bengtsson
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injectionSteve Ng
 
Beyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS AppsBeyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS AppsRebecca Murphey
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfakankshasorate1
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfbhagyashri686896
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfHarshuPawar4
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfsannykhopade
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the HoodVladik Khononov
 
The Canvas API for Rubyists
The Canvas API for RubyistsThe Canvas API for Rubyists
The Canvas API for Rubyistsdeanhudson
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 

Similar to Advanced Javascript Techniques (20)

Engineering JavaScript
Engineering JavaScriptEngineering JavaScript
Engineering JavaScript
 
Joose - JavaScript Meta Object System
Joose - JavaScript Meta Object SystemJoose - JavaScript Meta Object System
Joose - JavaScript Meta Object System
 
Joose @jsconf
Joose @jsconfJoose @jsconf
Joose @jsconf
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScript
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
Lazy Loading Because I'm Lazy
Lazy Loading Because I'm LazyLazy Loading Because I'm Lazy
Lazy Loading Because I'm Lazy
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24
Nevyn — Promise, It's Async! Swift Language User Group Lightning Talk 2015-09-24
 
Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
 
Beyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS AppsBeyond the DOM: Sane Structure for JS Apps
Beyond the DOM: Sane Structure for JS Apps
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
 
The Canvas API for Rubyists
The Canvas API for RubyistsThe Canvas API for Rubyists
The Canvas API for Rubyists
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
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.pdfsudhanshuwaghmare1
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
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, Adobeapidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
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 FMESafe Software
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 

Advanced Javascript Techniques

  • 2. Agenda  OOP (Object Oriented Programming)  Scope  Closure  Context (this, arguments...)
  • 3. OOP  Object = data structure = properties + methods var myPresent = { slides: 30, currentSlide: 17, previousSlide: function() { this.current--; }, nextSlide: function() { this.current++; } } myPresent.slides; //30 myPresent.currentSlide; //17 myPresent.previousSlide(); myPresent.currentSlide; //16 myPresent.nextSlide();
  • 4. OOP 3 primary goals of oop:  Encapsulation  Polymorphism  Inheritance
  • 5. Encapsulation:  Hiding properties (private): myPresent.slides; //not available myPresent.currentSlide; //not available  Accessing by methods only: myPresent.getSlides(); myPresent.getCurrentSlide(); myPresent.nextSlide(); myPresent.previousSlide();
  • 6. Polymorphism Ability of one type, A, to appear as and be used like another type, B function getPresent(type) { if (type === 'special') { return new SpecialPresent(); } else if (type === 'normal')) { return new NormalPresent(); } else { throw new Error('type for Present is not specified'); } } var mySpecialPresent = getPresent('special'); var myNormalPresent = getPresent('normal'); methods can be called: getSlides(), getCurrentSlide(); previous(); next();
  • 7. Inheritance code reuse (sub class) var Present = Class.extend({ init: function() { this.slides = 30; this.currentSlide = 17; }, getSlides: function() { return this.slides; }, getCurrentSlide: function() { return this.currentSlide; }, previous: function() { this.currentSlide--; }, next: function() { this.currentSlide++; } }); var SpecialPresent = Present.extend({ init: function() { this._super(); this.specialPresent = 21; }, getSpecialPresent: function() { return this.specialPresent; } }); var specialPresent = new SpecialPresent(); specialPresent.getSlides(); //30 specialPresent.getSpecialPresent(); //21 speicalPresent.next(); specialPresent.getCurrentSlide(); //18 Simple inheritance by John Resig
  • 8. Psedoclassical vs Prototypal school function Present() { var slides = 30, currentSlide = 17; this.getSlides = function() { return slides; } this.getCurrentSlide = function() { return currentSlide; } this.setCurrentSlide = function(i) { currentSlide = i; } } Present.prototype.previous = function() { this.setCurrentSlide((this.getCurrentSlide())--); } Present.prototype.next = function() { this.setCurrentSlide((this.getCurrentSlide())++); } var myPresent = new Present(); myPresent.next(); myPresent.getSlides();
  • 9. Psedoclassical vs Prototypal school var Present = (function() { var slides = 30, currentSlide = 17; return { getSlides: function() { return slides; }, getCurrentSlide: function() { return currentSlide; }, previous: function() { currentSlide--; }, nextd: function() { currentSlide++; } }; })(); function clone(obj) { var F = function() {}; F.prototype = obj; return new F(); } var myPresent = clone(Present); myPresent.next(); myPresent.getSlides();
  • 10. Psedoclassical vs Prototypal school  Google Closure lib  Not really classical util js version 2?  Raphaël  truly natural js: object prototypal inheritance “I have been writing JavaScript for 8 years now, and I have  never once found need to use an uber function. The super idea  is fairly important in the classical pattern, but it appears to be  unnecessary in the prototypal and functional patterns. I now  see my early attempts to support the classical model in  JavaScript as a mistake.” ­ Douglas Crockford
  • 11. Scope  Avoid global scope  Use namespace  Use function wrapper
  • 12. Avoid global scope function test() { i = 3; } test(); alert(i) //3 function test() { var i = 3; } test(); alert(i); //undefined function Present() { } //what if other lib also define Present ??function? (function() { function Present() { //implement here } //expose window.mylib = window.mylib || {}; window.mylib.Present = Present; })();
  • 13. Closure  Inner function can access outer function and variable after the outer function executed  Function scope
  • 15. Thanks for your attention! Questions?