大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> Script -> JS 中对象equals方法的实现

JS 中对象equals方法的实现

时间: 2021-07-31 作者:daque

上回咱们提出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=utf8"><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==nulltypeof(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=utf8"><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==nulltypeof(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 十足采用 提醒:你可先窜改局部代码,再按运转]

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.