Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JS
Search
nctunba
March 14, 2012
Programming
11
610
JS
nctunba
March 14, 2012
Tweet
Share
More Decks by nctunba
See All by nctunba
被 Qt 快樂的玩弄
nctunba
2
340
被 Qt 快樂的玩弄 part 1
nctunba
1
240
Node - Express - Socket.io
nctunba
1
410
Python
nctunba
3
510
node
nctunba
9
49k
jQuery
nctunba
9
600
Other Decks in Programming
See All in Programming
SIMD Parallel Programming with the Vector API
josepaumard
0
240
使ってみよう Azure AI Document Intelligence
kosmosebi
2
380
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
12
4.3k
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
150
Balkan Ruby 2024 — How and why to run SQLite on Rails in production
fractaledmind
0
110
TCAとKMPを用いた新規動画配信アプリ 「ABEMA Live」の設計
tomu28
2
130
Domain-Driven Transformation
hschwentner
2
1.5k
新宿ダンジョンを可視化してみた
satoshi7190
3
420
酒飲んでたらテックリードになった話
spbaya0141
0
160
JavaScript Closure
asoluka
0
1.5k
Going beyond Apache Parquet's default settings
xhochy
0
140
Snowflakeで眠ったデータを起こそう!
estie
0
160
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
188
16k
Become a Pro
speakerdeck
PRO
13
4.6k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
41
4.4k
jQuery: Nuts, Bolts and Bling
dougneiner
60
7.2k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
Six Lessons from altMBA
skipperchong
22
3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
Learning to Love Humans: Emotional Interface Design
aarron
267
39k
KATA
mclloyd
16
12k
The Cost Of JavaScript in 2023
addyosmani
21
3.9k
Statistics for Hackers
jakevdp
790
220k
Code Review Best Practice
trishagee
56
15k
Transcript
JavaScript
“The World's Most Misunderstood Programming Language” Douglas Crackford
A Re-introduction to JavaScript
has nothing to do with Java JavaScript
runs not only in web browsers JavaScript
History
1995 by Brendan Eich for Netscape LiveScript
None
“I had to be done in ten days or something
worse than JS would have happened.” Brendan Eich
base on Scheme LiveScript
renamed JavaScript LiveScript
Standardized ECMAScript
Language Overview
No compilation Interpreter Based
None
No class Prototypical Inheritance
except for null and undefined Object Everything
My dad is Scheme Functional Programming
“Lisp in C's Clothing” Douglas Crackford
None
Not my business Input/Output?
Variable & Types
Object Array Function null Number Boolean String undefined
Number
var a = 42;
The only number type is Number Number
double-precision 64-bit IEEE 754 Number
var a = 0.1; // 0.1 a = 010; //
8 a = 0xab; // 171
String
var a = 'hello world';
'qouted like this' ”double qouted” String
'string'.length // 6 'string'[2] // 'r' 'hello'.substr(0, 2) // 'he'
'hello' + 'world' // 'helloworld' 'string' + 2 // 'string2'
var a = 42; a = 'hello world';
JavaScript is loosely typed values are typed, variables are not
Boolean
var a = true;
true & false Boolean
'123' == 123 // true '123' === 123 // false
Content equality ==
Object identity ===
Null
var a = null; // null
Nothing Exists but nothing Null
Undefined
var a; // undefined
Not even nothing Undefined
var a; // undefined a = 5; // 5
Everything in JavaScript is undefined Until you assign something to
it
[ ] Array
var a = [4, 5, 6];
Just a list Array
var a = [4, true, 'hello']; a[0] // 4 a[3]
// undefined
var a = [4, 5, [6, 7]]; a[2] // [6,
7] a.push(7) // a → [4, 5, [6, 7], 7]
var a = [4, 5, , 6]; a.length // 4
a.toString() // '4,5,undefined,6'
{ } Object
var a = {};
Hash table Object
var bob = { name: 'bob', Job: 'teacher', age: 36
};
Name-value pair { name: value } Object
var bob = { name: 'bob' };
dynamic Object
var bob = { name: 'bob' }; bob.hair = 'brown';
bob['hair'] = 'brown';
bob.hair; // 'brown' bob['name']; // 'Bob' Bob.banana; // undefined
delete bob.hair; delete bob['name']; // bob → {};
var alice = { name: 'alice', pet: { cat: 'bob',
fish: ['a', 'b'] } }; alice.pet.fish[0] // 'a'
Now, what've we got?
JSON JS Object Notation
lightweight text-based human-readable
var alice = { name: 'alice', pet: { cat: 'bob',
fish: ['a', 'b'] } };
None
Function
var lambda = function () {};
value Function
var a = function () {}, b = 5, c
= function () { // ... };
Invoke using () Function
var five = function () { return 5; }; five;
// undefined five(); // 5
Always return Function
var void = function () { // ... }; var
a = void(); // a → undefined
Higher-order (a.k.a fantastic) Function
var chef = function (recipe, ingr) { return recipe(ingr); };
var pie = function (ingr) { return ingr + ' pie'; }; chef(pie, 'apple'); // 'apple pie'
Mom, I'm passing FUNCTIONS!!
var grandma = function () { return function () {
Return 'lol'; }; }; grandma()(); // 'lol'
Closure Function
var add = function (a, b) { return a +
b; };
var addHalf = function (a) { return function (b) {
return a + b; }; }; var add4 = addHalf(4); add4(5); // 9;
Arguments Function
var addFirstThree = function () { return arguments[0] + arguments[1]
+ arguments[2]; }; Func(1, 2, 3, 2, 3); // 6;
Length Function
var func = function (a, b) { // ... };
func.length; // 2
Control Statements
C-like ; )
Scoping
var a = 2; // a → 2
var a = 2; if () { // a →
2 } // a → 2
if () { var a = 2; // a →
2 } // a → 2
var a = 2; // a → 2
No block scope
Only Functions can make scopes
var func = function () { var a = 2;
} // a → undefined
var func = function () { var a = 2;
} // a → undefined
Local variables Var
var a = 2; // a :: Local (in Global)
a = 2; // a :: Global
var func = function () { var a = 2;
// a :: Local };
var func = function () { a = 2; //
a :: Global };
Global is bad
Never drop your var Always use them
Scope Chains
Function var Closure var Global
var addHalf = function (a) { return function (b) {
return a + b; }; }; var add4 = addHalf(4); add4(5); // 9;
Constructor Function
Function as Constructor
var Cat = function () { this.name = 'Bob'; };
var bob = new Cat; Bob.name; // 'Bob'
What is this ?
var Cat = function () { this; // Cat };
var foo = { name: 'bob', miaou: function () {
this; // foo } };
var Cat = function (name) { this.name = name ||
'Bob'; }; var bob = new Cat, alice = new Cat('Alice'); bob.name; // 'Bob' alice.name; // 'Alice'
var Cat = function () { this.name = 'Bob'; this.miaou
= function () { return this.name }; }; var bob = new Cat; bob.miaou(); // 'Bob'
var Cat = function () { this.name = 'Bob'; };
Cat.prototype.miaou = function () { return this.name }; var bob = new Cat; bob.miaou(); // 'Bob'
An attribute of every object Prototype
mom Prototype
ancenstors Prototype Chain
me mom grandma
Q & A