- 热门文章:
- · 在网页中实现OICQ里的头像选择的下拉框 (附例子)
- · 请看用javascript设置和读取cookie的简单例子.....
- · 请看被打开的子窗口继承父窗口定义的styleSheets的例子
- · 经常有人询问如何用javascript判断日期是否有效,我以前也遇到过,不过后来得高人指点解决了,贴出来大...
- · ShowModalDialog的具体用法
- · 下拉式互动列表框(EC潮流网同学录之真情留言板使用的代码)
- · MSGBOX返回值
- · js中几种去掉字串左右空格的方法,请看
- · VBSctipt 5.0中的新特性
- · Jscript 5.0中的新特性
- · 加快 DHTML 的一组技巧(Copy from Microsoft)
- · 一个不太让人讨厌的自动弹出窗口:)
上一篇:三级下拉框连动的数据库版! >>
给一个类增加属性和方法?看看这个够不够?
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype.Property of
String
Implemented in
JavaScript 1.1, NES 3.0
ECMA version
ECMA-262
prototype
A value from which instances of a particular class are created. Every object that can be created by calling a constructor function has an associated prototype property.Property of
Function
Implemented in
JavaScript 1.1, NES 2.0
ECMA version
ECMA-262
Description
You can add new properties or methods to an existing class by adding them to the prototype associated with the constructor function for that class. The syntax for adding a new property or method is:
fun.prototype.name = value
wherefun
The name of the constructor function object you want to change.
name
The name of the property or method to be created.
value
The value initially assigned to the new property or method.
If you add a property to the prototype for an object, then all objects created with that object@#s constructor function will have that new property, even if the objects existed before you created the new property. For example, assume you have the following statements:
var array1 = new Array();var array2 = new Array(3);Array.prototype.description=null;array1.description="Contains some stuff"array2.description="Contains other stuff"
After you set a property for the prototype, all subsequent objects created with Array will have the property:
anotherArray=new Array()anotherArray.description="Currently empty"
Example
The following example creates a method, str_rep, and uses the statement String.prototype.rep = str_rep to add the method to all String objects. All objects created with new String() then have that method, even objects already created. The example then creates an alternate method and adds that to one of the String objects using the statement s1.rep = fake_rep. The str_rep method of the remaining String objects is not altered.
var s1 = new String("a")var s2 = new String("b")var s3 = new String("c")
// Create a repeat-string-N-times method for all String objectsfunction str_rep(n) { var s = "", t = this.toString() while (--n >= 0) s += t return s}
String.prototype.rep = str_rep
s1a=s1.rep(3) // returns "aaa"s2a=s2.rep(5) // returns "bbbbb"s3a=s3.rep(2) // returns "cc"
// Create an alternate method and assign it to only one String variablefunction fake_rep(n) { return "repeat " + this + " " + n + " times."}
s1.rep = fake_reps1b=s1.rep(1) // returns "repeat a 1 times."s2b=s2.rep(4) // returns "bbbb"s3b=s3.rep(6) // returns "cccccc"
The function in this example also works on String objects not created with the String constructor. The following code returns "zzz".
"z".rep(3)
下一篇:在网页中实现OICQ里的头像选择的下拉框 (附例子) >>
相关文章:
- · 一个把数字转英文的实用程序
- · rollarea.js及其用法示例
- · 下拉框连动的小例子(.htm版)
- · 判断访问者的浏览器是否支持javascript和Cookies
- · 在Windows桌面上使用WSH接收邮件 (转)
- · HTML4.0的 Access Key
- · 绝对是好东西 (select1 <==> select2):
- · 一个类似vbscript的round函数的javascript函数
- · 用javascript检查yyyy-mm-dd格式的正确源码。
- · HEAD元素使用集锦 (转)
- · 脚本控制Frame (转)
- · vbscript错误代码及对应解释大全
- · jscript错误代码及相应解释大全
- · 打开最大化窗口的一点经验
- · 无偿贡献,进入页面后自动刷新一次
- · 庆祝 Joy ASP 上贴数超过800页!!! 送给大家一个小礼物 ^_^
- · 自己动手,结合javascript和dhtml做一个ubb编辑器(附例子代码)
- · 选择最快的镜像站点
- · 一段有趣并且实用的程序--利用javascript和dhtml实现两个列表框中内容的移动。(代码见内,把它存为一个...
- · 动态菜单的另一种实现(一) category.js
- · 动态数组的另一种实现(二) 界面
- · 实际使用“DXML”:在站点上实现 DHTML 菜单和目录(co.)
- · 以前收集的一些资料---JS中处理日期的一些函数和方法
- · 以前搜集的一些资料---html中的特殊字符(2)
- · 以前搜集的一些资料---html中的特殊字符(1)
- · 在浏览器里实现类似VB Form的界面控制
- · CSS2参考之一(转贴)
- · CSS2参考之二(转贴)
- · CSS2参考之三(转贴)
- · CSS2参考之四(转贴)
- · CSS2参考之五(转贴)
- · CSS2参考之六(转贴)
- · CSS2参考之七(转贴)
- · CSS2参考之八(转贴)
- · CSS2参考之九(转贴)
- · CSS2参考之十一(转贴)<完>
- · CSS2参考之十(转贴)
- · 李由的奉献(一)----Windows Scripting Host 对象参考
