• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

给一个类增加属性和方法?看看这个够不够?

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 prototype
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)

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网