作者B9 (叶酸酸)
看板Ajax
标题Re: [问题] 推荐的台北捷运沿线网页书书店
时间Tue Feb 21 00:37:39 2012
现代在谈 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
1F:推 LaPass:推专业 02/21 00:42
2F:推 tomin:推 精辟 都讲到重点 而且是我看过最好懂的解说 02/21 01:19
3F:推 gauss02:推!! 语言的核心~ 03/01 01:08