唯独从大局看来,那样会促成出现局地让我们难以掌握控制的意况的产出:变量同名、多少个函数共用三个全局变量后的值的改变……等等。所以,有的时候候,对于部分粗略的全局变量,大家能够透过另一种艺术来管理——用自推行函数+闭包的措施来解:

一.系统预订义的动画片函数

修正hashtableobj.set(“length”,”0″) bug
可以安装key忽略大小写
可以clone hashtable对象
能够 使用obj.valueOf(“key”,”defalutvalue”) 设置暗中同意值等等
接待改进bug

比如:大家要在网页加载时交由贰个提醒,在网页关闭时提交另贰个唤起
上边包车型地铁代码,完毕了上述成效

1.出示遮盖函数
show(); //彰显元素(同有时间扩张宽、高、不光滑度)
hide(); //遮盖元素
推行hide()遮蔽分界面成分,也等于将css样式中的display:none.

复制代码 代码如下:

复制代码 代码如下:

咱俩也足以在函数中投入参数,具体如下:
show(“slow”);
除开slow取值外,还足以设置为normal,fast,分别表示时间为600,400,200纳秒

<html>
<head>
<script type=”text/javascript”>
// Authors Birdshome, 麻袋@博客园 改版 phito,彭海涛
Object.prototype.Clone = function()
{
var objClone;
if ( this.constructor == Object ) objClone = new this.constructor();
else objClone = new this.constructor(this.valueOf());
for ( var key in this )
{
if ( objClone[key] != this[key] )
{
if ( typeof(this[key]) == ‘object’ )
{
objClone[key] = this[key].Clone();
}
else
{
objClone[key] = this[key];
}
}
}
objClone.toString = this.toString;
objClone.valueOf = this.valueOf;
return objClone;
}
function Hashtable() {
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
this.hashtable = new Object();
this.set = hashtable_set;
this.valueOf = hashtable_valueOf;
this.clone = hashtable_clone;
this.ignoreupperlower = true;
//是还是不是忽略大小写
}
/*=======Private methods for internal use only========*/
function hashtable_clone(){
return this.Clone();
}
function hashtable_put(key, value) {
if (this.ignoreupperlower && typeof(key) == “string”) {
key = key.toUpperCase();
}
if (key == null || value == null) {
throw “NullPointerException {” + key + “},{” + value + “}”;
} else {
this.hashtable[key] = value;
}
}
function hashtable_set(key, value) {
if (this.ignoreupperlower && typeof(key) == “string”) {
key = key.toUpperCase();
}
if (this.containsKey(key)) {
this.remove(key);
}
this.put(key, value);
}
function hashtable_get(key) {
if (this.ignoreupperlower && typeof(key) == “string”) {
key = key.toUpperCase();
}
return this.hashtable[key];
}
function hashtable_valueOf(key, defvalue) {
var ret = this.get(key);
if (typeof(ret) == “undefined”) {
return defvalue;
}
return ret;
}
function hashtable_remove(key) {
if (this.containsKey(key)) {
delete this.hashtable[key] ;
}
}
function hashtable_isEmpty() {
return (parseInt(this.size()) == 0) ? true: false;
}
function hashtable_size() {
var size = 0;
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
if (this.hashtable[i] != null) {
size++;
}
}
return size;
}
function hashtable_toString() {
var result = “”;
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
if (this.hashtable[i] != null) {
result += “{” + i + “:” + this.hashtable[i] + “}\n”;
}
}
return result;
}
function hashtable_clear() {
this.hashtable = new Object();
}
function hashtable_containsKey(key) {
if (this.ignoreupperlower && typeof(key) == “string”) {
key = key.toUpperCase();
}
var exists = false;
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
if (i == key && this.hashtable[i] != null) {
exists = true;
break;
}
}
return exists;
}
function hashtable_containsValue(value) {
var contains = false;
if (value != null) {
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
if (this.hashtable[i] == value) {
contains = true;
break;
}
}
}
return contains;
}
function hashtable_values() {
var values = new Object();
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
if (this.hashtable[i] != null) values.push(this.hashtable[i]);
}
return values;
}
function hashtable_keys() {
var keys = new Object();
for (var i in this.hashtable) {
if(typeof(this.hashtable[i])==”function”){
continue;
}
keys.push(i);
}
return keys;
}
function test() {
var ht = new Hashtable();
ht.put(“3”, “Jackson”);
ht.put(“2”, “Tom”);
ht.put(“4”, 3);
ht.set(“length”, 445555);
ht.set(“ddd”, “ddd”);
ht.set(“index”, “ddd”);
var et = ht.toString();
ht.ignoreupperlower = false;
//忽略大小写
ht.clear();
ht.put(“3”, “Jackson”);
ht.put(“2”, “Tom”);
ht.remove(“2”);
ht.put(“4”, 3);
ht.set(“length”, 5);
//如若用new Array的话该项会设置Array的长度
ht.set(“index”, “ddd”);
ht.set(“ddd”, “ddd”);
alert(et + “” + ht.toString() + “” + ht.size());
var cloneobj=ht.clone();
alert(cloneobj.toString());
}
</script>
</head>
<body onload=”test()”>
</body>
</html>

var msg1 = “应接光临!”; // 定义二个全局变量
var msg2 = “再见!” //定了另三个全局变量
window.onload = function() {
  alert(msg1);
}
window.onunload = function() {
  alert(msg2);
}

大家也能够加如具体时间取值。具体如下:

一经你想使用效率越来越好的hashtable和hashset请下载:

这段代码中早就接纳了八个全局变量。而只是为了落到实处三个简小的成效。
再正是,全局变量太多,我们必得牢记:msg1是迎接时的变量,msg2是关门时变量……假设变量越来越多,大家仍是可以够记得住吗?

slow(1000);
如此表示时间完全体现出成分的年华间隔为1000微秒

“) bug
能够设置key忽略大小写 能够clone hashtable对象 能够使用obj.valueOf(“key”,”defalutvalue”) 设置暗中认可值等等 款待改良…

发表评论

电子邮件地址不会被公开。 必填项已用*标注