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
630
JS
nctunba
March 14, 2012
Tweet
Share
More Decks by nctunba
See All by nctunba
被 Qt 快樂的玩弄
nctunba
2
370
被 Qt 快樂的玩弄 part 1
nctunba
1
260
Node - Express - Socket.io
nctunba
1
410
Python
nctunba
3
520
node
nctunba
9
49k
jQuery
nctunba
9
600
Other Decks in Programming
See All in Programming
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
2
490
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
840
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
130
快速入門可觀測性
blueswen
0
450
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
380
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
120
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
270
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
890
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
290
iOS開発におけるCopilot For XcodeとCode Completion / copilot for xcode
fuyan777
1
700
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
120
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
210
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
Designing Experiences People Love
moore
139
23k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
Visualization
eitanlees
146
15k
Why Our Code Smells
bkeepers
PRO
335
57k
Building Your Own Lightsaber
phodgson
103
6.1k
Making the Leap to Tech Lead
cromwellryan
133
9k
Automating Front-end Workflow
addyosmani
1366
200k
The World Runs on Bad Software
bkeepers
PRO
66
11k
A designer walks into a library…
pauljervisheath
205
24k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
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