javascript实现key value对象
JavaScript的实现的Map,用着挺方便的,不知道性能怎么样。
自己用只有不超过10个元素,所以性能无所谓了。
/********************jsmap.js**************************/
/////// map 类
function classMap() {
this.map = new Array();
var struct = function(key,
value){
this.key = key;
this.value = value;
};
this.lookUp = function (key){
for (var i = 0; i <
this.map.length; i++)
{
if ( this.map[i].key === key )
{
return this.map[i].value;
}
}
return null;
};
this.setAt = function (key, value){
for (var i = 0; i
< this.map.length; i++)
{
if ( this.map[i].key === key )
{
this.map[i].value = value;
return;
}
}
this.map[this.map.length] = new struct(key,value);
};
this.removeKey = function removeKey(key){
var v;
for (var i =
0; i < this.map.length; i++)
{
v = this.map.pop();
if
( v.key === key )
continue;
this.map.unshift(v);
}
};
this.getCount = function(){
return
this.map.length;
};
this.isEmpty = function(){
return this.map.length <= 0;
};
}
////////////////////////////////////////////////////////////////////////////////////////////////
/********************调用***********************/
window.onload = function(){
var map = new classMap();
alert("is the map empty? " +
map.isEmpty());
// string to array
map.setAt("sw1", "aaaa
相关文档:
要想写出跨浏览器的javascript,就必须懂得嗅探技术。这是浏览器大战遗留下的大地雷,事已如此,只好认命,乖乖写分支结构吧,函数就是这样不知不觉中变长的。
先看单一浏览器的判断,我们没有必须去找navigator.userAgent的麻烦,我在国外的博客网站收集了如下hack,短小精悍:
ie = !+"\v1" ;
ie ='\v'=='v' ; ......
以下分别是用递归和两种循环对斐波那契数列的简单实现。(结果仅供参考)
递归的模式:
function Fibonacci(num){
if(num <= 2){
return 1;
}else{
return Fibonacci(num - 1) + Fibonacci(num - 2)
}
}
var counter1 = new Counter();
Fibonacci(30);
counter1.show()
//Firefo ......
父窗口中有三种方式打开子窗口:
1:
window.open(URL,windowName,parameters);
2:
alert(""); //弹出信息提示对话框
confirm(""); //弹出信息确认对话框
prompt(""); //具有交互性质的对话框
3:
//创建模态你对话框
window.showModalDialog(sURL,vArguments,sFeatures)
//创建非模态对话框
window.showM ......
srcElement 是Dom事件中的事件最初指派到的元素。
比如有一个div,里面有一个按钮。你响应div的onclick事件,但实际上,你单击的只是它内部的按钮,那么,srcElement指向的,就是那个按钮。
srcElement只在IE中有效。
在Opera系列浏览器中对应的属性是target
给你一个IE下使用的例子。
<!DOCTYPE html PUBLIC "-//W ......
/**
* 去除多余空格函数
* trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
* 用法:
* var str = " hello ";
* str = str.trim();
*/
String.prototype.trim = function()
{
return this.replace(/(^[\\s]*)|([\\s]*$)/g, ""); ......