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

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

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?