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
07 - Javascript - OpenWebSchool
Search
openwebschool
August 16, 2012
Programming
3
340
07 - Javascript - OpenWebSchool
openwebschool
August 16, 2012
Tweet
Share
More Decks by openwebschool
See All by openwebschool
11 - CodeIgniter - OpenWebSchool
openwebschool
1
340
09 - Node.JS - OpenWebSchool
openwebschool
1
390
08 - js frontend & jQuery - OpenWebSchool
openwebschool
3
280
05 - MySQL - OpenWebSchool
openwebschool
1
250
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
270
03 - PHP II - OpenWebSchool
openwebschool
2
390
04 - CSS - OpenWebSchool
openwebschool
4
360
01 - W3 intro - OpenWebSchool
openwebschool
3
250
02 - PHP I - OpenWebSchool
openwebschool
3
270
Other Decks in Programming
See All in Programming
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.4k
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
810
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
310
Deep Dive into ~/.claude/projects
hiragram
9
1.6k
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
100
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
160
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
860
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
47
31k
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
210
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
240
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
150
VS Code Update for GitHub Copilot
74th
1
410
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Writing Fast Ruby
sferik
628
61k
Docker and Python
trallard
44
3.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
KATA
mclloyd
29
14k
The World Runs on Bad Software
bkeepers
PRO
69
11k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
How to Ace a Technical Interview
jacobian
277
23k
Transcript
JavaScript
Brendan Eich
“I had to be done in ten days or something
worse than JS would have happened”
Netscape 找他來幫網頁弄個程式語言 1995
Netscape 當時與 Sun 合作 從 LiveScript 更名為 JavaScript 1995
跟 Java 一點關係都沒有
送交 ECMA 國際 標準化成 ECMAScript 1996
直譯式語言 但現在速度已經不輸編譯式語言 :P
環境獨立 語言規範沒有輸入輸出 由執行環境提供
物件導向 原型繼承 不是用 Class 的意思
Lisp in C's Clothing 骨子裡是函數式語言,他爸是 Scheme
變數
var a; // undefined
undefined 未初始化
var a = 4; // 4 a = banana; //
undefined a = 6; // 6
var a = 4; // 4 a = ‘hello’; //
‘hello’
動態型別 擁有型別的是值,不是變數
型別
Array Object Function Number String Boolean
數字
42
64 位元雙精浮點數
運算子都跟 C 的一樣的樣子
Math 野生的物件
Math.sin(0); // 0 Math.round(1.5); // 2 Math.random(); // 0.11533094733022153 Math.PI;
// 3.14159265....
parseInt parseFloat 少數幾個野生的內建函式
parseInt(‘5’, 10); // 5 parseInt(‘hello’, 10); // NaN
NaN Not a Number
字串
‘Hello’ “Hello”
‘hello’ + ‘ world’ // ‘hello world’ ‘hello’ + 42
// ‘hello42’ 42 + ‘hello’ // ‘42hello’
‘hello’.length // 5 ‘hello’.toUpperCase() // ‘HELLO’ ‘hello’.substring(1, 3) // ‘el’
布林
true false
42 == 42 // true 42 != 42 // false
42 == ’42’ // true
Equality Operators 內容相等就好 :P
NaN == NaN // false
Identity Operators 連型別都要相等
42 === 42 // true 42 === ’42’ // false
列表
[42, 5, 23]
[42, true, ‘hello’]
[1, 2, 3, 4].length // 4 [1, 2, 3, 4][0]
// 1 [1, 2, 3, 4][99] // undefined
var a = [1, 2, 3, 4]; a[2] = ‘banana’;
// [1, ‘banana’, 3, 4]
var a = [1, 2, 3, 4]; typeof a; //
‘object’ Array.isArray(a); // true
var a = [1, 5, 2, 4].pop(); // 4 //
a === [1, 5, 2] a.sort(); // [1, 2, 5]
物件
{ name: ‘Bob’ }
{ name: ‘Bob’, age: 4 }
{ name: ‘Bob’, age: 4 }.name; // ‘Bob’ { name:
‘Bob’, age: 4 }[‘name’]; // ‘Bob’ { name: ‘Bob’, age: 4 }.banana; // undefined
var obj = { name: ‘Bob’, age: 4 }; obj.name
= ‘Alice’; // obj === { name: ‘Alice’, age: 4 } obj[‘name’] = ‘Alice’; // obj === { name: ‘Alice’, age: 4 }
var obj = { name: ‘Bob’, age: 4 }; delete
obj.name; // obj === { age: 4 }
var obj = { name: ‘Bob’, age: 4 }; delete
obj.name; // obj === { age: 4 }
Null 沒有東西存在的意思
typeof null; // ‘object’
JSON
JavaScript Object Notaion
目前最流行的資料交換格式之一
Douglas Crockford
{ ‘name’: ‘Bob’, ’age’: 4, ‘pet’: { ‘dog’: [‘Bob’, ‘Cob’],
‘cat’: [‘Dub’] }, ‘dead’: false }
函數
function (a) { return a; }
var f = function () {};
函數是一種值 函數式語言優良血統
var two = function () { return 2; }; two;
// function () { return 2; } two(); // 2
var cakeRecipe = function (ingredient) { return ingredient + ‘
cake’; }; var cookieRecipe = function (ingredient) { return ingredient + ‘ cookie’; }; cookieRecipe(‘chocolate’); // chocolate cookie
var cook = function (recipe, ingredient) { return recipe(ingredient); };
cook(cakeRecipe, ‘chocolate’); // chocolate cake cook(cookieRecipe, ‘chocolate’); // chocolate cookie
cook(function (ingredient) { return ingredient + ‘ icecream’ }, ‘banana’);
// banana icecream
var parser = function (base) { return function (x) {
return parseInt(x, base); }; }; var parseBin = parser(2); parseBin(‘1010’); // 10
var f = function () { return arguments[1]; }; f(2,
3, true, { foo: ‘bar’ }); // 3
流程控制
好像跟 C 一樣呵呵
for (var i = 0; i < 5; i++) {
// 做五次 }
var obj = { name: ‘Bob’, age: 4 }; ‘name’
in obj; // true ‘banana’ in obj; // false
for (var prop in obj) { return obj.prop; } //
‘Bob’, 42
Scope
和 C 不一樣的地方在於
JS 的大括號不會形成 scope 只有函數的才會形成 scope
if (true) { var a = 4; } a; //
4
var f = function () { var a = 4;
}; f(); a; // undefined
列表迭代 A_A
var a = [1, 2, 3]; for (var i =
0, len = a.length; i < len; i++) { a[i]; // 1, 2, 3 ... }
var a = [1, 2, 3]; a.forEach(function (n, i, array)
{ n; // 1, 2, 3; });
var a = [1, 2, 3]; var b = a.map(function
(n) { return n * 2; }); b; // [2, 4, 6]
var b = [1, 2, 3].filter(function (n) { return n
< 3; }); b; // [1, 2]
var add = function (a, b) { return a +
b; }; [1, 2, 3].reduce(add, 0); // 6