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.pdfsannykhopade
 
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
 
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.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
 
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

JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
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
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
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
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
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
 
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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 

Recently uploaded (20)

JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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...
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
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
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 
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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 

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?