Reply to comment

Functional JavaScript

บังเอิญไปเจอ Blog ของ Oliver Steele ซึ่งเค้าไปทำ Functional JavaScript ไว้ ลองมาดูตัวอย่างที่เค้าทำให้ดูละกัน

map(function(x){return x+1}, [1,2,3])
-> [2,3,4]
 
filter(function(x){return x>2}, [1,2,3,4]]
-> [3,4]
 
some(function(w){return w.length < 3}, 'are there any short words?'.split(' '))
-> false
 
// double the items in a list:
map('*2', [1,2,3])
-> [2, 4, 6]
 
// find just the odd numbers:
filter('%2', [1,2,3,4])
-> [1, 3]
 
// or just the evens:
filter(not('%2'), [1,2,3,4])
-> [2, 4]

ทั้งนี้เค้ายังได้ทำ API documentation ไว้ให้อ่านและศึกษากันด้วย

No votes yet

Reply

The content of this field is kept private and will not be shown publicly.