SlideShare a Scribd company logo
1 of 72
Let's
JavaScript
(Not any other
programming
language)

by Pawel Dorofiejczyk
Java... what !?
"Java is to JavaScript like car is to carpet"
● Object Oriented
● Dynamic and lose typed
● No classes, no interfaces, no
abstraction
● Prototypical inheritance
● No privacy control at object level
● Object as collection of properties
● Function scope
LOL! Who use that crap !?
● Websites frontend
● Server Side JavaScript (node.js, Rhino)
● Document oriented databases: MongoDB,
CouchDB
● Windows 8 (WinJS)
● GNOME Shell
● OpenOffice and Adobe Creative Suite
(Photoshop, Illustrator, Dreamwaver)
● Adobe AIR
and more...
Let's start brainwashing!
Let's create a variable
var name = "Let's JavaScript"; //string
var i = 1; //number ("integer")
var fl = 1.11; //number ("float")
var boo = true; //boolean
glob = "I'm global string";
JavaScript's variable
Local variable declaration by var
keyword
● Global variable declaration - without
var (don't use it!)
● Dynamic type
●
Variable types
primitives

objects

special

Number

Object

Null

Function

String

Array
Date

Boolean

RegExp

Undefined
typeof - the crap?
typeof "string var"; //returns "string";
typeof 1; //returns "number";
typeof {}; //returns "object";
typeof undefined; //returns "undefined";
typeof function(){}; //returns "function"
But...
typeof []; //returns "object" (!)
typeof null; //returns "object" (!!!)
typeof NaN; // returns "number" ;)
Let's create a function

var f = function(arg) {
//function body
};
Function in JavaScript
●
●
●
●
●
●
●
●

Is an object
Can have properties and methods
Is Closure
Reference can be stored in variable
Can create other functions
Have own scope
Have special property - prototype
Native methods bind, apply, call
So...
function hello() {
console.log('Hello world');

Looks like "normal" function declaration,
but in fact...

}

===
var hello = function() {
console.log('Hello world');
}

...it is translated to this form so
anonymous function is created and it's
reference is passed to var

===

var hello = new Function(
"console.log('Hello world')"
);

and what's more every function
declaration is creation of new object
Function arguments
● Are stored in pseudo-array arguments
● Primitives (number, string, boolean) are
always passed by value
● Objects are passed by reference
Function arguments
var test = function(a, b, obj) {
obj.modified = true;
console.log(arguments);
}
var obj = {};
console.log(obj.modified); //'undefined'
test(1, 'a', obj); //{ '0': 1, '1': 'a', '2': { modified: true } }
console.log(obj.modified); //true
It is object so was passed
by reference
Accumulator problem
Problem :
"I want to create accumulator function which
accumulates values passed to it"
Solution:
"In JavaScript, function is an object so I can
create function with internal variable handling
passed values sum"
How to do NOT use function
var accumulator = function(x) {
this.sum = (this.sum || 0) + x;
return this.sum;
}
console.log(accumulator(1)); //1
console.log(accumulator(1)); //2
console.log(accumulator.sum); //undefined
console.log(sum); //2
//whoups!
this IS NOT this ?!
this IS NOT this
this by default refers to the object that a
function is a member of

{
var accumulator = function(x) {
this.sum = (this.sum || 0) + x;
return this.sum;
}
}

In this case, function is member of
default global object so this refers to
global object.
If we run this code in browser, global
object is window so this === window
Let’s try without this
var accumulator = function(x) {
accumulator.sum = (accumulator.sum || 0) + x;
return accumulator.sum;
}
Public, so someone can spoil our
internal state

Ok… but...
accumulator.sum = undefined;
accumulator(1); //returns NaN :(
Scope, the ultimate!*

*Function is the ultimate but without scope, function is nothing
Let's create a scope

var f = function() {
//new scope :)
};

looks familiar?
or in other way...

(function() {
//new scope
}());
JavaScript's scope
● Is static (lexical)
● Is created only by function
● Function arguments becomes part of
scope
● Child scope have reference to parent
scope (scope chain)
● this is not scope (!!!)
Function scope
var test = 'global scope var';
var f = function() {
var test = 'function f scope';
console.log(test);
}

Local test variable declaration,
covers global test variable

console.log(test); //returns 'global scope var'
f(); //returns 'function f scope'
Hidden scope reference
var test = 'global scope var';
var a = function() {
console.log(test);
}
var b = function(callback) {
var test = 'function b scope var';
callback();
}
b(a); //returns 'global scope var'

Function object
contains hidden
reference to scope in
which was created
Hidden scope reference
var test = 5;
var inc = function() {
return ++test;
}
var dec = function() {
return --test;
}
inc(); //returns 6
dec(); //returns 5 NOT 4

Both functions inc
and dec have
reference to common
scope so both can
modify the same
variable
Returning to accumulator problem
"Creator" function

var accumulator = (function() {
Initialize sum variable in
var sum = 0;
creator function's scope
return function(x) {
Adds x to parent scope's
sum += x;
variable sum
return sum;
};
})();
Immediate executing

Scope based privacy!
Common scoping mistake
var callbacks = [];
for(var i = 0; i < 10; i++) {
callbacks[i] = function() {
console.log(i);
}
}
for(var index in callbacks) {
callbacks[index]();
}
Common scoping mistake
var callbacks = [];
for(var i = 0; i < 10; i++) {
callbacks[i] = function() {
console.log(i);
}
At the end of for loop, i === 10
}
for(var index in callbacks) {
callbacks[index]();
}

For loop doesn't create
new scope, so variable i is
global

Log global variable i
.bind for the rescue!

bind is Function object method which
creates new function with bounded
parameters
.bind for the rescue!
var log = function(x) {
console.log(x);
}

Object reference which
will be bound to this
variable in function
scope (It doesn't matter
in this case)

var logHello = log.bind(null, ”Hello”);
log(1); //prints 1
log(”test”); //prints test
logHello(); //prints Hello

Values which will be
bound to function’s
parameters
Common scoping mistake FIXED
var callbacks = [];
var logger = function(i) {
console.log(i);
}

Create local scope
variable i
Log logger function's
scope variable i

for(var i = 0; i < 10; i++) {
callbacks[i] = logger.bind(null, i);
}
Returns new function with bounded
global variable i value as function
argument
Let's create an object

var obj = {};
B-B-B-B-But... Where is the
class?!
Object-oriented, NOT Class-oriented
Java

JavaScript

class HelloWorld {
public void greet() {
System.out.println('Hello!');
}
}

var obj = {
greet: function() {
console.log('Hello');
}
}

HelloWorld obj = new HelloWorld();
obj.greet()

obj.greet();
JavaScript Object
●
●
●
●
●
●
●
●
●

Dynamic, not ordered, key-value
Collection of properties
Array access or object access
Iterable
Created in runtime
Object literal {}
No privacy control at object level
Prototypical inheritance
Constructor functions
MOAR objects!
Simple objects
var obj = {
prop1: 'test' //add property at creation time
}
obj.prop2 = function() { //add property later
console.log(this.prop1);
}
console.log(obj.prop1 == obj['prop1']); //returns true
obj['prop2'](); //returns 'test'
Iterate objects
var obj = {
prop1: 'test1',
prop2: 'test2',
prop3: function(){}
}
for(var prop in obj) {
console.log(obj[prop]);
}

Returns:
'test1'
'test2'
[Function]
OR
'test2'
'test1'
[Function]
OR
...
Iterate objects
var obj = {
prop1: 'test1',
prop2: 'test2',
prop3: function(){}
}
for(var prop in obj) {
console.log(obj[prop]);
}

IT DOESN'T GUARANTEE
ORDER!!!
Constructors in JavaScript

Constructor is not magic object method.
It's a function which returns new objects.
Constructors in JavaScript
Java

JavaScript

class HelloWorld {
private String greetMsg;

function HelloWorld(greetMsg) {
return {
greet: function() {
console.log(greetMsg);
}
}
}

public HelloWorld(String greetMsg) {
this.greetMsg = greetMsg;
}
public void greet() {
System.out.println(this.greetMsg);
}
}
HelloWorld hi = new HelloWorld('Hi!');
obj.greet();

var hi = HelloWorld('Hi!');
hi.greet();
new and prototype
How new works
Have to be used with constructor
function
● Uses prototype function property
reference as __proto__ of new object
● Binds new object to constructor's this
● Executes constructor function
●
How new works
var HelloWorld = function(greetMsg) {
this.greetMsg = greetMsg;
Don't use return. New
object will be returned
automatically

}

HelloWorld.prototype.greet = function() {
console.log(this.greetMsg);
}
var hi = new HelloWorld('Hi!');
hi.greet();

We create our constructor
as new function object

this is binded to new object so
we declare object variable
greetMsg

We declare common object's
properties in constructor
function's prototype
How new works
HelloWorld.prototype
● greet

HelloWorld
● prototype

hi
●

__proto__

hello
● __proto__

*Imagine __proto__ is hidden reference (it is available in some browsers but It is not standard)
How new works
var HelloWorld = function(greetMsg) {
this.greetMsg = greetMsg;
Don't use return. New
object will be returned
automatically

}

HelloWorld.prototype.greet = function() {
console.log(this.greetMsg);
}
var hi = new HelloWorld('Hi!');
hi.greet();
console.log(hi.greetMsg); //returns 'Hi!'

We create our constructor
as new function object

this is binded to new object so
we declare object variable
greetMsg

We declare common object's
properties in constructor
function's prototype
Privacy problem - how to do NOT fix
var HelloWorld = function(greetMsg) {
}
HelloWorld.prototype.greet = function() {
console.log(greetMsg);
}

We would like to keep greetMsg
private. As we know, the only
way is to do it private in
constructor's scope

We are trying to get private
variable but it is different scope
which don't have access to
constructor's scope

var hi = new HelloWorld('Hi!');
hi.greet(); //ReferenceError: greetMsg is not defined
Privacy problem fix
var HelloWorld = function(greetMsg) {
this.greet = function() {

We creating greet function
inside constructor so it has
access to constructor's scope.

console.log(greetMsg);
}
}

Consider that it is not part of
prototype so we create new
function per object creation.
In JavaScript, memory is price
for privacy.

var hi = new HelloWorld('Hi!');
hi.greet();
console.log(hi.greetMsg); //returns 'undefined'
Forgotten new disaster
var HelloWorld = function(greetMsg) {
this.greetMsg = greetMsg;
}

this is binded to global object
so we declare global variable
greetMsg

HelloWorld.prototype.greet = function() {
console.log(this.greetMsg);

Prototype won't be used, so
we won't have greet method

}
Disaster begins here

var hi = HelloWorld('Hi!');
hi.greet(); //TypeError: Cannot call method 'greet' of undefined
console.log(greetMsg); //returns 'Hi!'
Your application has just been blown up

Do you still like new?
Classical inheritance
var Animal = function(name) {
this.name = name;
}
Animal.prototype.getName = function() {
return this.name;
}
var dolphin = new Animal('Dolphin');
dolphin.getName(); //returns 'Dolphin'
Classical inheritance
We want WalkingAnimal, which extends Animal and
adds method walk
var WalkingAnimal = function() {
}

We need constructor
function

WalkingAnimal.prototype = new Animal();
We have to set object we would like to extend as
constructor prototype.
Unfortunately we have to call constructor to create
object. We don't pass any arguments so name will be
undefined
Classical inheritance
var WalkingAnimal = function(name) {
We should not forget
Animal.call(this, name);
about calling Animal
constructor to do it's job
}
WalkingAnimal.prototype = new Animal();
WalkingAnimal.prototype.walk = function() {
console.log(this.getName() + 'is walking');
Next we add method walk
}
to WalkingAnimal
prototype

var cow = new WalkingAnimal('Cow');
cow.getName(); //returns 'Cow'
cow.walk(); //'Cow is walking'
Looks ugly?
Object.create*
var WalkingAnimal = function(name) {
Animal.call(this, name);
}
WalkingAnimal.prototype = Object.create(Animal.prototype);
WalkingAnimal.prototype.walk = function() {
console.log(this.getName() + ' is walking');
}
Object.create sets hidden __proto__
reference to Animal.prototype but
without calling constructor

*Introduced in JavaScript 1.6
inherits function to hide ugly stuff
Function.prototype.inherits = function(parent, methods) {
this.prototype = Object.create(parent.prototype);
We store parent in
this.prototype.$parent = parent;
$parent variable. Can
be useful later
for(name in methods) {
this.prototype[name] = methods[name];
}
}
We copy every method from methods
object to function prototype
inherits method usage
var WalkingAnimal = function(name) {
this.$parent.call(this, name);
}
WalkingAnimal.inherits(Animal, {
walk: function() {
console.log(this.getName() + 'is walking');
}
}
Or maybe don't do it classical
●
●
●
●
●

Don't use new
Don't use prototype
Forgot about inheritance tree
Use composition and mixins
Use power of functions (including functional
programming)

● Treat objects like function namespace or data container
not state container

● Occasionally use Object.create and Object.
defineProperty if you need
References
●

Douglas Crockford "JavaScript: The good parts"

●

Douglas Crockford's site (http://www.crockford.com/)

●

Dymitry Soshnikov's blog (http://dmitrysoshnikov.com)

●

Mozilla's "A re-introduction to JavaScript" (https://developer.mozilla.org/enUS/docs/JavaScript/A_re-introduction_to_JavaScript?redirect=no)

●

Angus Croll's blog (http://javascriptweblog.wordpress.com)
Questions?
Want more?
Here you are director's cut ;)
JavaScript primitives with methods?
var a = 1;
console.log(typeof a); //returns 'number'
console.log(a instanceof Number); //returns false
console.log(a.toExponential()); //returns 1e+0
var b = "abc";
console.log(typeof b); //returns 'string'
console.log(b instanceof String); //returns false
console.log(b.slice(1)); //returns 'bc'
var c = true;
console.log(typeof c); //returns 'boolean'
console.log(c instanceof Boolean); //returns false
console.log(c.toString()); //returns 'true'
The secret life of primitives

var a = 1;
a.toExponential();

It is not really method
of primitive.

=

var a = 1;
(new Number(a)).toExponential();

Every time you trying access
primitive method, new object,
containing primitive value is
created secretly.

More Related Content

What's hot

Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsColin DeCarlo
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Reactive, component 그리고 angular2
Reactive, component 그리고  angular2Reactive, component 그리고  angular2
Reactive, component 그리고 angular2Jeado Ko
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Java script object model
Java script object modelJava script object model
Java script object modelJames Hsieh
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Libraryasync_io
 
Callbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptCallbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptŁukasz Kużyński
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jscacois
 

What's hot (20)

JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Zenly - Reverse geocoding
Zenly - Reverse geocodingZenly - Reverse geocoding
Zenly - Reverse geocoding
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Reactive, component 그리고 angular2
Reactive, component 그리고  angular2Reactive, component 그리고  angular2
Reactive, component 그리고 angular2
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Java script object model
Java script object modelJava script object model
Java script object model
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 
Callbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascriptCallbacks, promises, generators - asynchronous javascript
Callbacks, promises, generators - asynchronous javascript
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.js
 
Promises, Promises
Promises, PromisesPromises, Promises
Promises, Promises
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 

Similar to JavaScript Object Orientation in 40 Characters

Similar to JavaScript Object Orientation in 40 Characters (20)

JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Javascript
JavascriptJavascript
Javascript
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
oojs
oojsoojs
oojs
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bits
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

JavaScript Object Orientation in 40 Characters

  • 2. Java... what !? "Java is to JavaScript like car is to carpet"
  • 3. ● Object Oriented ● Dynamic and lose typed ● No classes, no interfaces, no abstraction ● Prototypical inheritance ● No privacy control at object level ● Object as collection of properties ● Function scope
  • 4. LOL! Who use that crap !?
  • 5. ● Websites frontend ● Server Side JavaScript (node.js, Rhino) ● Document oriented databases: MongoDB, CouchDB ● Windows 8 (WinJS) ● GNOME Shell ● OpenOffice and Adobe Creative Suite (Photoshop, Illustrator, Dreamwaver) ● Adobe AIR and more...
  • 7. Let's create a variable var name = "Let's JavaScript"; //string var i = 1; //number ("integer") var fl = 1.11; //number ("float") var boo = true; //boolean glob = "I'm global string";
  • 8. JavaScript's variable Local variable declaration by var keyword ● Global variable declaration - without var (don't use it!) ● Dynamic type ●
  • 9.
  • 11. typeof - the crap? typeof "string var"; //returns "string"; typeof 1; //returns "number"; typeof {}; //returns "object"; typeof undefined; //returns "undefined"; typeof function(){}; //returns "function" But... typeof []; //returns "object" (!) typeof null; //returns "object" (!!!) typeof NaN; // returns "number" ;)
  • 12.
  • 13. Let's create a function var f = function(arg) { //function body };
  • 14.
  • 15. Function in JavaScript ● ● ● ● ● ● ● ● Is an object Can have properties and methods Is Closure Reference can be stored in variable Can create other functions Have own scope Have special property - prototype Native methods bind, apply, call
  • 16. So... function hello() { console.log('Hello world'); Looks like "normal" function declaration, but in fact... } === var hello = function() { console.log('Hello world'); } ...it is translated to this form so anonymous function is created and it's reference is passed to var === var hello = new Function( "console.log('Hello world')" ); and what's more every function declaration is creation of new object
  • 17. Function arguments ● Are stored in pseudo-array arguments ● Primitives (number, string, boolean) are always passed by value ● Objects are passed by reference
  • 18. Function arguments var test = function(a, b, obj) { obj.modified = true; console.log(arguments); } var obj = {}; console.log(obj.modified); //'undefined' test(1, 'a', obj); //{ '0': 1, '1': 'a', '2': { modified: true } } console.log(obj.modified); //true It is object so was passed by reference
  • 19. Accumulator problem Problem : "I want to create accumulator function which accumulates values passed to it" Solution: "In JavaScript, function is an object so I can create function with internal variable handling passed values sum"
  • 20. How to do NOT use function var accumulator = function(x) { this.sum = (this.sum || 0) + x; return this.sum; } console.log(accumulator(1)); //1 console.log(accumulator(1)); //2 console.log(accumulator.sum); //undefined console.log(sum); //2 //whoups!
  • 21. this IS NOT this ?!
  • 22. this IS NOT this this by default refers to the object that a function is a member of { var accumulator = function(x) { this.sum = (this.sum || 0) + x; return this.sum; } } In this case, function is member of default global object so this refers to global object. If we run this code in browser, global object is window so this === window
  • 23. Let’s try without this var accumulator = function(x) { accumulator.sum = (accumulator.sum || 0) + x; return accumulator.sum; } Public, so someone can spoil our internal state Ok… but... accumulator.sum = undefined; accumulator(1); //returns NaN :(
  • 24.
  • 25. Scope, the ultimate!* *Function is the ultimate but without scope, function is nothing
  • 26. Let's create a scope var f = function() { //new scope :) }; looks familiar?
  • 27. or in other way... (function() { //new scope }());
  • 28. JavaScript's scope ● Is static (lexical) ● Is created only by function ● Function arguments becomes part of scope ● Child scope have reference to parent scope (scope chain) ● this is not scope (!!!)
  • 29. Function scope var test = 'global scope var'; var f = function() { var test = 'function f scope'; console.log(test); } Local test variable declaration, covers global test variable console.log(test); //returns 'global scope var' f(); //returns 'function f scope'
  • 30. Hidden scope reference var test = 'global scope var'; var a = function() { console.log(test); } var b = function(callback) { var test = 'function b scope var'; callback(); } b(a); //returns 'global scope var' Function object contains hidden reference to scope in which was created
  • 31. Hidden scope reference var test = 5; var inc = function() { return ++test; } var dec = function() { return --test; } inc(); //returns 6 dec(); //returns 5 NOT 4 Both functions inc and dec have reference to common scope so both can modify the same variable
  • 32. Returning to accumulator problem "Creator" function var accumulator = (function() { Initialize sum variable in var sum = 0; creator function's scope return function(x) { Adds x to parent scope's sum += x; variable sum return sum; }; })(); Immediate executing Scope based privacy!
  • 33.
  • 34. Common scoping mistake var callbacks = []; for(var i = 0; i < 10; i++) { callbacks[i] = function() { console.log(i); } } for(var index in callbacks) { callbacks[index](); }
  • 35. Common scoping mistake var callbacks = []; for(var i = 0; i < 10; i++) { callbacks[i] = function() { console.log(i); } At the end of for loop, i === 10 } for(var index in callbacks) { callbacks[index](); } For loop doesn't create new scope, so variable i is global Log global variable i
  • 36. .bind for the rescue! bind is Function object method which creates new function with bounded parameters
  • 37. .bind for the rescue! var log = function(x) { console.log(x); } Object reference which will be bound to this variable in function scope (It doesn't matter in this case) var logHello = log.bind(null, ”Hello”); log(1); //prints 1 log(”test”); //prints test logHello(); //prints Hello Values which will be bound to function’s parameters
  • 38. Common scoping mistake FIXED var callbacks = []; var logger = function(i) { console.log(i); } Create local scope variable i Log logger function's scope variable i for(var i = 0; i < 10; i++) { callbacks[i] = logger.bind(null, i); } Returns new function with bounded global variable i value as function argument
  • 39. Let's create an object var obj = {};
  • 40. B-B-B-B-But... Where is the class?!
  • 41. Object-oriented, NOT Class-oriented Java JavaScript class HelloWorld { public void greet() { System.out.println('Hello!'); } } var obj = { greet: function() { console.log('Hello'); } } HelloWorld obj = new HelloWorld(); obj.greet() obj.greet();
  • 42. JavaScript Object ● ● ● ● ● ● ● ● ● Dynamic, not ordered, key-value Collection of properties Array access or object access Iterable Created in runtime Object literal {} No privacy control at object level Prototypical inheritance Constructor functions
  • 44. Simple objects var obj = { prop1: 'test' //add property at creation time } obj.prop2 = function() { //add property later console.log(this.prop1); } console.log(obj.prop1 == obj['prop1']); //returns true obj['prop2'](); //returns 'test'
  • 45. Iterate objects var obj = { prop1: 'test1', prop2: 'test2', prop3: function(){} } for(var prop in obj) { console.log(obj[prop]); } Returns: 'test1' 'test2' [Function] OR 'test2' 'test1' [Function] OR ...
  • 46. Iterate objects var obj = { prop1: 'test1', prop2: 'test2', prop3: function(){} } for(var prop in obj) { console.log(obj[prop]); } IT DOESN'T GUARANTEE ORDER!!!
  • 47.
  • 48. Constructors in JavaScript Constructor is not magic object method. It's a function which returns new objects.
  • 49. Constructors in JavaScript Java JavaScript class HelloWorld { private String greetMsg; function HelloWorld(greetMsg) { return { greet: function() { console.log(greetMsg); } } } public HelloWorld(String greetMsg) { this.greetMsg = greetMsg; } public void greet() { System.out.println(this.greetMsg); } } HelloWorld hi = new HelloWorld('Hi!'); obj.greet(); var hi = HelloWorld('Hi!'); hi.greet();
  • 51. How new works Have to be used with constructor function ● Uses prototype function property reference as __proto__ of new object ● Binds new object to constructor's this ● Executes constructor function ●
  • 52. How new works var HelloWorld = function(greetMsg) { this.greetMsg = greetMsg; Don't use return. New object will be returned automatically } HelloWorld.prototype.greet = function() { console.log(this.greetMsg); } var hi = new HelloWorld('Hi!'); hi.greet(); We create our constructor as new function object this is binded to new object so we declare object variable greetMsg We declare common object's properties in constructor function's prototype
  • 53. How new works HelloWorld.prototype ● greet HelloWorld ● prototype hi ● __proto__ hello ● __proto__ *Imagine __proto__ is hidden reference (it is available in some browsers but It is not standard)
  • 54. How new works var HelloWorld = function(greetMsg) { this.greetMsg = greetMsg; Don't use return. New object will be returned automatically } HelloWorld.prototype.greet = function() { console.log(this.greetMsg); } var hi = new HelloWorld('Hi!'); hi.greet(); console.log(hi.greetMsg); //returns 'Hi!' We create our constructor as new function object this is binded to new object so we declare object variable greetMsg We declare common object's properties in constructor function's prototype
  • 55. Privacy problem - how to do NOT fix var HelloWorld = function(greetMsg) { } HelloWorld.prototype.greet = function() { console.log(greetMsg); } We would like to keep greetMsg private. As we know, the only way is to do it private in constructor's scope We are trying to get private variable but it is different scope which don't have access to constructor's scope var hi = new HelloWorld('Hi!'); hi.greet(); //ReferenceError: greetMsg is not defined
  • 56. Privacy problem fix var HelloWorld = function(greetMsg) { this.greet = function() { We creating greet function inside constructor so it has access to constructor's scope. console.log(greetMsg); } } Consider that it is not part of prototype so we create new function per object creation. In JavaScript, memory is price for privacy. var hi = new HelloWorld('Hi!'); hi.greet(); console.log(hi.greetMsg); //returns 'undefined'
  • 57. Forgotten new disaster var HelloWorld = function(greetMsg) { this.greetMsg = greetMsg; } this is binded to global object so we declare global variable greetMsg HelloWorld.prototype.greet = function() { console.log(this.greetMsg); Prototype won't be used, so we won't have greet method } Disaster begins here var hi = HelloWorld('Hi!'); hi.greet(); //TypeError: Cannot call method 'greet' of undefined console.log(greetMsg); //returns 'Hi!'
  • 58. Your application has just been blown up Do you still like new?
  • 59. Classical inheritance var Animal = function(name) { this.name = name; } Animal.prototype.getName = function() { return this.name; } var dolphin = new Animal('Dolphin'); dolphin.getName(); //returns 'Dolphin'
  • 60. Classical inheritance We want WalkingAnimal, which extends Animal and adds method walk var WalkingAnimal = function() { } We need constructor function WalkingAnimal.prototype = new Animal(); We have to set object we would like to extend as constructor prototype. Unfortunately we have to call constructor to create object. We don't pass any arguments so name will be undefined
  • 61. Classical inheritance var WalkingAnimal = function(name) { We should not forget Animal.call(this, name); about calling Animal constructor to do it's job } WalkingAnimal.prototype = new Animal(); WalkingAnimal.prototype.walk = function() { console.log(this.getName() + 'is walking'); Next we add method walk } to WalkingAnimal prototype var cow = new WalkingAnimal('Cow'); cow.getName(); //returns 'Cow' cow.walk(); //'Cow is walking'
  • 63. Object.create* var WalkingAnimal = function(name) { Animal.call(this, name); } WalkingAnimal.prototype = Object.create(Animal.prototype); WalkingAnimal.prototype.walk = function() { console.log(this.getName() + ' is walking'); } Object.create sets hidden __proto__ reference to Animal.prototype but without calling constructor *Introduced in JavaScript 1.6
  • 64. inherits function to hide ugly stuff Function.prototype.inherits = function(parent, methods) { this.prototype = Object.create(parent.prototype); We store parent in this.prototype.$parent = parent; $parent variable. Can be useful later for(name in methods) { this.prototype[name] = methods[name]; } } We copy every method from methods object to function prototype
  • 65. inherits method usage var WalkingAnimal = function(name) { this.$parent.call(this, name); } WalkingAnimal.inherits(Animal, { walk: function() { console.log(this.getName() + 'is walking'); } }
  • 66. Or maybe don't do it classical ● ● ● ● ● Don't use new Don't use prototype Forgot about inheritance tree Use composition and mixins Use power of functions (including functional programming) ● Treat objects like function namespace or data container not state container ● Occasionally use Object.create and Object. defineProperty if you need
  • 67. References ● Douglas Crockford "JavaScript: The good parts" ● Douglas Crockford's site (http://www.crockford.com/) ● Dymitry Soshnikov's blog (http://dmitrysoshnikov.com) ● Mozilla's "A re-introduction to JavaScript" (https://developer.mozilla.org/enUS/docs/JavaScript/A_re-introduction_to_JavaScript?redirect=no) ● Angus Croll's blog (http://javascriptweblog.wordpress.com)
  • 69. Want more? Here you are director's cut ;)
  • 70.
  • 71. JavaScript primitives with methods? var a = 1; console.log(typeof a); //returns 'number' console.log(a instanceof Number); //returns false console.log(a.toExponential()); //returns 1e+0 var b = "abc"; console.log(typeof b); //returns 'string' console.log(b instanceof String); //returns false console.log(b.slice(1)); //returns 'bc' var c = true; console.log(typeof c); //returns 'boolean' console.log(c instanceof Boolean); //returns false console.log(c.toString()); //returns 'true'
  • 72. The secret life of primitives var a = 1; a.toExponential(); It is not really method of primitive. = var a = 1; (new Number(a)).toExponential(); Every time you trying access primitive method, new object, containing primitive value is created secretly.