SlideShare a Scribd company logo
JavaScript
1995, Brendan Eich, Netscape
ECMAScript
Javascript Basics
Usage
Browsers Server Desktop
 interpreted
JS Features:
runtime interpreters
Futhark, InScript, JScript, KJS, Linear B, QtScript, Rhino, YAJI, Duktape
just-in-time interpreters
~2008
Carakan, Chakra, SpiderMonkey, SquirrelFish, Tamarin, V8, JavaScriptCore, Nashorn
ie9 Chakra
google’s V8
no intermediate byte codes, no interpreter
property access
point.x
# ebx = the point object
cmp [ebx,<hidden class offset>],<cached hidden class>
jne <inline cache miss>
mov eax,[ebx, <cached x offset>]
 interpreted
 loose typed
JS Features:
Number String Boolean Object
Array Function Classes
Date
Regexp
Error
Null Undefined
variable types
variable declaration
naming rules:
 a-z, A-Z, _, $
 num6ers (4ever)
 case sensitive (type / Type)
 non-reserved words
ES5
ES6
variable declaration
naming rules:
 a-z, A-Z, _, $
 num6ers (4ever)
 case sensitive (type / Type)
 non-reserved words
var a;
var a = 5;
a = 5;
a;
ReferenceError: a is not defined
ways of declaration:
let a;
let a = 5;
 interpreted
 loose typed
 closures
JS Features:
closures
function parent(){
var width = 5;
var keyword = 'ages';
console.log(width);
console.log(keyword);
console.log(age);
child();
function child(){
var width = 20,
age = 5;
console.log(width);
console.log(keyword);
console.log(age);
}
console.log(age);
console.log(width);
}
5
ages
ReferenceError: age is not defined
20
ages
5
5
ReferenceError: age is not defined
closures
function parent(){
var width = 5;
var keyword = 'ages';
console.log(width);
console.log(keyword);
console.log(age);
if (true){
var width = 20,
age = 5;
console.log(width);
console.log(keyword);
console.log(age);
}
console.log(age);
console.log(width);
}
5
ages
ReferenceError: age is not defined
20
ages
5
5
5
closures
function parent(){
var width = 5;
var keyword = 'ages';
console.log(width);
console.log(keyword);
console.log(age);
if (true){
let width = 20,
age = 5;
console.log(width);
console.log(keyword);
console.log(age);
}
console.log(age);
console.log(width);
}
5
ages
ReferenceError: age is not defined
20
ages
5
20
ReferenceError: age is not defined
ES6
 interpreted
 loose typed
 closures
 multi-paradigmal
JS Features:
 imperative
 functional
 object-oriented
Arrays
var a = new Array();
a = [];
console.log(a.length);
var b = new Array(3,4,5,6);
b = [3,4,5,6];
console.log(b[2]);
a[3] = 'wat';
console.log(a.length);
console.log(a);
read & write
same}
same}
element accessing
element modifying
5
4
0
[undefined × 3, "wat"]
Arrays
modifying
var ar = [3,5];
adding el to array
adding several els to array
form string splitted with char
reversing order of els
adding new els to array
removing last element
[3,5, 10];
[3,5, 10, -5, 20, -2];
[3,5, 10, -5, 20];
3 + 5 + 10 + -5 + 20
[20, -5, 10, 5, 3];
[20, -5, 10, 5, 3, -3, -5];
ar.push(10);
ar.push(-5, 20, -2);
ar.pop();
var str = ar.join(' + ');
ar.reverse();
var secAr = [-3, -5];
ar = ar.concat(secAr);
assigning el to undefined [20, -5, 10, 5, undefined, -3, -5]delete ar[4]
Arrays
modifying
var ar = [3, 5, -10, 6, 20, -10];
returns ar from pos 1
returns 4 els from pos 2
adds els to the start
returns ar from pos 2 from the end
[5, -10, 6, 20, -10];
[-10, 6, 20, -10];
[20, -10];
[4, -5, 7, 5, ‘newbie’, ‘new one’, 20, -10];
ar.slice(1);
ar.slice(2,4);
ar.slice(-2);
ar.splice(2,1);
ar.splice(1,2, 'new one');
ar.splice(1,0, 'newbie');
ar.shift();
ar.unshift(5);
ar.unshift(4, -5, 7);
ar.unshift(); does nothing [4, -5, 7, 5, ‘newbie’, ‘new one’, 20, -10];
adds el to the start [5, ‘newbie’, ‘new one’, 20, -10];
removes el from the start [‘newbie’, ‘new one’, 20, -10];
removes el from pos 2
removes 2 els from pos 1, adds el
adds el to pos 1
[3, 5, 6, 20, -10];
[3, ‘new one’, 20, -10];
[3, ‘newbie’, ‘new one’, 20, -10];
Arrays
traversing
var ar = [-2, 4, 7];
Iterates through array 0 6 9
a should be earlier
for (var i = 0; i < ar.length; i++){
console.log(ar[i] + 2);
}
ar.sort(function(a, b){
if (a > b){
return -1;
} else if (a === 0) {
return 0;
} else {
return 1;
}
});
ar.sort(function(a, b){
return b - a;
});
do nothing
b should be earlier
same result
[7, 4, -2]
[7, 4, -2]
var ar = [10, 20, -5];
ar.forEach(function(el, idx, array){
console.log(el, idx);
});
ar.filter(function(el){
if (el > 0) return true;
});
ar.map(function(el){
return el * 2;
});
ar.reduce(function(a, b){
return a + b;
});
ar.reduceRight(function(a, b){
return a + 0.5 * b;
});
Arrays
traversing
Iterates through array 0 6 9
returns filtered array [10, 20]
performs action on every el [20, 40, -10]
forms one value by performing action to els from the left 25
forms one value by performing action to els from the right 10
ES5
Functions
 first-class
 are actually objects
 can represent constructors for OOP
 can be passed as a parameter
 can take context
 variadic
Functions
function countToNumber(first, last){
var countOnce = function (options){
return options.interim + options.number + options.divider;
}
var result = '';
if (arguments.length === 1) last = 10;
while (typeof first === 'number' && typeof last === 'number' && first <= last){
var params = {
number: first,
divider: '-',
interim: result
};
result = countOnce(params);
first++;
}
return result;
}
console.log(countToNumber(1, 5));
function declaration
function expression nested function
passing hash as an argument
variadic function use
1-2-3-4-5-
declaration & call
(function (str, callback){
if (typeof str === 'string'){
str = str.replace(/pain/g, 'fun');
callback(str);
}
})(str, logFirstWord);
var str = 'invoking function is pain';
function logFirstWord(param){
if (typeof param === 'string'){
var words = param.split(' ');
console.log(words[words.length - 1]);
} else {
throw new Error('Parameter is not a string');
}
}
Functions
immediately-invoked
passing function as an argument
calling function as a constructor
fun
Immediately-invoked
Classes
 absent
 we’ll still call them classes though they’re objects
 access modifiers are absent
 we can emulate them
 inheritance is prototype-based
 composition over inheritance from the box
Classes
function Programmer(options){
this.languages = ['python', 'js'];
this.yearsXP = 1;
this.learnLanguage = function(name){
if (typeof name === 'string'){
this.languages.push(name);
}
}
}
var stewie = new Programmer();
stewie.learnLanguage('ruby');
console.log(stewie.languages);
var hamid = new Programmer();
hamid.learnLanguage('c#');
console.log(hamid.languages);
function-constructor
this points to the object's context
defining method by passing function as a property
creating instance of a Programmer class
["python", "js", "ruby"]
["python", "js", “c#"]
creating / instantiating
Classesfunction Programmer(options){
var languages = ['python', 'js'];
this.yearsXP = 1;
this.learnLanguage = function(name){
if (typeof name === 'string'){
languages.push(name);
logNewLanguage(name);
}
}
function logNewLanguage(language){
console.log(language);
}
}
Programmer.prototype.gainXP = function(years){
if (typeof years === 'number'){
this.yearsXP += years;
}
}
var suzy = new Programmer();
suzy.learnLanguage('php');
suzy.logNewLanguage('php');
private property
private method
logs ‘php’
TypeError: Object #<Programmer> has no method 'logNewLanguage‘
defining method using prototype
using prototype
privileged method
Classes
emulating private members
Programmer = function (options){
var languages = ['python', 'js'];
this.yearsXP = 1;
this.projects = [];
this.projects['Academy'] = {
monthsEstimated: 2,
codeLinesEstimated: 10000
};
}
Programmer.prototype.justCode = function(projectName) {
if (typeof projectName !== 'undefined' && typeof this.projects[projectName] !== 'undefined')
var percents = 30;
var linesScaffolded = scaffold.call(this, projectName, percents);
var linesCoded = codeWithHands.apply(this, [projectName, linesScaffolded]);
console.log('scaffolded ' + linesScaffolded, ' coded ' + linesCoded);
};
function scaffold(projectName, percents){
if (this.projects[projectName].codeLinesEstimated > 0 && percents > 0 && percents < 100){
return Math.ceil(this.projects[projectName].codeLinesEstimated / 100) * percents;
}
}
function codeWithHands(projectName, linesScaffolded){
return this.projects[projectName].codeLinesEstimated - linesScaffolded;
}
var lee = new Programmer();
lee.justCode('Academy');
public method
private method
private method
parameters as usual
parameters within array
context passing}
logs ‘scaffolded 3000 coded 7000’
Classes
inheritance
function Man(){
this.inheritedProperty = 5;
}
Man.prototype.setName = function(name) {
if (typeof name === 'string'){
this.name = name;
}
};
Man.prototype.introduce = function() {
console.log("Hi, my name is " + this.name);
};
var kelly = new Man();
kelly.setName('Kelly');
kelly.introduce();
function Programmer(){
this.selfProperty = 5;
}
Programmer.prototype = new Man();
var joe = new Programmer();
joe.setName('Joe');
joe.introduce();
constructor
constructor
inheriting prototype members of super
Hi, my name is Kelly
Hi, my name is Joe
Programmer
name: "Joe"
selfProperty: 5
__proto__: Man
inheritedProperty: 5
__proto__: Man
constructor: function Man(){
introduce: function () {
setName: function (name) {
__proto__: Object
__defineGetter__: function
__defineSetter__: function
....
Thanks
https://github.com/msemenistyi/js-basics/
@msemenistyi
nikita.s_binary
nikita.semenistyi@binary-studio.com

More Related Content

What's hot (20)

PDF
ReactJS presentation
Thanh Tuong
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPT
Oops concepts in php
CPD INDIA
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
PPTX
presentation in html,css,javascript
FaysalAhammed5
 
PPTX
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
PDF
Strings in java
Kuppusamy P
 
PPT
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
PPT
Php forms
Anne Lee
 
PDF
[143] Modern C++ 무조건 써야 해?
NAVER D2
 
PPT
Javascript
Manav Prasad
 
PPTX
Interface in java
PhD Research Scholar
 
PDF
An Introduction to Redux
NexThoughts Technologies
 
PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PPT
Java Basics
Sunil OS
 
PPTX
Object oreinted php | OOPs
Ravi Bhadauria
 
ReactJS presentation
Thanh Tuong
 
PHP Web Programming
Muthuselvam RS
 
Oops concepts in php
CPD INDIA
 
Methods in Java
Jussi Pohjolainen
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
presentation in html,css,javascript
FaysalAhammed5
 
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
Strings in java
Kuppusamy P
 
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
Php forms
Anne Lee
 
[143] Modern C++ 무조건 써야 해?
NAVER D2
 
Javascript
Manav Prasad
 
Interface in java
PhD Research Scholar
 
An Introduction to Redux
NexThoughts Technologies
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Java Basics
Sunil OS
 
Object oreinted php | OOPs
Ravi Bhadauria
 

Viewers also liked (8)

PPT
11 Java User Interface Libraries for Developing Mobile Applications
AEGIS-ACCESSIBLE Projects
 
PPTX
Java ME - 02 - High Level UI
Andreas Jakl
 
PDF
J2ME GUI Programming
Rohan Chandane
 
PDF
Java in the Air: A Case Study for Java-based Environment Monitoring Stations
Eurotech
 
PPTX
Statistical Process Control Tools
Raja Farhan Saeed
 
PPTX
AggreGate SCADA/HMI
Tibbo
 
PDF
8 introduction to_java_script
Vijay Kalyan
 
PPTX
Java script
Jay Patel
 
11 Java User Interface Libraries for Developing Mobile Applications
AEGIS-ACCESSIBLE Projects
 
Java ME - 02 - High Level UI
Andreas Jakl
 
J2ME GUI Programming
Rohan Chandane
 
Java in the Air: A Case Study for Java-based Environment Monitoring Stations
Eurotech
 
Statistical Process Control Tools
Raja Farhan Saeed
 
AggreGate SCADA/HMI
Tibbo
 
8 introduction to_java_script
Vijay Kalyan
 
Java script
Jay Patel
 
Ad

Similar to Javascript Basics (20)

PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PDF
Workshop 10: ECMAScript 6
Visual Engineering
 
PPTX
Academy PRO: ES2015
Binary Studio
 
PDF
JavaScript Primer
Daniel Cousineau
 
PPTX
Java script
Adrian Caetano
 
PDF
Introduction to web programming for java and c# programmers by @drpicox
David Rodenas
 
PPT
Intermediate JavaScript
☆ Milan Adamovsky ☆
 
PPTX
ES6: Features + Rails
Santosh Wadghule
 
PDF
Introduction to ECMAScript 2015
Tomasz Dziuda
 
PDF
ES6 - Next Generation Javascript
RameshNair6
 
KEY
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
PPT
JavaScript Needn't Hurt!
Thomas Kjeldahl Nilsson
 
KEY
Week3
Will Gaybrick
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PPT
25-functions.ppt
JyothiAmpally
 
PDF
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
PPTX
Front end fundamentals session 1: javascript core
Web Zhao
 
PDF
The hidden and new parts of JS
Ritesh Kumar
 
PDF
Fewd week5 slides
William Myers
 
PPTX
11. session 11 functions and objects
Phúc Đỗ
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Workshop 10: ECMAScript 6
Visual Engineering
 
Academy PRO: ES2015
Binary Studio
 
JavaScript Primer
Daniel Cousineau
 
Java script
Adrian Caetano
 
Introduction to web programming for java and c# programmers by @drpicox
David Rodenas
 
Intermediate JavaScript
☆ Milan Adamovsky ☆
 
ES6: Features + Rails
Santosh Wadghule
 
Introduction to ECMAScript 2015
Tomasz Dziuda
 
ES6 - Next Generation Javascript
RameshNair6
 
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
JavaScript Needn't Hurt!
Thomas Kjeldahl Nilsson
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
25-functions.ppt
JyothiAmpally
 
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
Front end fundamentals session 1: javascript core
Web Zhao
 
The hidden and new parts of JS
Ritesh Kumar
 
Fewd week5 slides
William Myers
 
11. session 11 functions and objects
Phúc Đỗ
 
Ad

Recently uploaded (20)

PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Designing Production-Ready AI Agents
Kunal Rai
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 

Javascript Basics

  • 2. 1995, Brendan Eich, Netscape ECMAScript
  • 6. runtime interpreters Futhark, InScript, JScript, KJS, Linear B, QtScript, Rhino, YAJI, Duktape just-in-time interpreters ~2008 Carakan, Chakra, SpiderMonkey, SquirrelFish, Tamarin, V8, JavaScriptCore, Nashorn
  • 8. google’s V8 no intermediate byte codes, no interpreter property access point.x # ebx = the point object cmp [ebx,<hidden class offset>],<cached hidden class> jne <inline cache miss> mov eax,[ebx, <cached x offset>]
  • 9.  interpreted  loose typed JS Features:
  • 10. Number String Boolean Object Array Function Classes Date Regexp Error Null Undefined variable types
  • 11. variable declaration naming rules:  a-z, A-Z, _, $  num6ers (4ever)  case sensitive (type / Type)  non-reserved words
  • 12. ES5
  • 13. ES6
  • 14. variable declaration naming rules:  a-z, A-Z, _, $  num6ers (4ever)  case sensitive (type / Type)  non-reserved words var a; var a = 5; a = 5; a; ReferenceError: a is not defined ways of declaration: let a; let a = 5;
  • 15.  interpreted  loose typed  closures JS Features:
  • 16. closures function parent(){ var width = 5; var keyword = 'ages'; console.log(width); console.log(keyword); console.log(age); child(); function child(){ var width = 20, age = 5; console.log(width); console.log(keyword); console.log(age); } console.log(age); console.log(width); } 5 ages ReferenceError: age is not defined 20 ages 5 5 ReferenceError: age is not defined
  • 17. closures function parent(){ var width = 5; var keyword = 'ages'; console.log(width); console.log(keyword); console.log(age); if (true){ var width = 20, age = 5; console.log(width); console.log(keyword); console.log(age); } console.log(age); console.log(width); } 5 ages ReferenceError: age is not defined 20 ages 5 5 5
  • 18. closures function parent(){ var width = 5; var keyword = 'ages'; console.log(width); console.log(keyword); console.log(age); if (true){ let width = 20, age = 5; console.log(width); console.log(keyword); console.log(age); } console.log(age); console.log(width); } 5 ages ReferenceError: age is not defined 20 ages 5 20 ReferenceError: age is not defined ES6
  • 19.  interpreted  loose typed  closures  multi-paradigmal JS Features:  imperative  functional  object-oriented
  • 20. Arrays var a = new Array(); a = []; console.log(a.length); var b = new Array(3,4,5,6); b = [3,4,5,6]; console.log(b[2]); a[3] = 'wat'; console.log(a.length); console.log(a); read & write same} same} element accessing element modifying 5 4 0 [undefined × 3, "wat"]
  • 21. Arrays modifying var ar = [3,5]; adding el to array adding several els to array form string splitted with char reversing order of els adding new els to array removing last element [3,5, 10]; [3,5, 10, -5, 20, -2]; [3,5, 10, -5, 20]; 3 + 5 + 10 + -5 + 20 [20, -5, 10, 5, 3]; [20, -5, 10, 5, 3, -3, -5]; ar.push(10); ar.push(-5, 20, -2); ar.pop(); var str = ar.join(' + '); ar.reverse(); var secAr = [-3, -5]; ar = ar.concat(secAr); assigning el to undefined [20, -5, 10, 5, undefined, -3, -5]delete ar[4]
  • 22. Arrays modifying var ar = [3, 5, -10, 6, 20, -10]; returns ar from pos 1 returns 4 els from pos 2 adds els to the start returns ar from pos 2 from the end [5, -10, 6, 20, -10]; [-10, 6, 20, -10]; [20, -10]; [4, -5, 7, 5, ‘newbie’, ‘new one’, 20, -10]; ar.slice(1); ar.slice(2,4); ar.slice(-2); ar.splice(2,1); ar.splice(1,2, 'new one'); ar.splice(1,0, 'newbie'); ar.shift(); ar.unshift(5); ar.unshift(4, -5, 7); ar.unshift(); does nothing [4, -5, 7, 5, ‘newbie’, ‘new one’, 20, -10]; adds el to the start [5, ‘newbie’, ‘new one’, 20, -10]; removes el from the start [‘newbie’, ‘new one’, 20, -10]; removes el from pos 2 removes 2 els from pos 1, adds el adds el to pos 1 [3, 5, 6, 20, -10]; [3, ‘new one’, 20, -10]; [3, ‘newbie’, ‘new one’, 20, -10];
  • 23. Arrays traversing var ar = [-2, 4, 7]; Iterates through array 0 6 9 a should be earlier for (var i = 0; i < ar.length; i++){ console.log(ar[i] + 2); } ar.sort(function(a, b){ if (a > b){ return -1; } else if (a === 0) { return 0; } else { return 1; } }); ar.sort(function(a, b){ return b - a; }); do nothing b should be earlier same result [7, 4, -2] [7, 4, -2]
  • 24. var ar = [10, 20, -5]; ar.forEach(function(el, idx, array){ console.log(el, idx); }); ar.filter(function(el){ if (el > 0) return true; }); ar.map(function(el){ return el * 2; }); ar.reduce(function(a, b){ return a + b; }); ar.reduceRight(function(a, b){ return a + 0.5 * b; }); Arrays traversing Iterates through array 0 6 9 returns filtered array [10, 20] performs action on every el [20, 40, -10] forms one value by performing action to els from the left 25 forms one value by performing action to els from the right 10 ES5
  • 25. Functions  first-class  are actually objects  can represent constructors for OOP  can be passed as a parameter  can take context  variadic
  • 26. Functions function countToNumber(first, last){ var countOnce = function (options){ return options.interim + options.number + options.divider; } var result = ''; if (arguments.length === 1) last = 10; while (typeof first === 'number' && typeof last === 'number' && first <= last){ var params = { number: first, divider: '-', interim: result }; result = countOnce(params); first++; } return result; } console.log(countToNumber(1, 5)); function declaration function expression nested function passing hash as an argument variadic function use 1-2-3-4-5- declaration & call
  • 27. (function (str, callback){ if (typeof str === 'string'){ str = str.replace(/pain/g, 'fun'); callback(str); } })(str, logFirstWord); var str = 'invoking function is pain'; function logFirstWord(param){ if (typeof param === 'string'){ var words = param.split(' '); console.log(words[words.length - 1]); } else { throw new Error('Parameter is not a string'); } } Functions immediately-invoked passing function as an argument calling function as a constructor fun Immediately-invoked
  • 28. Classes  absent  we’ll still call them classes though they’re objects  access modifiers are absent  we can emulate them  inheritance is prototype-based  composition over inheritance from the box
  • 29. Classes function Programmer(options){ this.languages = ['python', 'js']; this.yearsXP = 1; this.learnLanguage = function(name){ if (typeof name === 'string'){ this.languages.push(name); } } } var stewie = new Programmer(); stewie.learnLanguage('ruby'); console.log(stewie.languages); var hamid = new Programmer(); hamid.learnLanguage('c#'); console.log(hamid.languages); function-constructor this points to the object's context defining method by passing function as a property creating instance of a Programmer class ["python", "js", "ruby"] ["python", "js", “c#"] creating / instantiating
  • 30. Classesfunction Programmer(options){ var languages = ['python', 'js']; this.yearsXP = 1; this.learnLanguage = function(name){ if (typeof name === 'string'){ languages.push(name); logNewLanguage(name); } } function logNewLanguage(language){ console.log(language); } } Programmer.prototype.gainXP = function(years){ if (typeof years === 'number'){ this.yearsXP += years; } } var suzy = new Programmer(); suzy.learnLanguage('php'); suzy.logNewLanguage('php'); private property private method logs ‘php’ TypeError: Object #<Programmer> has no method 'logNewLanguage‘ defining method using prototype using prototype privileged method
  • 31. Classes emulating private members Programmer = function (options){ var languages = ['python', 'js']; this.yearsXP = 1; this.projects = []; this.projects['Academy'] = { monthsEstimated: 2, codeLinesEstimated: 10000 }; } Programmer.prototype.justCode = function(projectName) { if (typeof projectName !== 'undefined' && typeof this.projects[projectName] !== 'undefined') var percents = 30; var linesScaffolded = scaffold.call(this, projectName, percents); var linesCoded = codeWithHands.apply(this, [projectName, linesScaffolded]); console.log('scaffolded ' + linesScaffolded, ' coded ' + linesCoded); }; function scaffold(projectName, percents){ if (this.projects[projectName].codeLinesEstimated > 0 && percents > 0 && percents < 100){ return Math.ceil(this.projects[projectName].codeLinesEstimated / 100) * percents; } } function codeWithHands(projectName, linesScaffolded){ return this.projects[projectName].codeLinesEstimated - linesScaffolded; } var lee = new Programmer(); lee.justCode('Academy'); public method private method private method parameters as usual parameters within array context passing} logs ‘scaffolded 3000 coded 7000’
  • 32. Classes inheritance function Man(){ this.inheritedProperty = 5; } Man.prototype.setName = function(name) { if (typeof name === 'string'){ this.name = name; } }; Man.prototype.introduce = function() { console.log("Hi, my name is " + this.name); }; var kelly = new Man(); kelly.setName('Kelly'); kelly.introduce(); function Programmer(){ this.selfProperty = 5; } Programmer.prototype = new Man(); var joe = new Programmer(); joe.setName('Joe'); joe.introduce(); constructor constructor inheriting prototype members of super Hi, my name is Kelly Hi, my name is Joe Programmer name: "Joe" selfProperty: 5 __proto__: Man inheritedProperty: 5 __proto__: Man constructor: function Man(){ introduce: function () { setName: function (name) { __proto__: Object __defineGetter__: function __defineSetter__: function ....