SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
JavaScript OOP
Maxis Kao @ Yahoo Search
Frontend 101
JavaScript
JavaScript
Java
OOP
Classical OOP
➔ Object-oriented programming
➔ C++, Java, Python
➔ Prototype-based programming
➔ Prototypical inheritance
in JavaScript,
before we start
Class
// define a new class called Pokemon with an empty constructor
var Pokemon = function () {};
Defines the object's characteristics.
A class is a template definition of an object's properties and methods.
Object
// create two instances, mew and pikachu
var mew = new Pokemon();
var pikachu = new Pokemon();
An instance of a class.
Property
An object characteristic, such as name.
// define the name property for the Pokemon class
var Pokemon = function (pokemonName) {
this.name = pokemonName;
};
Method
An object capability, such as walk. It is a subroutine or function
associated with a class.
// define the method bark() for the Pokemon class
Pokemon.prototype.bark = function () {
console.log("Hey, I'm " + this.name);
};
Question 1.
Please use JavaScript object to describe a Pokemon which includes a property
“name” and a method “bark” to shout its name out.
1. Object function
var Pokemon = function (pokemonName) {
// this refers to Pokemon
this.name = pokemonName;
};
Pokemon.prototype.bark = function () {
// this refers to Pokemon
console.log("Hey, I'm " + this.name);
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
pikachu.name = 'PikaPika';
pikachu.bark();
1. Object function
var Pokemon = function (pokemonName) {
// this refers to Pokemon
this.name = pokemonName;
};
Pokemon.prototype.bark = function () {
// this refers to Pokemon
console.log("Hey, I'm " + this.name);
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
// "Hey, I'm Pikachu"
pikachu.name = 'PikaPika';
pikachu.bark();
// "Hey, I'm PikaPika"
2. Object function (without prototype)
var Pokemon = function(pokemonName) {
this.name = pokemonName;
this.bark = function() {
// this refers to Pokemon
console.log("Hey, I'm " + this.name);
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
pikachu.name = 'PikaPika';
pikachu.bark();
2. Object function (without prototype)
var Pokemon = function(pokemonName) {
this.name = pokemonName;
this.bark = function() {
// this refers to Pokemon
console.log("Hey, I'm " + this.name);
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
// "Hey, I'm Pikachu"
pikachu.name = 'PikaPika';
pikachu.bark();
// "Hey, I'm PikaPika"
3. Object Literal
var Pokemon = {
name: "Pikachu",
bark: function() {
console.log("Hey, I'm " + this.name);
}
};
Pokemon.name = 'Pikachu';
Pokemon.bark();
Pokemon.name = 'PikaPika';
Pokemon.bark();
3. Object Literal
var Pokemon = {
name: "Pikachu",
bark: function() {
console.log("Hey, I'm " + this.name);
}
};
Pokemon.name = 'Pikachu';
Pokemon.bark();
// "Hey, I'm Pikachu"
Pokemon.name = 'PikaPika';
Pokemon.bark();
// "Hey, I'm PikaPika"
Question 2.
Please make the public property “name” private in Question 1.
var Pokemon = function(pokemonName) {
this.name = pokemonName;
...
}
1. Object function
var Pokemon = function(pokemonName) {
var name = pokemonName;
this.bark = function () {
console.log("Hey, I'm " + name);
};
// introduce a setter
this.setName = function (newName) {
name = newName;
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
pikachu.setName('PikaPika');
pikachu.bark();
1. Object function
var Pokemon = function(pokemonName) {
var name = pokemonName;
this.bark = function () {
console.log("Hey, I'm " + name);
};
// introduce a setter
this.setName = function (newName) {
name = newName;
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
// "Hey, I'm Pikachu"
pikachu.setName('PikaPika');
pikachu.bark();
// "Hey, I'm PikaPika"
Trying to access the private member...
var Pokemon = function(pokemonName) {
var name = pokemonName;
this.bark = function () {
console.log("Hey, I'm " + name);
};
// introduce a setter
this.setName = function (newName) {
name = newName;
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
pikachu.setName('PikaPika');
pikachu.name = 'PikaPika';
pikachu.bark();
Trying to access the private member...
var Pokemon = function(pokemonName) {
var name = pokemonName;
this.bark = function () {
console.log("Hey, I'm " + name);
};
// introduce a setter
this.setName = function (newName) {
name = newName;
};
};
var pikachu = new Pokemon('Pikachu');
pikachu.bark();
// "Hey, I'm Pikachu"
pikachu.setName('PikaPika');
pikachu.name = 'PikaPika';
pikachu.bark();
// "Hey, I'm Pikachu"
Pokemon.bark();
Pokemon.setName('Pikachu');
Pokemon.bark();
2. Immediately Invoked Function (IIF)
var Pokemon = (function(){
var name;
var setName = function(newName) {
name = newName;
};
var bark = function () {
console.log("Hey, I'm " + name);
};
return {
bark: bark,
setName: setName
};
})();
var Pokemon = (function(){
var name;
var setName = function(newName) {
name = newName;
};
var bark = function () {
console.log("Hey, I'm " + name);
};
return {
bark: bark,
setName: setName
};
})();
Pokemon.bark();
// "Hey, I'm undefined"
Pokemon.setName('Pikachu');
Pokemon.bark();
// "Hey, I'm Pikachu"
2. Immediately Invoked Function (IIF)
function MythPokemon(pokemonName, ability) {
// Call the parent constructor using .call()
Pokemon.call(this, pokemonName);
// Initialize the MythPokomon-specific properties
this.ability = ability;
}
MythPokemon.prototype = Object.create(Pokemon.prototype);
MythPokemon.prototype.bark = function(){
console.log("I'm mighty " + this.name + "! I can " + this.ability);
};
Inheritance
Inheritance: use MythPokemon
var mew = new MythPokemon('Mew', 'sing a song');
mew.bark();
Inheritance: use MythPokemon
var mew = new MythPokemon('Mew', 'sing a song');
mew.bark();
// "I'm mighty mew! I can sing a song"
Happy JavaScript OOP!

Más contenido relacionado

La actualidad más candente

JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival GuideGiordano Scalzo
 
Perforce Object and Record Model
Perforce Object and Record Model  Perforce Object and Record Model
Perforce Object and Record Model Perforce
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leetjohndaviddalton
 
Neuroevolution in Elixir
Neuroevolution in ElixirNeuroevolution in Elixir
Neuroevolution in ElixirJeff Smith
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. ExperienceMike Fogus
 
Introduction to Ruby & Ruby on Rails
Introduction to Ruby & Ruby on RailsIntroduction to Ruby & Ruby on Rails
Introduction to Ruby & Ruby on RailsMarcelo Pinheiro
 
Code Samples & Screenshots
Code Samples & ScreenshotsCode Samples & Screenshots
Code Samples & ScreenshotsNii Amah Hesse
 
Snakes for Camels
Snakes for CamelsSnakes for Camels
Snakes for Camelsmiquelruizm
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Workhorse Computing
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional ProgrammingHoàng Lâm Huỳnh
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]Eleanor McHugh
 
NodeJs
NodeJsNodeJs
NodeJsdizabl
 
NIO.2, the I/O API for the future
NIO.2, the I/O API for the futureNIO.2, the I/O API for the future
NIO.2, the I/O API for the futureMasoud Kalali
 

La actualidad más candente (20)

JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
 
POE
POEPOE
POE
 
Json perl example
Json perl exampleJson perl example
Json perl example
 
Perforce Object and Record Model
Perforce Object and Record Model  Perforce Object and Record Model
Perforce Object and Record Model
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
Neuroevolution in Elixir
Neuroevolution in ElixirNeuroevolution in Elixir
Neuroevolution in Elixir
 
Tutorial Puppet
Tutorial PuppetTutorial Puppet
Tutorial Puppet
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Introduction to Ruby & Ruby on Rails
Introduction to Ruby & Ruby on RailsIntroduction to Ruby & Ruby on Rails
Introduction to Ruby & Ruby on Rails
 
Code Samples & Screenshots
Code Samples & ScreenshotsCode Samples & Screenshots
Code Samples & Screenshots
 
Snakes for Camels
Snakes for CamelsSnakes for Camels
Snakes for Camels
 
Meet.js promises
Meet.js promisesMeet.js promises
Meet.js promises
 
ReUse Your (Puppet) Modules!
ReUse Your (Puppet) Modules!ReUse Your (Puppet) Modules!
ReUse Your (Puppet) Modules!
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Having Fun Programming!
Having Fun Programming!Having Fun Programming!
Having Fun Programming!
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
 
NodeJs
NodeJsNodeJs
NodeJs
 
NIO.2, the I/O API for the future
NIO.2, the I/O API for the futureNIO.2, the I/O API for the future
NIO.2, the I/O API for the future
 

Destacado

الحياة والبرمجة كائنية التوجه
الحياة والبرمجة كائنية التوجهالحياة والبرمجة كائنية التوجه
الحياة والبرمجة كائنية التوجهAnees Abu hmaid
 
باللغة العربية jQuery دورة
 باللغة العربية jQuery دورة باللغة العربية jQuery دورة
باللغة العربية jQuery دورةanees abu-hmaid
 
Canvas دورة باللغة العربية
Canvas دورة باللغة العربيةCanvas دورة باللغة العربية
Canvas دورة باللغة العربيةanees abu-hmaid
 
Bootstrap3 دورة باللغة العربية
Bootstrap3 دورة باللغة العربيةBootstrap3 دورة باللغة العربية
Bootstrap3 دورة باللغة العربيةanees abu-hmaid
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsVolodymyr Voytyshyn
 

Destacado (10)

Oop
OopOop
Oop
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
الحياة والبرمجة كائنية التوجه
الحياة والبرمجة كائنية التوجهالحياة والبرمجة كائنية التوجه
الحياة والبرمجة كائنية التوجه
 
jQuery UI (Effect)
jQuery UI (Effect) jQuery UI (Effect)
jQuery UI (Effect)
 
باللغة العربية jQuery دورة
 باللغة العربية jQuery دورة باللغة العربية jQuery دورة
باللغة العربية jQuery دورة
 
Canvas دورة باللغة العربية
Canvas دورة باللغة العربيةCanvas دورة باللغة العربية
Canvas دورة باللغة العربية
 
Bootstrap3 دورة باللغة العربية
Bootstrap3 دورة باللغة العربيةBootstrap3 دورة باللغة العربية
Bootstrap3 دورة باللغة العربية
 
Js dom & JS bom
Js dom & JS bomJs dom & JS bom
Js dom & JS bom
 
Adopt-a-School Program Kit
Adopt-a-School Program Kit Adopt-a-School Program Kit
Adopt-a-School Program Kit
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design Patterns
 

Similar a [Frontend 101] JavaScript OOP

Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: PrototypesVernon Kesner
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptLeonardo Borges
 
JavaScript - Like a Box of Chocolates
JavaScript - Like a Box of ChocolatesJavaScript - Like a Box of Chocolates
JavaScript - Like a Box of ChocolatesRobert Nyman
 
JSGeneve - Backbone.js
JSGeneve - Backbone.jsJSGeneve - Backbone.js
JSGeneve - Backbone.jsPierre Spring
 
JSDay Italy - Backbone.js
JSDay Italy - Backbone.jsJSDay Italy - Backbone.js
JSDay Italy - Backbone.jsPierre Spring
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritancepedro.carvalho
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02plutoone TestTwo
 
import java.util.ArrayList; import java.util.Iterator; .pdf
import java.util.ArrayList; import java.util.Iterator;  .pdfimport java.util.ArrayList; import java.util.Iterator;  .pdf
import java.util.ArrayList; import java.util.Iterator; .pdfdilipanushkagallery
 
Link.javaLink.javapackage com.bookstore.domain.model;import .docx
Link.javaLink.javapackage com.bookstore.domain.model;import .docxLink.javaLink.javapackage com.bookstore.domain.model;import .docx
Link.javaLink.javapackage com.bookstore.domain.model;import .docxSHIVA101531
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
ekb.py - Python VS ...
ekb.py - Python VS ...ekb.py - Python VS ...
ekb.py - Python VS ...it-people
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptMiao Siyu
 

Similar a [Frontend 101] JavaScript OOP (20)

Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Prototype
PrototypePrototype
Prototype
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
 
JavaScript - Like a Box of Chocolates
JavaScript - Like a Box of ChocolatesJavaScript - Like a Box of Chocolates
JavaScript - Like a Box of Chocolates
 
JSGeneve - Backbone.js
JSGeneve - Backbone.jsJSGeneve - Backbone.js
JSGeneve - Backbone.js
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
JSDay Italy - Backbone.js
JSDay Italy - Backbone.jsJSDay Italy - Backbone.js
JSDay Italy - Backbone.js
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
JavaScript OOP
JavaScript OOPJavaScript OOP
JavaScript OOP
 
import java.util.ArrayList; import java.util.Iterator; .pdf
import java.util.ArrayList; import java.util.Iterator;  .pdfimport java.util.ArrayList; import java.util.Iterator;  .pdf
import java.util.ArrayList; import java.util.Iterator; .pdf
 
Link.javaLink.javapackage com.bookstore.domain.model;import .docx
Link.javaLink.javapackage com.bookstore.domain.model;import .docxLink.javaLink.javapackage com.bookstore.domain.model;import .docx
Link.javaLink.javapackage com.bookstore.domain.model;import .docx
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
ekb.py - Python VS ...
ekb.py - Python VS ...ekb.py - Python VS ...
ekb.py - Python VS ...
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 

Más de Maxis Kao

User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖Maxis Kao
 
優化開發環境 無料提升戰鬥力
優化開發環境 無料提升戰鬥力優化開發環境 無料提升戰鬥力
優化開發環境 無料提升戰鬥力Maxis Kao
 
2016 前端潮玩意兒
2016 前端潮玩意兒2016 前端潮玩意兒
2016 前端潮玩意兒Maxis Kao
 
Bot 甘苦談,使用者至上
Bot 甘苦談,使用者至上Bot 甘苦談,使用者至上
Bot 甘苦談,使用者至上Maxis Kao
 
Enjoy privacy on Gitlab
Enjoy privacy on GitlabEnjoy privacy on Gitlab
Enjoy privacy on GitlabMaxis Kao
 
Timelapse News
Timelapse NewsTimelapse News
Timelapse NewsMaxis Kao
 

Más de Maxis Kao (6)

User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖
 
優化開發環境 無料提升戰鬥力
優化開發環境 無料提升戰鬥力優化開發環境 無料提升戰鬥力
優化開發環境 無料提升戰鬥力
 
2016 前端潮玩意兒
2016 前端潮玩意兒2016 前端潮玩意兒
2016 前端潮玩意兒
 
Bot 甘苦談,使用者至上
Bot 甘苦談,使用者至上Bot 甘苦談,使用者至上
Bot 甘苦談,使用者至上
 
Enjoy privacy on Gitlab
Enjoy privacy on GitlabEnjoy privacy on Gitlab
Enjoy privacy on Gitlab
 
Timelapse News
Timelapse NewsTimelapse News
Timelapse News
 

Último

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 

Último (20)

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 

[Frontend 101] JavaScript OOP

  • 1. JavaScript OOP Maxis Kao @ Yahoo Search Frontend 101
  • 5. OOP
  • 6. Classical OOP ➔ Object-oriented programming ➔ C++, Java, Python
  • 7. ➔ Prototype-based programming ➔ Prototypical inheritance in JavaScript,
  • 9. Class // define a new class called Pokemon with an empty constructor var Pokemon = function () {}; Defines the object's characteristics. A class is a template definition of an object's properties and methods.
  • 10. Object // create two instances, mew and pikachu var mew = new Pokemon(); var pikachu = new Pokemon(); An instance of a class.
  • 11. Property An object characteristic, such as name. // define the name property for the Pokemon class var Pokemon = function (pokemonName) { this.name = pokemonName; };
  • 12. Method An object capability, such as walk. It is a subroutine or function associated with a class. // define the method bark() for the Pokemon class Pokemon.prototype.bark = function () { console.log("Hey, I'm " + this.name); };
  • 13. Question 1. Please use JavaScript object to describe a Pokemon which includes a property “name” and a method “bark” to shout its name out.
  • 14. 1. Object function var Pokemon = function (pokemonName) { // this refers to Pokemon this.name = pokemonName; }; Pokemon.prototype.bark = function () { // this refers to Pokemon console.log("Hey, I'm " + this.name); }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); pikachu.name = 'PikaPika'; pikachu.bark();
  • 15. 1. Object function var Pokemon = function (pokemonName) { // this refers to Pokemon this.name = pokemonName; }; Pokemon.prototype.bark = function () { // this refers to Pokemon console.log("Hey, I'm " + this.name); }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); // "Hey, I'm Pikachu" pikachu.name = 'PikaPika'; pikachu.bark(); // "Hey, I'm PikaPika"
  • 16. 2. Object function (without prototype) var Pokemon = function(pokemonName) { this.name = pokemonName; this.bark = function() { // this refers to Pokemon console.log("Hey, I'm " + this.name); }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); pikachu.name = 'PikaPika'; pikachu.bark();
  • 17. 2. Object function (without prototype) var Pokemon = function(pokemonName) { this.name = pokemonName; this.bark = function() { // this refers to Pokemon console.log("Hey, I'm " + this.name); }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); // "Hey, I'm Pikachu" pikachu.name = 'PikaPika'; pikachu.bark(); // "Hey, I'm PikaPika"
  • 18. 3. Object Literal var Pokemon = { name: "Pikachu", bark: function() { console.log("Hey, I'm " + this.name); } }; Pokemon.name = 'Pikachu'; Pokemon.bark(); Pokemon.name = 'PikaPika'; Pokemon.bark();
  • 19. 3. Object Literal var Pokemon = { name: "Pikachu", bark: function() { console.log("Hey, I'm " + this.name); } }; Pokemon.name = 'Pikachu'; Pokemon.bark(); // "Hey, I'm Pikachu" Pokemon.name = 'PikaPika'; Pokemon.bark(); // "Hey, I'm PikaPika"
  • 20. Question 2. Please make the public property “name” private in Question 1. var Pokemon = function(pokemonName) { this.name = pokemonName; ... }
  • 21. 1. Object function var Pokemon = function(pokemonName) { var name = pokemonName; this.bark = function () { console.log("Hey, I'm " + name); }; // introduce a setter this.setName = function (newName) { name = newName; }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); pikachu.setName('PikaPika'); pikachu.bark();
  • 22. 1. Object function var Pokemon = function(pokemonName) { var name = pokemonName; this.bark = function () { console.log("Hey, I'm " + name); }; // introduce a setter this.setName = function (newName) { name = newName; }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); // "Hey, I'm Pikachu" pikachu.setName('PikaPika'); pikachu.bark(); // "Hey, I'm PikaPika"
  • 23. Trying to access the private member... var Pokemon = function(pokemonName) { var name = pokemonName; this.bark = function () { console.log("Hey, I'm " + name); }; // introduce a setter this.setName = function (newName) { name = newName; }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); pikachu.setName('PikaPika'); pikachu.name = 'PikaPika'; pikachu.bark();
  • 24. Trying to access the private member... var Pokemon = function(pokemonName) { var name = pokemonName; this.bark = function () { console.log("Hey, I'm " + name); }; // introduce a setter this.setName = function (newName) { name = newName; }; }; var pikachu = new Pokemon('Pikachu'); pikachu.bark(); // "Hey, I'm Pikachu" pikachu.setName('PikaPika'); pikachu.name = 'PikaPika'; pikachu.bark(); // "Hey, I'm Pikachu"
  • 25. Pokemon.bark(); Pokemon.setName('Pikachu'); Pokemon.bark(); 2. Immediately Invoked Function (IIF) var Pokemon = (function(){ var name; var setName = function(newName) { name = newName; }; var bark = function () { console.log("Hey, I'm " + name); }; return { bark: bark, setName: setName }; })();
  • 26. var Pokemon = (function(){ var name; var setName = function(newName) { name = newName; }; var bark = function () { console.log("Hey, I'm " + name); }; return { bark: bark, setName: setName }; })(); Pokemon.bark(); // "Hey, I'm undefined" Pokemon.setName('Pikachu'); Pokemon.bark(); // "Hey, I'm Pikachu" 2. Immediately Invoked Function (IIF)
  • 27. function MythPokemon(pokemonName, ability) { // Call the parent constructor using .call() Pokemon.call(this, pokemonName); // Initialize the MythPokomon-specific properties this.ability = ability; } MythPokemon.prototype = Object.create(Pokemon.prototype); MythPokemon.prototype.bark = function(){ console.log("I'm mighty " + this.name + "! I can " + this.ability); }; Inheritance
  • 28. Inheritance: use MythPokemon var mew = new MythPokemon('Mew', 'sing a song'); mew.bark();
  • 29. Inheritance: use MythPokemon var mew = new MythPokemon('Mew', 'sing a song'); mew.bark(); // "I'm mighty mew! I can sing a song"