Ajax 板


LINE

现代在谈 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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP