About the for... in statement after the object is cloned and prototype is used

Source: Internet
Author: User

Today, I encountered a problem and started to think it was quite strange. Later I suddenly realized that it was a problem of the shortest copy and deep copy in object cloning. I know what I mean when reading a book, but I cannot think of where I can use it? So that the xuanjicang was found here after a bug occurs. The problem is as follows:

VaR strtree = {"000501": {"ID": "127", "name": "Personnel download", "haschild": True, "islast": false, "changeflag": 0, "isfolder": true },
"000502": {"ID": "128", "name": "Labor download", "haschild": false, "islast": True, "changeflag": 0, "isfolder": false }};
Strtree ["000601"] = strtree ["000501"];
Strtree ["000601"]. changeflag = 1;

The result shows that the changeflag of strtree ["000501"] And strtree ["000601"] is changed to 1. The reason is that two JSON Objects Reference the same object address, of course, you can use the re-apply object and address initialization method to solve the problem of different references, and then assign values to attributes one by one, such:

Function exchangejson (divobj, id1, Id2)
{
If (strtree [id1] = undefined)
Strtree [id1] = {"ID": "0", "name": "", "haschild": false, "islast": false, "isfolder": false, "changeflag": 1 };
Strtree [id1]. Name = strtree [Id2]. Name;
Strtree [id1]. haschild = strtree [Id2]. haschild;
Strtree [id1]. islast = strtree [Id2]. islast;
Strtree [id1]. isfolder = strtree [Id2]. isfolder;
}

But obviously, this is not good enough. It only targets the specific structure of the strtree and has no scalability and versatility. The prototype method is used to solve all the problems of the same type. before writing the code, I checked it on the Internet and found a perfect solution in, copy it (of course, it's not just wonlong jujube)

// Authors birdshome, Sacks @ blog Garden
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;
}

However, in my application, a very depressing problem occurs because prototype extension is used, and the strtree in my application is processed by the for... in loop.

For (element in strtree)
{
Strtree [element]. Name =

}

But there is a problem. There is a value in the element, that is, the method name after the prototype extension "clone", causing the program to report an error, which makes me very uncomfortable. Do you need to add If (element! = "Clone") then? As the application goes deeper, prototype may be extended at any time. Obviously this is not the case, .. the in statement is used to traverse strtree with irregular underlying standards. It seems that it is not feasible to replace it with another loop. Someone on the Internet has proposed a solution that hasn't come up with a good solution yet. It's not so uncomfortable .... Hope to enlighten me!

Yesterday, iflytek sent me an email, providing a good way to use object. constructor is used to distinguish prototype extension methods from expando. For constructor, I did not care about it before. how useful it is. The help manual explains: the constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except global and math objects. The constructor attribute stores references to the functions used to construct a specific object instance. That is to say, by setting a var OBJ = new sourceobj. constructor (); you can assign the prototypte Extension Method of the object together with the instance function name to OBJ, And the JSON object member in the example will not be assigned a value, so you can apply it like this:

VaR OBJ = new this. jsontree. Constructor ();
For (flag in this. jsontree)
{
If (OBJ [flag]! = This. jsontree [flag])

}

The general problems are also solved, and prototype can be expanded. Done!

There is nothing in the problem itself, but the problems reflected cannot be underestimated, that is, they do not pay enough attention to the principles of things, the way to solve the problem is simply to search online, or turn to others, of course, this is not a bad idea, but it is obviously impossible to create something and do something pioneering. Please remind yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.