JS prototype方法应用
来源:互联网 发布:拉里约翰逊数据 编辑:程序博客网 时间:2024/06/05 16:26
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Property </TITLE>
<script langugage="javaScript">
/**********************************************HashMap*************************************************************/
/**
*定义一个Map 对象
*/
function HashMap(){
this.length=0;
this.prefix = "hashmap_prefix_20050524_";
}
/**
* 向HashMap中添加键值对
*/
HashMap.prototype.put = function (key, value)
{
this[this.prefix + key] = value;
this.length ++;
}
/**
* 向HashMap中通过键值取到对应的值
*/
HashMap.prototype.get = function(key)
{
return typeof this[this.prefix + key] == "undefined"
? null : this[this.prefix + key];
}
/**
* 判断HashMap是否存在某个key
*/
HashMap.prototype.containsKey = function(key)
{
for(var strKey in this)
{
if(strKey == this.prefix + key)
return true;
}
return false;
}
/**
* 判断HashMap是否存在某个value
*/
HashMap.prototype.containsValue = function(value)
{
for(var strKey in this)
{
if(this[strKey] == value)
return true;
}
return false;
}
//调用方法
function mapMethod(){
var map = new HashMap();
map.put("key1",0001);
map.put("key2",0002);
map.put("key3",0003);
map.put("key4",0004);
var key1 = new String("key1");
var key8 = new String("key8");
alert(map.containsKey(key1));
alert(map.containsKey(key8));
alert(map.get("key1"));
alert(map.get("key8"));
}
/*************************************************Number*******************************************************/
/**
* Number 对象的加法运算
*/
Number.prototype.add = function(num){
return(this+num);
}
/**
* Number 对象的减法运算
*/
Number.prototype.sub = function(num){
return (this-num);
}
/**
* Number 对象的乘法运算
*/
Number.prototype.multi = function(num){
return (this*num);
}
/**
* Number 对象的除法运算
*/
Number.prototype.divi = function(num){
if(num == 0){
return ("被除数分母不能为0");
} else {
return (this/num);
}
}
//调用方法
function numMethod(){
var count = new Number(12);
alert(count.add(23));
alert(count.sub(342));
alert(count.multi(0));
alert(count.divi(0));
}
</script>
</HEAD>
<BODY>
<table cellpadding=0 cellspacing=0 border=0>
<tr><td>操作Map: </td><td><input type="button" value="operatorMap" onClick="mapMethod();"/></td></tr>
<tr><td>操作Number: </td><td><input type="button" value="operatorNum" onClick="numMethod();"/></td></tr>
</table>
</table
</BODY>
</HTML>
- JS prototype方法应用
- js中prototype方法
- 【js】Array.prototype.reduce方法
- javascript中apply方法和call方法的作用以及prototype.js中的应用
- 深入理解js的prototype以及prototype的一些应用
- prototype.js Request的简单应用
- Ajax应用(prototype.js为框架)
- prototype.js日常应用函数积累
- prototype.js中Function.prototype.bind方法浅解
- prototype.js中Function.prototype.bind方法浅解
- 使用prototype.js的事件处理方法
- prototype.js常用函数 及其方法
- js Number.prototype 对象 设置一些方法
- prototype、js中的三种方法、call
- prototype.js中的class.create()方法
- 基于prototype.js验证框架(validation.js)的三个应用
- prototype.js
- Prototype.Js
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- 爬虫之事
- 图片旋转处理js
- Windows Mobile 网络连接管理
- js设置图片或文字自动左右滚动控制,鼠标经过停留
- JS prototype方法应用
- 在shell中用su命令
- 圆角头像的制作
- lucene之旅(一)
- linux删除文件命令
- sql数据库
- 测试C#
- crontab 调度程序
- 如何做网站的策划和推广