SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Downloaden Sie, um offline zu lesen
18th of December, 2014
Javascript and OOP
The power of simplicity
18th of December, 2014
Outline
● The Javascript object
● Adding behaviour
● Inheritance without classes
● OOP concepts in JS
18th of December, 2014
Outline
● The Javascript object
● Adding behaviour
● Inheritance without classes
● OOP concepts in JS
18th of December, 2014
What is an object ?
var label = {
x: 20,
y: 23,
width: 100,
height: 24,
text: "Click me!"
};
label["x"] === 20;
label.y === 23;
18th of December, 2014
What is an object ?
var label = {
x: 20,
y: 23,
width: 100,
height: 24,
text: "Click me!"
};
label["x"] === 20;
label.y === 23;
18th of December, 2014
What is an object ?
var label = {
x: 20,
y: 23,
width: 100,
height: 24,
text: "Click me!"
};
label["x"] === 20;
label.y === 23;
18th of December, 2014
Create it in a function
function createLabel(x, y, text) {
return {
x: x,
y: y,
width: computeWidth(text),
height: defaultHeight,
text: text
};
}
var errorLabel = createLabel(20, 23, "There was an error");
var successLabel = createLabel(56, 89, "Success");
18th of December, 2014
The constructor function
function Label(x, y, text) {
this.x = x;
this.y = y;
this.width = computeWidth(text);
this.height = defaultHeight;
this.text = text;
}
var errorLabel = new Label(20, 23, "There was an error");
errorLabel.x === 20;
18th of December, 2014
The constructor function
function Label(x, y, text) {
this.x = x;
this.y = y;
this.width = computeWidth(text);
this.height = defaultHeight;
this.text = text;
}
var errorLabel = new Label(20, 23, "There was an error");
errorLabel.x === 20;
18th of December, 2014
The constructor function
function Label(x, y, text) {
this.x = x;
this.y = y;
this.width = computeWidth(text);
this.height = defaultHeight;
this.text = text;
}
var errorLabel = new Label(20, 23, "There was an error");
errorLabel.x === 20;
18th of December, 2014
Outline
● The Javascript object
● Adding behaviour
● Inheritance without classes
● OOP concepts in JS
18th of December, 2014
Adding behaviour
var greeter = {
nameToGreet: "Roger",
};
greeter.greet(); // How to do it ?
18th of December, 2014
Adding behaviour
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
greeter.greet(); // "Hello Roger!"
18th of December, 2014
Adding behaviour
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
greeter.greet(); // "Hello Roger!"
18th of December, 2014
A word about this...
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
var semiGreeter = { nameToGreet: "Bob" };
semiGreeter.greet = greeter.greet;
semiGreeter.greet(); // "Hello Bob!"
18th of December, 2014
A word about this...
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
var semiGreeter = { nameToGreet: "Bob" };
semiGreeter.greet = greeter.greet;
semiGreeter.greet(); // "Hello Bob!"
18th of December, 2014
A word about this...
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
var semiGreeter = { nameToGreet: "Bob" };
semiGreeter.greet = greeter.greet;
semiGreeter.greet(); // "Hello Bob!"
18th of December, 2014
A word about this...
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
var semiGreeter = { nameToGreet: "Bob" };
semiGreeter.greet = greeter.greet;
semiGreeter.greet(); // "Hello Bob!"
18th of December, 2014
A word about this...
var greeter = {
nameToGreet: "Roger",
greet: function () {
console.log("Hello " + this.nameToGreet + "!");
}
};
var semiGreeter = { nameToGreet: "Bob" };
semiGreeter.greet = greeter.greet;
semiGreeter.greet(); // "Hello Bob!"
18th of December, 2014
Outline
● The Javascript object
● Adding behaviour
● Inheritance without classes
● OOP concepts in JS
18th of December, 2014
Prototypal inheritance
Do not have classes ?
Extend objects !
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
18th of December, 2014
Prototypal inheritance
var baseObject = {
name: "baseObject"
};
var childObject = {
__proto__: baseObject
};
childObject.name === "baseObject";
childObject.name = "childObject";
childObject.name === "childObject";
Overrides baseObject.
name
18th of December, 2014
Prototype with constructor
var baseObject = {
name: "baseObject"
};
var ChildObject = function () {};
ChildObject.prototype = baseObject;
var childObject = new ChildObject();
childObject.name === "baseObject";
18th of December, 2014
Prototype with constructor
var baseObject = {
name: "baseObject"
};
var ChildObject = function () {};
ChildObject.prototype = baseObject;
var childObject = new ChildObject();
childObject.name === "baseObject";
18th of December, 2014
Outline
● The Javascript object
● Adding behaviour
● Inheritance without classes
● OOP concepts in JS
18th of December, 2014
Classes
function Greeter(name) {
this.name = name;
}
Greeter.prototype = {
greet: function () {
console.log("Hello " + this.name + "!");
}
};
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
18th of December, 2014
Classes
function Greeter(name) {
this.name = name;
}
Greeter.prototype = {
greet: function () {
console.log("Hello " + this.name + "!");
}
};
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
Initialize instance
18th of December, 2014
Classes
function Greeter(name) {
this.name = name;
}
Greeter.prototype = {
greet: function () {
console.log("Hello " + this.name + "!");
}
};
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
Methods
18th of December, 2014
Static fields
function Greeter(name) {
this.name = name || Greeter.DEFAULT_NAME;
}
Greeter.DEFAULT_NAME = "Sir";
Greeter.prototype = {
// ...
};
var greeter = new Greeter();
greeter.greet(); // "Hello Sir!"
18th of December, 2014
Inheritance
function AwesomeGreeter(name) {
Greeter.call(this, name);
}
AwesomeGreeter.prototype = new Greeter();
AwesomeGreeter.prototype.superGreet = function () {
this.greet();
console.log("You are awesome!");
};
var greeter = new AwesomeGreeter("Chuck");
greeter.superGreet(); // "Hello Chuck!" "You are awesome!"
18th of December, 2014
Inheritance
function AwesomeGreeter(name) {
Greeter.call(this, name);
}
AwesomeGreeter.prototype = new Greeter();
AwesomeGreeter.prototype.superGreet = function () {
this.greet();
console.log("You are awesome!");
};
var greeter = new AwesomeGreeter("Chuck");
greeter.superGreet(); // "Hello Chuck!" "You are awesome!"
Call parent’s
constructor
18th of December, 2014
Inheritance
function AwesomeGreeter(name) {
Greeter.call(this, name);
}
AwesomeGreeter.prototype = new Greeter();
AwesomeGreeter.prototype.superGreet = function () {
this.greet();
console.log("You are awesome!");
};
var greeter = new AwesomeGreeter("Chuck");
greeter.superGreet(); // "Hello Chuck!" "You are awesome!"
Inherit parent’s methods
18th of December, 2014
Inheritance
function AwesomeGreeter(name) {
Greeter.call(this, name);
}
AwesomeGreeter.prototype = new Greeter();
AwesomeGreeter.prototype.superGreet = function () {
this.greet();
console.log("You are awesome!");
};
var greeter = new AwesomeGreeter("Chuck");
greeter.superGreet(); // "Hello Chuck!" "You are awesome!"
Add new
methods
18th of December, 2014
Private fields
function Greeter(name) {
this.greet = function () {
console.log('Hello ' + name + '!');
};
}
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
console.log(greeter.name); // undefined
18th of December, 2014
Private fields
function Greeter(name) {
this.greet = function () {
console.log('Hello ' + name + '!');
};
}
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
console.log(greeter.name); // undefined
Define greet as a
closure
18th of December, 2014
Private fields
function Greeter(name) {
function buildMessage() {
return 'Hello ' + name + '!';
}
this.greet = function () {
console.log(buildMessage());
};
}
var greeter = new Greeter("Roger");
greeter.greet(); // "Hello Roger!"
console.log(greeter.name); // undefined
Private method
18th of December, 2014
What did we learn ?
With:
● Objects
18th of December, 2014
What did we learn ?
With:
● Objects
● Functions
18th of December, 2014
What did we learn ?
With:
● Objects
● Functions
● Prototypes
18th of December, 2014
What did we learn ?
With:
● Objects
● Functions
● Prototypes
We got:
● Classes
● Methods
● Inheritance
● Static fields
● Private fields
18th of December, 2014
What did we learn ?
With:
● Objects
● Functions
● Prototypes
We got:
● Classes
● Methods
● Inheritance
● Static fields
● Private fields
So Javascript is cool !
18th of December, 2014
Questions ?
For online questions, please leave a comment on the article.
18th of December, 2014
Join the community !
(in Paris)
Social networks :
● Follow us on Twitter : https://twitter.com/steamlearn
● Like us on Facebook : https://www.facebook.com/steamlearn
SteamLearn is an Inovia initiative : inovia.fr
You wish to be in the audience ? Join the meetup group!
http://www.meetup.com/Steam-Learn/
18th of December, 2014
References
● MDN - Introduction to object-oriented Javascript
● 2ality - JavaScript’s “this”: how it works, where it can trip
you up
● Ecmascript 6 specification draft

Weitere ähnliche Inhalte

Was ist angesagt?

An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
Paul Irish
 
Five Things you Need to Know About Scaling
Five Things you Need to Know About ScalingFive Things you Need to Know About Scaling
Five Things you Need to Know About Scaling
MongoDB
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
guestcf600a
 
HTML5 JavaScript Interfaces
HTML5 JavaScript InterfacesHTML5 JavaScript Interfaces
HTML5 JavaScript Interfaces
Aaron Gustafson
 

Was ist angesagt? (18)

Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
NoSQL & MongoDB
NoSQL & MongoDBNoSQL & MongoDB
NoSQL & MongoDB
 
Mobile Database Persistence
Mobile Database PersistenceMobile Database Persistence
Mobile Database Persistence
 
Php
PhpPhp
Php
 
Introduction To Moose
Introduction To MooseIntroduction To Moose
Introduction To Moose
 
MongoDB - Introduction
MongoDB - IntroductionMongoDB - Introduction
MongoDB - Introduction
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
 
Five Things you Need to Know About Scaling
Five Things you Need to Know About ScalingFive Things you Need to Know About Scaling
Five Things you Need to Know About Scaling
 
Moose (Perl 5)
Moose (Perl 5)Moose (Perl 5)
Moose (Perl 5)
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)
 
Introduction to Moose
Introduction to MooseIntroduction to Moose
Introduction to Moose
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 
HTML5 JavaScript Interfaces
HTML5 JavaScript InterfacesHTML5 JavaScript Interfaces
HTML5 JavaScript Interfaces
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
2018 PyCon Korea - Ring
2018 PyCon Korea - Ring2018 PyCon Korea - Ring
2018 PyCon Korea - Ring
 

Ähnlich wie Steam Learn: Javascript and OOP

Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
iKlaus
 
JSDay Italy - Backbone.js
JSDay Italy - Backbone.jsJSDay Italy - Backbone.js
JSDay Italy - Backbone.js
Pierre Spring
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
Olaf Alders
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 

Ähnlich wie Steam Learn: Javascript and OOP (20)

Node.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer NäheNode.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer Nähe
 
AmdJavaMeetupBDDUsingCucumber
AmdJavaMeetupBDDUsingCucumberAmdJavaMeetupBDDUsingCucumber
AmdJavaMeetupBDDUsingCucumber
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
 
Object-oriented Javascript
Object-oriented JavascriptObject-oriented Javascript
Object-oriented Javascript
 
JSGeneve - Backbone.js
JSGeneve - Backbone.jsJSGeneve - Backbone.js
JSGeneve - Backbone.js
 
JavaScript - Like a Box of Chocolates
JavaScript - Like a Box of ChocolatesJavaScript - Like a Box of Chocolates
JavaScript - Like a Box of Chocolates
 
JSDay Italy - Backbone.js
JSDay Italy - Backbone.jsJSDay Italy - Backbone.js
JSDay Italy - Backbone.js
 
Hooks WCSD12
Hooks WCSD12Hooks WCSD12
Hooks WCSD12
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
JavaScript - Like a Box of Chocolates - jsDay
JavaScript - Like a Box of Chocolates - jsDayJavaScript - Like a Box of Chocolates - jsDay
JavaScript - Like a Box of Chocolates - jsDay
 
JavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New WorldJavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New World
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Google Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidGoogle Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & Android
 
Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First Widget
 
20160227 Granma
20160227 Granma20160227 Granma
20160227 Granma
 

Mehr von inovia

Mehr von inovia (20)

10 tips for Redux at scale
10 tips for Redux at scale10 tips for Redux at scale
10 tips for Redux at scale
 
10 essentials steps for kafka streaming services
10 essentials steps for kafka streaming services10 essentials steps for kafka streaming services
10 essentials steps for kafka streaming services
 
Redux at scale
Redux at scaleRedux at scale
Redux at scale
 
DocuSign's Road to react
DocuSign's Road to reactDocuSign's Road to react
DocuSign's Road to react
 
API Gateway: Nginx way
API Gateway: Nginx wayAPI Gateway: Nginx way
API Gateway: Nginx way
 
Kafka: meetup microservice
Kafka: meetup microserviceKafka: meetup microservice
Kafka: meetup microservice
 
Microservice: starting point
Microservice:  starting pointMicroservice:  starting point
Microservice: starting point
 
Correlation id (tid)
Correlation id (tid)Correlation id (tid)
Correlation id (tid)
 
Meetic back end redesign - Meetup microservices
Meetic back end redesign - Meetup microservicesMeetic back end redesign - Meetup microservices
Meetic back end redesign - Meetup microservices
 
Security in microservices architectures
Security in microservices architecturesSecurity in microservices architectures
Security in microservices architectures
 
Building a Secure, Performant Network Fabric for Microservice Applications
Building a Secure, Performant Network Fabric for Microservice ApplicationsBuilding a Secure, Performant Network Fabric for Microservice Applications
Building a Secure, Performant Network Fabric for Microservice Applications
 
Microservices vs SOA
Microservices vs SOAMicroservices vs SOA
Microservices vs SOA
 
CQRS, an introduction by JC Bohin
CQRS, an introduction by JC BohinCQRS, an introduction by JC Bohin
CQRS, an introduction by JC Bohin
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Oauth2, open-id connect with microservices
Oauth2, open-id connect with microservicesOauth2, open-id connect with microservices
Oauth2, open-id connect with microservices
 
You probably don't need microservices
You probably don't need microservicesYou probably don't need microservices
You probably don't need microservices
 
Api Gateway - What's the use of an api gateway?
Api Gateway - What's the use of an api gateway?Api Gateway - What's the use of an api gateway?
Api Gateway - What's the use of an api gateway?
 
Steam Learn: An introduction to Redis
Steam Learn: An introduction to RedisSteam Learn: An introduction to Redis
Steam Learn: An introduction to Redis
 
Steam Learn: Speedrun et TAS
Steam Learn: Speedrun et TASSteam Learn: Speedrun et TAS
Steam Learn: Speedrun et TAS
 
Steam Learn: Asynchronous Javascript
Steam Learn: Asynchronous JavascriptSteam Learn: Asynchronous Javascript
Steam Learn: Asynchronous Javascript
 

Kürzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Kürzlich hochgeladen (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Steam Learn: Javascript and OOP

  • 1. 18th of December, 2014 Javascript and OOP The power of simplicity
  • 2. 18th of December, 2014 Outline ● The Javascript object ● Adding behaviour ● Inheritance without classes ● OOP concepts in JS
  • 3. 18th of December, 2014 Outline ● The Javascript object ● Adding behaviour ● Inheritance without classes ● OOP concepts in JS
  • 4. 18th of December, 2014 What is an object ? var label = { x: 20, y: 23, width: 100, height: 24, text: "Click me!" }; label["x"] === 20; label.y === 23;
  • 5. 18th of December, 2014 What is an object ? var label = { x: 20, y: 23, width: 100, height: 24, text: "Click me!" }; label["x"] === 20; label.y === 23;
  • 6. 18th of December, 2014 What is an object ? var label = { x: 20, y: 23, width: 100, height: 24, text: "Click me!" }; label["x"] === 20; label.y === 23;
  • 7. 18th of December, 2014 Create it in a function function createLabel(x, y, text) { return { x: x, y: y, width: computeWidth(text), height: defaultHeight, text: text }; } var errorLabel = createLabel(20, 23, "There was an error"); var successLabel = createLabel(56, 89, "Success");
  • 8. 18th of December, 2014 The constructor function function Label(x, y, text) { this.x = x; this.y = y; this.width = computeWidth(text); this.height = defaultHeight; this.text = text; } var errorLabel = new Label(20, 23, "There was an error"); errorLabel.x === 20;
  • 9. 18th of December, 2014 The constructor function function Label(x, y, text) { this.x = x; this.y = y; this.width = computeWidth(text); this.height = defaultHeight; this.text = text; } var errorLabel = new Label(20, 23, "There was an error"); errorLabel.x === 20;
  • 10. 18th of December, 2014 The constructor function function Label(x, y, text) { this.x = x; this.y = y; this.width = computeWidth(text); this.height = defaultHeight; this.text = text; } var errorLabel = new Label(20, 23, "There was an error"); errorLabel.x === 20;
  • 11. 18th of December, 2014 Outline ● The Javascript object ● Adding behaviour ● Inheritance without classes ● OOP concepts in JS
  • 12. 18th of December, 2014 Adding behaviour var greeter = { nameToGreet: "Roger", }; greeter.greet(); // How to do it ?
  • 13. 18th of December, 2014 Adding behaviour var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; greeter.greet(); // "Hello Roger!"
  • 14. 18th of December, 2014 Adding behaviour var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; greeter.greet(); // "Hello Roger!"
  • 15. 18th of December, 2014 A word about this... var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; var semiGreeter = { nameToGreet: "Bob" }; semiGreeter.greet = greeter.greet; semiGreeter.greet(); // "Hello Bob!"
  • 16. 18th of December, 2014 A word about this... var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; var semiGreeter = { nameToGreet: "Bob" }; semiGreeter.greet = greeter.greet; semiGreeter.greet(); // "Hello Bob!"
  • 17. 18th of December, 2014 A word about this... var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; var semiGreeter = { nameToGreet: "Bob" }; semiGreeter.greet = greeter.greet; semiGreeter.greet(); // "Hello Bob!"
  • 18. 18th of December, 2014 A word about this... var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; var semiGreeter = { nameToGreet: "Bob" }; semiGreeter.greet = greeter.greet; semiGreeter.greet(); // "Hello Bob!"
  • 19. 18th of December, 2014 A word about this... var greeter = { nameToGreet: "Roger", greet: function () { console.log("Hello " + this.nameToGreet + "!"); } }; var semiGreeter = { nameToGreet: "Bob" }; semiGreeter.greet = greeter.greet; semiGreeter.greet(); // "Hello Bob!"
  • 20. 18th of December, 2014 Outline ● The Javascript object ● Adding behaviour ● Inheritance without classes ● OOP concepts in JS
  • 21. 18th of December, 2014 Prototypal inheritance Do not have classes ? Extend objects !
  • 22. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject";
  • 23. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject";
  • 24. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject";
  • 25. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject";
  • 26. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject";
  • 27. 18th of December, 2014 Prototypal inheritance var baseObject = { name: "baseObject" }; var childObject = { __proto__: baseObject }; childObject.name === "baseObject"; childObject.name = "childObject"; childObject.name === "childObject"; Overrides baseObject. name
  • 28. 18th of December, 2014 Prototype with constructor var baseObject = { name: "baseObject" }; var ChildObject = function () {}; ChildObject.prototype = baseObject; var childObject = new ChildObject(); childObject.name === "baseObject";
  • 29. 18th of December, 2014 Prototype with constructor var baseObject = { name: "baseObject" }; var ChildObject = function () {}; ChildObject.prototype = baseObject; var childObject = new ChildObject(); childObject.name === "baseObject";
  • 30. 18th of December, 2014 Outline ● The Javascript object ● Adding behaviour ● Inheritance without classes ● OOP concepts in JS
  • 31. 18th of December, 2014 Classes function Greeter(name) { this.name = name; } Greeter.prototype = { greet: function () { console.log("Hello " + this.name + "!"); } }; var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!"
  • 32. 18th of December, 2014 Classes function Greeter(name) { this.name = name; } Greeter.prototype = { greet: function () { console.log("Hello " + this.name + "!"); } }; var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!" Initialize instance
  • 33. 18th of December, 2014 Classes function Greeter(name) { this.name = name; } Greeter.prototype = { greet: function () { console.log("Hello " + this.name + "!"); } }; var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!" Methods
  • 34. 18th of December, 2014 Static fields function Greeter(name) { this.name = name || Greeter.DEFAULT_NAME; } Greeter.DEFAULT_NAME = "Sir"; Greeter.prototype = { // ... }; var greeter = new Greeter(); greeter.greet(); // "Hello Sir!"
  • 35. 18th of December, 2014 Inheritance function AwesomeGreeter(name) { Greeter.call(this, name); } AwesomeGreeter.prototype = new Greeter(); AwesomeGreeter.prototype.superGreet = function () { this.greet(); console.log("You are awesome!"); }; var greeter = new AwesomeGreeter("Chuck"); greeter.superGreet(); // "Hello Chuck!" "You are awesome!"
  • 36. 18th of December, 2014 Inheritance function AwesomeGreeter(name) { Greeter.call(this, name); } AwesomeGreeter.prototype = new Greeter(); AwesomeGreeter.prototype.superGreet = function () { this.greet(); console.log("You are awesome!"); }; var greeter = new AwesomeGreeter("Chuck"); greeter.superGreet(); // "Hello Chuck!" "You are awesome!" Call parent’s constructor
  • 37. 18th of December, 2014 Inheritance function AwesomeGreeter(name) { Greeter.call(this, name); } AwesomeGreeter.prototype = new Greeter(); AwesomeGreeter.prototype.superGreet = function () { this.greet(); console.log("You are awesome!"); }; var greeter = new AwesomeGreeter("Chuck"); greeter.superGreet(); // "Hello Chuck!" "You are awesome!" Inherit parent’s methods
  • 38. 18th of December, 2014 Inheritance function AwesomeGreeter(name) { Greeter.call(this, name); } AwesomeGreeter.prototype = new Greeter(); AwesomeGreeter.prototype.superGreet = function () { this.greet(); console.log("You are awesome!"); }; var greeter = new AwesomeGreeter("Chuck"); greeter.superGreet(); // "Hello Chuck!" "You are awesome!" Add new methods
  • 39. 18th of December, 2014 Private fields function Greeter(name) { this.greet = function () { console.log('Hello ' + name + '!'); }; } var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!" console.log(greeter.name); // undefined
  • 40. 18th of December, 2014 Private fields function Greeter(name) { this.greet = function () { console.log('Hello ' + name + '!'); }; } var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!" console.log(greeter.name); // undefined Define greet as a closure
  • 41. 18th of December, 2014 Private fields function Greeter(name) { function buildMessage() { return 'Hello ' + name + '!'; } this.greet = function () { console.log(buildMessage()); }; } var greeter = new Greeter("Roger"); greeter.greet(); // "Hello Roger!" console.log(greeter.name); // undefined Private method
  • 42. 18th of December, 2014 What did we learn ? With: ● Objects
  • 43. 18th of December, 2014 What did we learn ? With: ● Objects ● Functions
  • 44. 18th of December, 2014 What did we learn ? With: ● Objects ● Functions ● Prototypes
  • 45. 18th of December, 2014 What did we learn ? With: ● Objects ● Functions ● Prototypes We got: ● Classes ● Methods ● Inheritance ● Static fields ● Private fields
  • 46. 18th of December, 2014 What did we learn ? With: ● Objects ● Functions ● Prototypes We got: ● Classes ● Methods ● Inheritance ● Static fields ● Private fields So Javascript is cool !
  • 47. 18th of December, 2014 Questions ? For online questions, please leave a comment on the article.
  • 48. 18th of December, 2014 Join the community ! (in Paris) Social networks : ● Follow us on Twitter : https://twitter.com/steamlearn ● Like us on Facebook : https://www.facebook.com/steamlearn SteamLearn is an Inovia initiative : inovia.fr You wish to be in the audience ? Join the meetup group! http://www.meetup.com/Steam-Learn/
  • 49. 18th of December, 2014 References ● MDN - Introduction to object-oriented Javascript ● 2ality - JavaScript’s “this”: how it works, where it can trip you up ● Ecmascript 6 specification draft