SlideShare a Scribd company logo
Introduction to
JavaScript #4
@danielknell

Friday, 18 October 13
http://artsn.co/js-tpl

Friday, 18 October 13
Recap
var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]]
;
for (var x = 0; x < data.length; ++x) {
for (var y = 0; y < data[x].length; ++y) {
console.log(data[x][y]);
}
}

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.id;
el.className;

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.setAttribute('lang', 'en');
el.getAttribute('lang');
// <div id="output" lang="en"></div>
el.setAttribute('data-foo', 'foo');
el.getAttribute('data-foo');
// <div id="output" data-foo="foo"></div>

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.addEventListener('click', callback, false); // not IE
< 9
el.attachEvent('onclick', callback); // IE < 9

Friday, 18 October 13
Recap
var el = document.getElementById('output');
function callback(e) {
alert('hello world');
e.preventDefault();
e.stopPropagation();
}
el.addEventListener('click', callback, false);

Friday, 18 October 13
Recap
Math.round(0.5); // 1
Math.floor(0.9); // 0
Math.ceil(0.1); // 1
Math.abs(-1); // 1
Math.sqrt(9); // 3
Math.sin(1);
Math.cos(1);
Math.tan(1);
Math.asin(1);
Math.acos(1);
Math.atan(1);

//
//
//
//
//
//

0.8414709848078965
0.5403023058681398
1.5574077246549023
1.5707963267948966
0
0.7853981633974483

Math.min(1, 5); // 1
Math.max(1, 5); // 5
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045

Friday, 18 October 13
Slide Puzzle

Friday, 18 October 13
Slide Puzzle
child.addEventListener('click', function(e) {
var x = this.getAttribute('data-x')
, y = this.getAttribute('data-y')
, ok = false
;
if (x == pos.x && Math.abs(y - pos.y) == 1) { ok = true; }
if (y == pos.y && Math.abs(x - pos.x) == 1) { ok = true; }
if (ok) {
this.style.left = (pos.x * pieceWidth) + 'px';
this.style.top = (pos.y * pieceHeight) + 'px';
this.setAttribute('data-x', pos.x);
this.setAttribute('data-y', pos.y);
pos.x = x;
pos.y = y;
}
});

Friday, 18 October 13
Recursion

Friday, 18 October 13
Recursion
function counter(count) {
if (count === undefined) {
count = 1;
}
console.log(count);
if (count < 10) {
counter(count + 1);
}
}
counter();

Friday, 18 October 13
Recursion

Friday, 18 October 13
Recursion

}

Friday, 18 October 13

function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) - fib(n - 2);
Recursion

Friday, 18 October 13
Scope

Friday, 18 October 13
Scope
function greeter(greeting) {
var count = 0
;
function greet(name) {
count++;
}
}

console.log(greeting + ' ' + name + '! (#' + count + ')');

return greet;

hi = greeter('Hi');
hi('Bob'); // Hi Bob (#1)
hi('Fred'); // Hi Fred (#2)
count // undefined

Friday, 18 October 13
Classes

Friday, 18 October 13
Classes
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.name // fido
cat.name // puss

Friday, 18 October 13
Classes
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
Animal.prototype.greet = function() {
console.log(this.sound);
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.greet();
cat.greet();

Friday, 18 October 13
Classes
function Dog(name) {
this.name = name;
this.sound = 'woof!';
}
Dog.prototype = new Animal(null, null);
dog = new Dog('fido');

Friday, 18 October 13
Classes
function Dog(name) {
Animal.call(this, name, 'woof!');
}
Dog.prototype = new Animal(null, null);

Friday, 18 October 13
Slide Puzzle

Friday, 18 October 13
Thats All Folks
email: contact@danielknell.co.uk
twitter: @danielknell
website: http://danielknell.co.uk/

Friday, 18 October 13

More Related Content

What's hot (20)

DOCX
Save game function
George Scott IV
 
TXT
Problemas de Arreglos en c++
Manfred Ariel Martinez Bastos
 
TXT
Program to sort array using insertion sort
Swarup Boro
 
PDF
A tour of Python
Aleksandar Veselinovic
 
PPTX
Intro to Cuda
David Hauck
 
PDF
Bytes in the Machine: Inside the CPython interpreter
akaptur
 
PPTX
API Python Chess: Distribution of Chess Wins based on random moves
Yao Yao
 
PPTX
Fixing Web Data in Production
Aaron Knight
 
PDF
Writing a compiler in go
Yusuke Kita
 
PDF
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
akaptur
 
PDF
Docopt
René Ribaud
 
PDF
Codigos
Brian Joseff
 
DOCX
Opp compile
Muhammad Faiz
 
PDF
Меняем javascript с помощью javascript
Pavel Volokitin
 
PDF
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
akaptur
 
PDF
Diving into byte code optimization in python
Chetan Giridhar
 
PDF
D-Talk: What's awesome about Ruby 2.x and Rails 4
Jan Berdajs
 
PDF
ng-conf 2017: Angular Mischief Maker Slides
Lukas Ruebbelke
 
Save game function
George Scott IV
 
Problemas de Arreglos en c++
Manfred Ariel Martinez Bastos
 
Program to sort array using insertion sort
Swarup Boro
 
A tour of Python
Aleksandar Veselinovic
 
Intro to Cuda
David Hauck
 
Bytes in the Machine: Inside the CPython interpreter
akaptur
 
API Python Chess: Distribution of Chess Wins based on random moves
Yao Yao
 
Fixing Web Data in Production
Aaron Knight
 
Writing a compiler in go
Yusuke Kita
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
akaptur
 
Docopt
René Ribaud
 
Codigos
Brian Joseff
 
Opp compile
Muhammad Faiz
 
Меняем javascript с помощью javascript
Pavel Volokitin
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
akaptur
 
Diving into byte code optimization in python
Chetan Giridhar
 
D-Talk: What's awesome about Ruby 2.x and Rails 4
Jan Berdajs
 
ng-conf 2017: Angular Mischief Maker Slides
Lukas Ruebbelke
 

Viewers also liked (20)

PPT
JAVA SCRIPT
Go4Guru
 
PDF
Introduction to JavaScript: Week Two
Event Handler
 
PPTX
The big bang theory - UNIT 2
lm092068
 
PPTX
The big bang theory of social recruiting
FastCollab
 
PPTX
8. java script
AnusAhmad
 
PDF
An Introduction to JavaScript: Week 3
Event Handler
 
PDF
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
PPTX
Introduction to Java Script
Vijay Kumar Verma
 
PPT
Java Script
siddaram
 
PDF
Unchallengeable miracle of Holy Quran
yoursincerefriend
 
PDF
An Introduction to JavaScript: Week One
Event Handler
 
PPTX
Big Bang Theory
Kevin James
 
PPT
Java script -23jan2015
Sasidhar Kothuru
 
PPT
Chapter 1 - How the world begin
Green Pond Baptist Church
 
PPTX
Big Bang Theorychandler
guest008d7bd
 
PPTX
Qur’an and its sciences
Kalsoom Mohammed
 
PDF
Large-Scale JavaScript Development
Addy Osmani
 
PPT
The Quran and Computational Linguistics
Abdul Baquee Muhammad Sharaf
 
PPT
Java script Learn Easy
prince Loffar
 
PPTX
Evolution of universe
Anmol Marya
 
JAVA SCRIPT
Go4Guru
 
Introduction to JavaScript: Week Two
Event Handler
 
The big bang theory - UNIT 2
lm092068
 
The big bang theory of social recruiting
FastCollab
 
8. java script
AnusAhmad
 
An Introduction to JavaScript: Week 3
Event Handler
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
Introduction to Java Script
Vijay Kumar Verma
 
Java Script
siddaram
 
Unchallengeable miracle of Holy Quran
yoursincerefriend
 
An Introduction to JavaScript: Week One
Event Handler
 
Big Bang Theory
Kevin James
 
Java script -23jan2015
Sasidhar Kothuru
 
Chapter 1 - How the world begin
Green Pond Baptist Church
 
Big Bang Theorychandler
guest008d7bd
 
Qur’an and its sciences
Kalsoom Mohammed
 
Large-Scale JavaScript Development
Addy Osmani
 
The Quran and Computational Linguistics
Abdul Baquee Muhammad Sharaf
 
Java script Learn Easy
prince Loffar
 
Evolution of universe
Anmol Marya
 
Ad

Similar to An Introduction to JavaScript: Week 4 (20)

PDF
Javascript
Vlad Ifrim
 
PDF
CoffeeScript
Ryan McGeary
 
PDF
Js in the open
Victor Porof
 
PPTX
Es6 hackathon
Justin Alexander
 
PDF
The Shape of Functional Programming
Mike Fogus
 
PPTX
Academy PRO: ES2015
Binary Studio
 
PDF
Workshop 10: ECMAScript 6
Visual Engineering
 
PDF
Intro to Advanced JavaScript
ryanstout
 
PDF
CoffeeScript
Scott Leberknight
 
PDF
GDI Seattle - Intro to JavaScript Class 2
Heather Rock
 
PDF
Introduction to ECMAScript 2015
Tomasz Dziuda
 
PPT
13665449.ppt
JP Chicano
 
PPTX
Workshop 1: Good practices in JavaScript
Visual Engineering
 
PDF
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
PDF
ES2015 (ES6) Overview
hesher
 
PPTX
Week 6 java script loops
brianjihoonlee
 
PDF
The Future of JavaScript (Ajax Exp '07)
jeresig
 
PPTX
ES6(ES2015) is beautiful
monikagupta18jan
 
PPT
Hub102 - JS - Lesson3
Tiểu Hổ
 
PDF
JSDC 2014 - functional java script, why or why not
ChengHui Weng
 
Javascript
Vlad Ifrim
 
CoffeeScript
Ryan McGeary
 
Js in the open
Victor Porof
 
Es6 hackathon
Justin Alexander
 
The Shape of Functional Programming
Mike Fogus
 
Academy PRO: ES2015
Binary Studio
 
Workshop 10: ECMAScript 6
Visual Engineering
 
Intro to Advanced JavaScript
ryanstout
 
CoffeeScript
Scott Leberknight
 
GDI Seattle - Intro to JavaScript Class 2
Heather Rock
 
Introduction to ECMAScript 2015
Tomasz Dziuda
 
13665449.ppt
JP Chicano
 
Workshop 1: Good practices in JavaScript
Visual Engineering
 
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
ES2015 (ES6) Overview
hesher
 
Week 6 java script loops
brianjihoonlee
 
The Future of JavaScript (Ajax Exp '07)
jeresig
 
ES6(ES2015) is beautiful
monikagupta18jan
 
Hub102 - JS - Lesson3
Tiểu Hổ
 
JSDC 2014 - functional java script, why or why not
ChengHui Weng
 
Ad

More from Event Handler (8)

PDF
Selling UCD: Getting buy-in and measuring the value of UX
Event Handler
 
PDF
Best Practice for UX Deliverables - 2014
Event Handler
 
PPT
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
Event Handler
 
PPTX
Tumours and Tree Trunks - GeekyScience: Evolution
Event Handler
 
PDF
Best Practice for UX Deliverables
Event Handler
 
PPSX
The Missing Ingredient
Event Handler
 
PPTX
Productivity quickly
Event Handler
 
PPTX
Anna pickard geekyfinal
Event Handler
 
Selling UCD: Getting buy-in and measuring the value of UX
Event Handler
 
Best Practice for UX Deliverables - 2014
Event Handler
 
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
Event Handler
 
Tumours and Tree Trunks - GeekyScience: Evolution
Event Handler
 
Best Practice for UX Deliverables
Event Handler
 
The Missing Ingredient
Event Handler
 
Productivity quickly
Event Handler
 
Anna pickard geekyfinal
Event Handler
 

Recently uploaded (20)

PDF
Gabino Barbosa - A Master Of Efficiency
Gabino Barbosa
 
PDF
3rd Edition of Human Resources Management Awards
resources7371
 
PPTX
SYMCA LGP - Social Enterprise Exchange.pptx
Social Enterprise Exchange
 
PDF
Top 10 Emerging Tech Trends to Watch in 2025.pdf
marketingyourtechdig
 
PDF
Jordan Minnesota City Codes and Ordinances
Forklift Trucks in Minnesota
 
PDF
Maksym Vyshnivetskyi: Управління закупівлями (UA)
Lviv Startup Club
 
PDF
"Complete Guide to the Partner Visa 2025
Zealand Immigration
 
PPTX
Phygital & Omnichannel Retail: Navigating the Future of Seamless Shopping
RUPAL AGARWAL
 
PPTX
World First Cardiovascular & Thoracic CT Scanner
arineta37
 
PDF
GIÁO TRÌNH KINH DOANH QUỐC TẾ ĐẠI HỌC NGOẠI THƯƠNG
k622314115078
 
PDF
Buy Facebook Accounts Buy Facebook Accounts
darlaknowles49
 
DOCX
TCP Communication Flag Txzczczxcxzzxypes.docx
esso24
 
PDF
Pencil Box with Charmbox Gift Shop - CharmboxGift
Charm Box
 
PDF
FastnersFastnersFastnersFastnersFastners
mizhanw168
 
PPTX
Micro Battery Market Size & Share | Growth - 2034
Aman Bansal
 
PPTX
Business profile making an example ppt for small scales
Bindu222929
 
PDF
Your Best Year Yet​ Create a Sharp, Focused AOP for FY2026​
ChristopherVicGamuya
 
PPTX
Sustainability Strategy ESG Goals and Green Transformation Insights.pptx
presentifyai
 
DOCX
DiscoveryBit The 21st century seen.docx
seomehk
 
PDF
Agriculture Machinery PartsAgriculture Machinery Parts
mizhanw168
 
Gabino Barbosa - A Master Of Efficiency
Gabino Barbosa
 
3rd Edition of Human Resources Management Awards
resources7371
 
SYMCA LGP - Social Enterprise Exchange.pptx
Social Enterprise Exchange
 
Top 10 Emerging Tech Trends to Watch in 2025.pdf
marketingyourtechdig
 
Jordan Minnesota City Codes and Ordinances
Forklift Trucks in Minnesota
 
Maksym Vyshnivetskyi: Управління закупівлями (UA)
Lviv Startup Club
 
"Complete Guide to the Partner Visa 2025
Zealand Immigration
 
Phygital & Omnichannel Retail: Navigating the Future of Seamless Shopping
RUPAL AGARWAL
 
World First Cardiovascular & Thoracic CT Scanner
arineta37
 
GIÁO TRÌNH KINH DOANH QUỐC TẾ ĐẠI HỌC NGOẠI THƯƠNG
k622314115078
 
Buy Facebook Accounts Buy Facebook Accounts
darlaknowles49
 
TCP Communication Flag Txzczczxcxzzxypes.docx
esso24
 
Pencil Box with Charmbox Gift Shop - CharmboxGift
Charm Box
 
FastnersFastnersFastnersFastnersFastners
mizhanw168
 
Micro Battery Market Size & Share | Growth - 2034
Aman Bansal
 
Business profile making an example ppt for small scales
Bindu222929
 
Your Best Year Yet​ Create a Sharp, Focused AOP for FY2026​
ChristopherVicGamuya
 
Sustainability Strategy ESG Goals and Green Transformation Insights.pptx
presentifyai
 
DiscoveryBit The 21st century seen.docx
seomehk
 
Agriculture Machinery PartsAgriculture Machinery Parts
mizhanw168
 

An Introduction to JavaScript: Week 4