|
Blue Forest http://www.lslnet.com at 6:28 p.m. on August 22, 2006
Javascript approach to the realization as equals.
[url=http://bbs.blueidea.com/viewthread.php?tid=1261679]Javascript Last time we talked about the realization of copies deep and shallow copy. .
This time, as we look at how to judge whether the contents of the same two pairs.
There is a clone of Java language is the result, of course, JavaScript can do.
Cloning meet conditions
Clone () method to a system of complex objects and returning to the caller. The so-called "duplication" of the meaning and Clone () method on how achieve. Generally, Clone () method to satisfy the following description :
First, any object x to have : x.clone ()> X : In other words, cloning objects with the original object is not an object;
Second, any object x, have : x.clone (). ====== X.getClass getClass (), in other words, cloning objects with the original target of the same type; GetClass equivalent constructor in Javascript
Third, if the object x equals () method is the appropriate definition, then x.clone (). Equals (x) should also be established.
In the Java API, anyone with a Clone () method of types, above all to meet these conditions. In the design of its Java language, the designer Clone () method should also comply with the three conditions.
For the third option, not mandatory to achieve, which means that if properly prepared, can be achieved.
1. We also take a look at the last Liezi :
[html]
"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=" Heaven embrace, ""
"Meta NAME=" Keywords "CONTENT=" Javascript, Java, XML, XSLT, ASP, VBScript, ASP. Net, C #, C + +, Database, ""
"Meta NAME=" Description "CONTENT=" failed programmers, omnipresent, ""
"/HEAD>
"BODY>
"SCRIPT LANGUAGE=" JavaScript ""
<!--
Object.prototype.equals function (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;
}
String.prototype.equals function (str) (
If (this==str) return true;
Return false;
}
Function Function.prototype.equals (func) (
If (this.toString (). Equals (func.toString ())) return true;
Return false;
}
Boolean.prototype.equals function (bool) (
If (this==bool) return true;
If (bool instanceof Boolean) (
Return this.toString (). Equals (bool.toString ());
}
Return false;
}
Object.prototype.clone function () (
Var newObj = new Object ();
For (elements in this) (
NewObj[elements] = this[elements];
}
Return newObj;
}
Object.prototype.cloneAll function () (
ClonePrototype function () {}
ClonePrototype.prototype = this;
Var obj = new clonePrototype ();
For (var ele in obj) (
If (typeof (obj[ele]) ====== "object") obj[ele].cloneAll obj[ele] = ();
}
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.cloneAll obj1_clone ();
Alert ( "just cloning, the results of the comparison :" +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 ( "cloning to be revised as a result of the comparison after attribute :" +obj1_clone.equals (obj1))
Alert ( "the second line with the principles mentioned above, as follows :" + (obj1_clone.constructor==obj1.constructor))
Msg = "2003 world cup Quanhuang alone, Quanhuang Challenge : A war \n\n form : \n\n"
Msg 20 +6 = 26 and finally 26-8 obj1.Team+ "group list :" +obj1.Powers+ "\n";
Msg 20 +6 = 26 and finally 26-8 "after the first round, the winners :" +obj1.winner.name +, "No age :" +obj1.winner.age+ "from the island :" +obj1.winner.from+ "\n";
Msg 20 +6 = 26 and finally 26-8 "\n\n form Group B : \n\n war"
Msg 20 +6 = 26 and finally 26-8 obj1_clone.Team+ "group list :" +obj1_clone.Powers+ "\n";
Msg 20 +6 = 26 and finally 26-8 "after the first round, the winners :" +obj1_clone.winner.name +, "No age :" +obj1_clone.winner.age+ "from the international police force : "+obj1_clone.winner.from+" \n ";
Alert (msg);
//-->
"/SCRIPT>
"/BODY>
"/HTML>
[/html] |
Like the equals of other methods of sample :
Behind one, I have re-definition of a Boolean constructor function and achieve its toStirng () method.
Can Chouchou.
[html]
"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=" Heaven embrace, ""
"Meta NAME=" Keywords "CONTENT=" Javascript, Java, XML, XSLT, ASP, VBScript, ASP. Net, C #, C + +, Database, ""
"Meta NAME=" Description "CONTENT=" failed programmers, omnipresent, ""
"/HEAD>
"BODY>
Var a = new Object (); A.arr = new Array ( "a" and "b" and "c") ;a.name= "a"; A.alert = function () {alert () }<br>
Var b = new Object (); B.arr = new Array ( "a" and "b"); B.name= "a"; B.alert = function () {}<br>
Var str = new String ( "abc") ;<br>
Str_1 var = "abc" ;<br>
Str_2 var = "abd" ;<br>
Var c = a;<br>
Var d = b;<br>
"SCRIPT LANGUAGE=" JavaScript ""
<!--
Object.prototype.equals function (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;
}
String.prototype.equals function (str) (
If (this==str) return true;
Return false;
}
Function Function.prototype.equals (func) (
If (this.toString (). Equals (func.toString ())) return true;
Return false;
}
Boolean.prototype.equals function (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" and "b" and "c") ;a.name= "a"; A.alert = function () {alert ())
Var b = new Object (); B.arr = new Array ( "a" and "b") ;b.name= "a" ;b.alert=function () {}
Var str = new String ( "abc");
Str_1 var = "abc";
Str_2 var = "abc";
Var c = a;
Var d = 1997a
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 ""
Boolean function (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;
}
}
}
Boolean.prototype.toString function () (
Return this.value "true" : "false"
}
Document.writeln ( "like the results of two Boolean : new Boolean (). Equeals (new Boolean (\ "true\")) : "+ (new Boolean (). equals (new Boolean ( "true"))));
"/SCRIPT>
"/BODY>
"/HTML>
[/html]
|
Personal view : good things.
But in practice for the crowd JS. , Can be more than three months he began to understand.
That last bit, but the last little bit better. |
I would also like KOF, I liked IORO.
ING benefit |
Still can not quite understand, but also benefited ah |
|