Re: [問題] 推薦的台北捷運沿線網頁書書店

看板Ajax作者 (葉酸酸)時間14年前 (2012/02/21 00:37), 編輯推噓3(300)
留言3則, 3人參與, 最新討論串3/3 (看更多)
現代在談 javascript 泛指 ecmascript 與及 browser implementation。 ecmascript (1997) 是 ecma 整合 javascript (1995) 與 jscript (1996) 的結果。 就像 c 語言有 c90 與 c99 兩種版本一樣,ecmascript 也有許多版本, 流通性比較高,較多 browser implement 的是 3 版 (1999),最新的是 5.1 (2011)。 我覺得 ecmascript 比較可以談的是: - function ecmascript 的 function 是 first class object。 - 可以動態產生 - 可以 pass 進 function - 可以 return - 可以存到 variable 這些是 function 很重要的特性。另外在 function context 裡面, 有兩個特別的變數: - arguments arguments object 有 function arguments 的資料, 所有傳進 function 的 parameters 都在 arguments 裡面, arguments 本身是一個 array like object, 可以用 arguments[index] 取出位於 index 的 argument, 也有 arguments.length 可以知道拿到幾個 arguments, arguments.lentgh 就像是 c main() 的 argc,arguments 像是 argv。 順便提一下 arguments.callee 這個 property (5.1 deprecated), 這個 property 指向這個 function 本身,可以用在某些 tricky 的時刻。 - this 這個 object 指向 owner object,例如 Date.parse() 執行的時候, 在 parse 這個 function 的 context,this 指的就是 Date 這個 object, 那如果沒有 owner object 的話,this 指的是 global object (5.1 modified), 例如 parseInt() 執行的時候 this 就是指 global object。 function object 本身有兩個跟 this 有關的相似 methods,call 跟 apply, 這兩個 methods 都用來執行 function。那 parseInt() 跟 parseInt.call() 有什麼不同?差別在 call 的第一個 parameter 表示在 function context 裡面, this 是什麼東西,例如 parseInt.call(Date) 表示在 parseInt 的 context, this 會是 Date 這個 object。call 的 parameters 從第二個開始變 arguments。 call 跟 apply 很相似,差別是 apply 只接受兩個 parameters,第一個一樣是 this,第二個是一個 array like object,會直接變成 arguments。 apply 這個 method 可以應用在一些 tricky 的地方,例如:Math.max()。 Math.max method 接受 0 到 n 個 parameters,傳回其中最大的數字。 例如 Math.max(1, 2, 3, 4) 結果是 4。假設我有一個 array k [1, 2, 3, 4], 我想找出最大的數字,這時候就可以寫 Math.max.apply(Math, k), 等同於 Math.max(1, 2, 3, 4)。 另一個 apply 的用途是用來原封不動把 arguments 送給另一個 function。 例如 function bounds() { var bounds = { lower: Math.min.apply(Math, arguments), upper: Math.max.apply(Math, arguments) }; return bounds; } 其他我累了,改天再說 囧" - scope - closure - object - prototype -- Oni devas ami animalojn. Ili estas tiel bongustaj. One should love animals. They are so tasty. 每個人都應該愛動物,他們是如此美味。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 175.180.46.115

02/21 00:42, , 1F
推專業
02/21 00:42, 1F

02/21 01:19, , 2F
推 精闢 都講到重點 而且是我看過最好懂的解說
02/21 01:19, 2F

03/01 01:08, , 3F
推!! 語言的核心~
03/01 01:08, 3F
文章代碼(AID): #1FGdRNaY (Ajax)
文章代碼(AID): #1FGdRNaY (Ajax)