上次我们谈到Javascript 中 浅拷贝与深拷贝的实现..
这回我们看看如何实现判断两个对像的内容是否相等。
这里有一个克隆结果原则是针对Java语言的,当然JavaScript也可以胜任。克隆满足的条件
clone()方法将对象复制了一份并返还给调用者。所谓“复制”的含义与clone()方法是怎么实现的有关。一般而言,clone()方法满足以下的描述:
第一、对任何的对象x,都有:x.clone() != x;换言之,克隆对象与原对象不是同一个对象。
第二、对任何的对象x,都有:x.clone().getClass == x.getClass(),换言之,克隆对象与原对象的类型一样;在Javascript getClass 就相当于constructor。
第三、如果对象x的equals()方法是定义恰当的话,那么 x.clone().equals(x)也应当是成立的。
在Java语言的API中,凡是提供了clone()方法的类,都满足上面的这些这些条件。Java语言的设计师在设计自己的clone()方法是,也应当遵守这三个条件。
第三条为可选,不必强制实现的,意思是说只要编制得当,也可以实现。
我们还拿上次的例子看一下:
运行代码框
<HTML><HEAD><TITLE> New Document </TITLE><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><META NAME="Generator" CONTENT="Wawa Editor 1.0"><META NAME="Author" CONTENT="八神奄"><META NAME="Keywords" CONTENT="Javascript,Java,XML,XSLT,ASP,VBScript,ASP .net,C#,C++,Database"><META NAME="Description" CONTENT="不及格的程序员,无所不在"></HEAD><BODY><SCRIPT LANGUAGE="JavaScript"><!--function Object.prototype.equals(obj){if(this == obj)return true;if(typeof(obj)=="undefined"
obj==null
typeof(obj)!="object")return false;var length = 0; var length1=0;for(var ele in this) length++;for(var ele in obj) length1++;if(length!=length1) return false;if(obj.constructor==this.constructor){for(var ele in this){if(typeof(this[ele])=="object") {if(!this[ele].equals(obj[ele]))return false;}else if(typeof(this[ele])=="function"){if(!this[ele].toString().equals(obj[ele].toString())) return false;}else if(this[ele]!=obj[ele]) return false;}return true;}return false;}function String.prototype.equals(str){if(this==str)return true;return false;}function Function.prototype.equals(func){if(this.toString().equals(func.toString()))return true;return false;}function Boolean.prototype.equals(bool){if(this==bool)return true;if (bool instanceof Boolean){ return this.toString().equals(bool.toString());} return false;}function Object.prototype.clone(){var newObj = new Object();for(elements in this){newObj[elements] = this[elements];}return newObj;}function Object.prototype.cloneAll(){function clonePrototype(){}clonePrototype.prototype = this;var obj = new clonePrototype();for(var ele in obj){if(typeof(obj[ele])=="object") obj[ele] = obj[ele].cloneAll();}return obj;}var obj1= new Object();obj1.Team= "First";obj1.Powers= new Array("Iori","Kyo");obj1.msg= function(){alert()};obj1.winner= new Object();obj1.winner.name= obj1.Powers[0];obj1.winner.age= 23;obj1.winner.from= "Japan"var obj1_clone= obj1.cloneAll();alert("刚刚克隆之后,对比结果为: "+obj1_clone.equals(obj1))obj1_clone.Team= "Second";obj1_clone.Powers= new Array("Jimmy","Anndy");obj1_clone.winner.name= obj1_clone.Powers[1];obj1_clone.winner.age= 22;obj1_clone.winner.from= "USA";alert("修改过被克隆对像属性之后的对比结果: "+obj1_clone.equals(obj1))alert("是否符合上面提到的第二条原则呢,结果为: "+(obj1_clone.constructor==obj1.constructor))msg = "2003界拳皇单打独斗杯,拳皇挑战赛: \n\n A组 对战形式:\n\n"msg += obj1.Team+" 组 ,人员名单:"+obj1.Powers+"\n";msg += "第一轮过后,胜利者为:"+obj1.winner.name +" ,参赛者年龄:"+obj1.winner.age+" ,来自岛国: "+obj1.winner.from+"\n";msg += "\n\n B组 对战形式:\n\n"msg += obj1_clone.Team+" 组 ,人员名单:"+obj1_clone.Powers+"\n";msg += "第一轮过后,胜利者为:"+obj1_clone.winner.name +" ,参赛者年龄:"+obj1_clone.winner.age+" ,来自国际警察部队: "+obj1_clone.winner.from+"\n";alert(msg);//--></SCRIPT></BODY></HTML>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]其它对像的equals 方法的样例:
后面一个,我重定义了 Boolean 构造函数,并实现了它的toStirng()方法。
可以瞅瞅。
运行代码框
<HTML><HEAD><TITLE> New Document </TITLE><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><META NAME="Generator" CONTENT="Wawa Editor 1.0"><META NAME="Author" CONTENT="八神奄"><META NAME="Keywords" CONTENT="Javascript,Java,XML,XSLT,ASP,VBScript,ASP .net,C#,C++,Database"><META NAME="Description" CONTENT="不及格的程序员,无所不在"></HEAD><BODY>var a = new Object(); a.arr = new Array("a","b","c");a.name="a"; a.alert = function(){alert()}<br>var b = new Object(); b.arr = new Array("a","b"); b.name="a"; b.alert = function(){}<br>var str = new String("abc");<br>var str_1 = "abc";<br>var str_2 = "abd";<br>var c = a;<br>var d = b;<br><SCRIPT LANGUAGE="JavaScript"><!--function Object.prototype.equals(obj){if(this == obj)return true;if(typeof(obj)=="undefined"
obj==null
typeof(obj)!="object")return false;var length = 0; var length1=0;for(var ele in this) length++;for(var ele in obj) length1++;if(length!=length1) return false;if(obj.constructor==this.constructor){for(var ele in this){if(typeof(this[ele])=="object") {if(!this[ele].equals(obj[ele]))return false;}else if(typeof(this[ele])=="function"){if(!this[ele].toString().equals(obj[ele].toString())) return false;}else if(this[ele]!=obj[ele]) return false;}return true;}return false;}function String.prototype.equals(str){if(this==str)return true;return false;}function Function.prototype.equals(func){if(this.toString().equals(func.toString()))return true;return false;}function Boolean.prototype.equals(bool){if(this==bool)return true;if (bool instanceof Boolean){ return this.toString().equals(bool.toString());} return false;}var a = new Object();a.arr = new Array("a","b","c");a.name="a"; a.alert = function(){alert()}var b = new Object(); b.arr = new Array("a","b");b.name="a";b.alert=function(){}var str = new String("abc");var str_1 = "abc";var str_2 = "abc";var c = a;var d = b;alert(" a.equals(b): "+a.equals(b)+"\n\n str.equals(str_1): "+str.equals(str_1)+"\n\n str_1.equals(str_2): "+str_1.equals(str_2)+"\n\n c.equals(a): "+a.equals(c)+"\n\n d.equals(b) : "+b.equals(d)+"\n\n a.alert.equals(b.alert): "+a.alert.equals(b.alert))</SCRIPT><SCRIPT LANGUAGE="JavaScript">function Boolean(booleans){if(typeof(booleans)=="undefined"){this.value = false;return this;}else{if(booleans==null){this.value = false;return this;}if(booleans==""){this.value = false;return this;}if(!isNaN(booleans)){if(booleans<=0){this.value = false;}else{this.value = true;}return this;}if(booleans!="true"){this.value = false;return this;}else{this.value = true;return this;}}} function Boolean.prototype.toString(){ return this.value ? "true" : "false" } document.writeln("两个布尔型对像的比较结果:new Boolean().equeals(new Boolean(\"true\")): "+(new Boolean().equals(new Boolean("true"))));</SCRIPT></BODY></HTML>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
……