Alex刺客

Dancing fingers, damage world. -- 舞动手指,破坏世界.

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  57 随笔 :: 0 文章 :: 76 评论 :: 0 Trackbacks
 1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2<html xmlns="http://www.w3.org/1999/xhtml">
 3    <head>
 4        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5        <title>修改对象:创建新方法</title>
 6        <script type="text/javascript">
 7            /*
 8            *    项目: book -> Javascript高级程序设计.pdf -> 第3章 -> 3.6 修改对象
 9            *
10            *    说明:每个构造函数都有一个prototype属性,可用于定义方法,而在ECMAScript中,每个本地对象也有一个用法完全相同的prototype属性。
11            *            
12            *    练习者: Alex刺客
13            *
14            *    日期: 2009-12-13
15            */

16            
17            /*
18                可用prototype属性为任何已有的类定义新方法,就像处理自己的类一样。
19                例如以下为Number类新增一个toHexString()方法。
20            */

21            Number.prototype.toHexString = function(){
22                return this.toString(16);
23            }

24            
25            var iNumber = 11;
26            
27            alert(iNumber.toHexString());
28            
29            /*
30                Array类新增一个检索匹配数组的值
31            */

32            
33            Array.prototype.indexOf = function (vItem){
34                for (var i=0; i<this.length; i++{
35                    if(this[i] == vItem) {
36                        return i;
37                    }

38                }

39                return -1;
40            }

41            
42            var aColors = new Array("red","green","yellow");
43            alert(aColors.indexOf("green"));
44        </script>
45    </head>
46    <body>
47    </body>
48</html>
posted on 2009-12-13 23:00 Alex刺客 阅读(195) 评论(0)  编辑  收藏 所属分类: JavaScript

只有注册用户登录后才能发表评论。


网站导航: