SlideShare a Scribd company logo
JavaScript
Java is to JavaScript as ham is to hamster
• The basic language
• The good parts
• The bad parts
• The salvation
The basic language
     Do what I mean
alert(”Hello world”);
function fib(n) {
    if (n == 1 || n == 2) {
        return 1;
    }
    return fib(n-1) + fib(n-2);
}
alert(fib(100));
var burt = new Person();
burt.age = 2;
burt.father = ”Gustav”;
try {
    var input = prompt();
    if (input <= 0) {
        throw ”Natural plz”;
    }
    alert(fib(input));
} catch (ex) {
    alert(”Doh: ” + ex);
}
var s1 = ”a ‘quoted’ string”;
var s2 = ’a ”quoted” string’;
var s3 = ”a ‘quoted” string”;
var a1 = [2, 3, 5, 7, 11];
var a2 = [”foo”, ”bar”];
var a3 = [2, ”foo”, null];
var p = /^[A-Z][a-z]*$/;
alert(”Jakob”.match(p));
alert(”iPhone”.match(p));
The good parts
First-class functions, object literals, closures, prototyping
                  and dynamic arguments
First-class functions
var burt = new Person();
burt.age = 2;
burt.father = ”Gustav”;
burt.tellAge = function() {
    alert(”Age: ” + this.age);
}
burt.tellAge();
burt.age = 3;
burt.tellAge();
var p = [2, 3, 5, 7, 11];
alert(p.filter(function(x) {
  return x & 1 == 0;
}));
get(”jsonip.com”, function(x) {
  alert(x);
});
Object literals
var burt = {
    age: 2,
    father: ”Gustav”,
    tellAge: function() {
      alert(”Age: ” + this.age);
    },
    employees: [”Jakob”, ... ]
}
var whichOne = prompt();
alert(burt[whichOne]);
for (var property in burt) {
    alert(property);
    alert(burt[property]);
}
burt.lookMom = ”a new value!”;
alert(Object.keys(burt));
alert(Object.values(burt));
var json = {
    ”xml”: ”no”,
    ”json”: true,
    ”numbers”: [1, 2, 3],
    ”functions”: false,
    ”recursive”: {
        ”guess”: ”Hell yeah!”
    }
}
Closures
class Counter {
    private int x = 0;
    public void count() {
        return x++;
    }
}
var c1 = new Counter();
var c2 = new Counter();
var a = [c1.count(),
         c1.count(),
         c2.count()];
alert(a);
function counter() {
    var x = 0;
    var f = function() {
        return x++;
    }
    return f;
}
var c1 = counter();
var c2 = counter();
var a = [c1(), c1(), c2()];
alert(a);
• C lets us forget stacks, heaps and jumps
• Java lets us forget memory management
• JavaScript lets us forget creating state
Prototyping
function Animal(name) {
    this.name = name;
    this.talk = function() {
        alert(this.name + ”: huh?”);
    }
}
var a = new Animal(”garfield”);
var b = new Animal(”lassie”);
alert(a.name);
alert(b.talk());
function Animal(name) {
    this.name = name;
}
Animal.prototype.talk = f() {
    alert(this.name + ”: huh?”);
}
var a = new Animal(”garfield”);
var b = new Animal(”lassie”);
alert(a.name);
alert(b.talk());
function clone(o) {
    function F() {}
    F.prototype = o;
    return new F();
}
var animal = { };
animal.talk = function() {
    alert(this.name + ": huh?");
};
var cat = clone(animal);
cat.talk = function() {
 alert(this.name + ": meow!");
}
var a = clone(cat);
a.name = "garfield";
a.talk();
The bad parts
  The bad and the ugly
IE6
The DOM
eval
var sum = eval(”1 + 4”);
eval(”var x = 10”);
eval(”alert(x + sum)”);
• Opens up the code for injection attacks
• Debugging becomes hell
• Slow execution
Tricky truthiness
if (x) {
    alert(”when?”);
}
• Not null
• Not undefined
• Not false
• Not 0
• Not the empty string
Missing ”var”
// Style #1
function foobar() {
    x = 10;
}
// Style #2
function foobar() {
    var x = 10;
}
// Style #3
var x;
function foobar() {
    x = 10;
}
fs.readFileSync(
  sourceDir + "/" + files[i],
  encoding = "utf8"
);
Silly semicolon
    insertion
function foobar() {
    var x = 1 + 2
    return x
}
function foobar() {
    var x = 1 + 2;
    return
    x;
}
with
with(burt) {
    alert(age);
    alert(father);
}
with(burt) {
    age = 3;
    foo = ”bar”;
}
parseInt
var a = parseInt(”123”);
var b = parseInt(”110”, 2);
var c = parseInt(”0123”);
Everything is an object
  (except when parsing numbers)
alert(x.toString());
alert(1.toString());
alert(1.0.toString());
alert(1..toString());
alert((1).toString());
The salvation
Good coding practices, common sense & static analysis

More Related Content

What's hot (20)

ZIP
Intro to Pig UDF
Chris Wilkes
 
PDF
Bytes in the Machine: Inside the CPython interpreter
akaptur
 
PPTX
Introduzione a C#
Lorenz Cuno Klopfenstein
 
PDF
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
akaptur
 
PPTX
TCO in Python via bytecode manipulation.
lnikolaeva
 
PDF
Powered by Python - PyCon Germany 2016
Steffen Wenz
 
PDF
Damn Fine CoffeeScript
niklal
 
PDF
Building fast interpreters in Rust
Ingvar Stepanyan
 
PDF
Brief intro to clojure
Roy Rutto
 
PDF
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
PPTX
Android Guava
丞廷 鄭
 
PDF
响应式编程及框架
jeffz
 
KEY
Code as data as code.
Mike Fogus
 
ODP
Naïveté vs. Experience
Mike Fogus
 
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Wanbok Choi
 
PDF
How to write rust instead of c and get away with it
Flavien Raynaud
 
PPTX
Python GC
delimitry
 
PDF
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
PPTX
Es6 hackathon
Justin Alexander
 
PDF
Hammurabi
Mario Fusco
 
Intro to Pig UDF
Chris Wilkes
 
Bytes in the Machine: Inside the CPython interpreter
akaptur
 
Introduzione a C#
Lorenz Cuno Klopfenstein
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
akaptur
 
TCO in Python via bytecode manipulation.
lnikolaeva
 
Powered by Python - PyCon Germany 2016
Steffen Wenz
 
Damn Fine CoffeeScript
niklal
 
Building fast interpreters in Rust
Ingvar Stepanyan
 
Brief intro to clojure
Roy Rutto
 
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
Android Guava
丞廷 鄭
 
响应式编程及框架
jeffz
 
Code as data as code.
Mike Fogus
 
Naïveté vs. Experience
Mike Fogus
 
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Wanbok Choi
 
How to write rust instead of c and get away with it
Flavien Raynaud
 
Python GC
delimitry
 
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
Es6 hackathon
Justin Alexander
 
Hammurabi
Mario Fusco
 

Similar to JavaScript @ CTK (20)

PDF
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
PDF
ES2015 New Features
Giacomo Zinetti
 
PDF
Nik Graf - Get started with Reason and ReasonReact
OdessaJS Conf
 
KEY
ddd+scala
潤一 加藤
 
PDF
Code examples javascript ebook
Laurence Svekis ✔
 
PDF
An Introduction to Scala (2014)
William Narmontas
 
PDF
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
PDF
JavaScript for PHP developers
Stoyan Stefanov
 
PDF
From Javascript To Haskell
ujihisa
 
PPT
OO JS for AS3 Devs
Jason Hanson
 
PPT
JavaScript Functions
Brian Moschel
 
PPT
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
PPTX
Benefits of Kotlin
Benjamin Waye
 
PDF
CoffeeScript
None
 
PPTX
Start Writing Groovy
Evgeny Goldin
 
PDF
Introduction to Scala
Aleksandar Prokopec
 
PPT
SDC - Einführung in Scala
Christian Baranowski
 
DOC
Jsphp 110312161301-phpapp02
Seri Moth
 
PDF
On Functional Programming - A Clojurian Perspective
looselytyped
 
PDF
Miracle of std lib
Jedsada Tiwongvokul
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
ES2015 New Features
Giacomo Zinetti
 
Nik Graf - Get started with Reason and ReasonReact
OdessaJS Conf
 
ddd+scala
潤一 加藤
 
Code examples javascript ebook
Laurence Svekis ✔
 
An Introduction to Scala (2014)
William Narmontas
 
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
JavaScript for PHP developers
Stoyan Stefanov
 
From Javascript To Haskell
ujihisa
 
OO JS for AS3 Devs
Jason Hanson
 
JavaScript Functions
Brian Moschel
 
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Benefits of Kotlin
Benjamin Waye
 
CoffeeScript
None
 
Start Writing Groovy
Evgeny Goldin
 
Introduction to Scala
Aleksandar Prokopec
 
SDC - Einführung in Scala
Christian Baranowski
 
Jsphp 110312161301-phpapp02
Seri Moth
 
On Functional Programming - A Clojurian Perspective
looselytyped
 
Miracle of std lib
Jedsada Tiwongvokul
 
Ad

Recently uploaded (20)

PDF
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Are there government-backed agri-software initiatives in Limerick.pdf
giselawagner2
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
Français Patch Tuesday - Juillet
Ivanti
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Are there government-backed agri-software initiatives in Limerick.pdf
giselawagner2
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Ad

JavaScript @ CTK

Editor's Notes