aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT
Advertise Here

Advertize

wwwSW
Posted by Aamir Hasan   on Friday, April 1, 2011 Total Views:  

In this example you will see how to use deep copy and shallow copy an object using jQuery. By using extend method you can copy an original object to another object.

Syntax

jQuery.extend( [ deep ], target, object1, [ objectN ] ) //returns Object

 

Deep copy

  var deepCopyObject = jQuery.extend(true, new Object(), Obj);


Shallow copy

 var shallowCopyObject = jQuery.extend(new Object(), Obj);

 

Example


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Copy an Object using jQuery </title>

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">

 

        $(document).ready(function () {

            var Obj = { "size": 4,

                "color": "#EAEAEA",

                "seq": [5, 6, 7, 8],

                "Information": {

                    "firstName": "Aamir",

                    "lastName": "Ha",

                    "Details": { "Gender": "Male", "Location": "PK" }

                }

            };

            /* Deep copy*/

            var deepCopyObject = jQuery.extend(true, new Object(), Obj);

 

            deepCopyObject.Information.Details.Location = "PAK";

 

            //alert("Obj.Information.Details.Location => " + Obj.Information.Details.Location);

            //alert("deepCopyObject.Information.Details.Location => "
                      + deepCopyObject.Information.Details.Location);

 

            $("#msg").append("<b>Orignal Object</b> > Obj.Information.Details.Location => <b>"
                              + Obj.Information.Details.Location + "</b><br>");

            $("#msg").append("<b>Copied Object</b> > deepCopyObject.Information.Details.Location => <b>"
                              + deepCopyObject.Information.Details.Location + "</b><br><br>");

 

            deepCopyObject.Information.lastName = "Hasan";

 

            //alert("Obj.Information.firstName => " + Obj.Information.lastName.toString());

            //alert("deepCopyObject.Information.Details.Location => "
                       + deepCopyObject.Information.lastName.toString());

 

            $("#msg").append("<b>Orignal Object</b> > Obj.Information.firstName => <b>"
                               + Obj.Information.lastName.toString() + "</b><br>");

            $("#msg").append("<b>Copied Object</b> > deepCopyObject.Information.Details.Location => <b>"
                               + deepCopyObject.Information.lastName.toString() + "</b><br>");

 

 

            var shallowCopyObject = jQuery.extend(new Object(), Obj);

 

            shallowCopyObject.Information.Details.Location = "PAK";

 

            //alert("Obj.Information.Details.Location => " + Obj.Information.Details.Location);

            //alert("shallowCopyObject.Information.Details.Location => "
                    + shallowCopyObject.Information.Details.Location);

 

            $("#msgShallow").append("<b>Orignal Object</b> > Obj.Information.Details.Location => <b>"
                                      + Obj.Information.Details.Location + "</b><br>");

            $("#msgShallow").append("<b>Copied Object</b> > shallowCopyObject.Information.Details.Location => <b>"
                                      + shallowCopyObject.Information.Details.Location + "</b><br><br>");

 

            shallowCopyObject.Information.lastName = "Hasan";

 

            //alert("Obj.Information.firstName => " + Obj.Information.lastName.toString());

            //alert("shallowCopyObject.Information.Details.Location => "
                    + shallowCopyObject.Information.lastName.toString());

 

            $("#msgShallow").append("<b>Orignal Object</b> > Obj.Information.firstName => <b>"
                                      + Obj.Information.lastName.toString() + "</b><br>");

            $("#msgShallow").append("<b>Copied Object</b> > shallowCopyObject.Information.Details.Location => <b>"
                                      + shallowCopyObject.Information.lastName.toString() + "</b><br>");

 

        });

 

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h1>Deep copy an Object</h1>

        <div id="msg">&nbsp;</div>

        <h1>Shallow copy an Object</h1>

        <div id="msgShallow">&nbsp;</div>

    </div>

    </form>

</body>

</html>


Output

Download

Deep and Shallow Copy an Object.rar (43.58 kb)

See live demo

Protected by Copyscape Online Plagiarism Tool

Comments (2) -

car rentals
car rentals United States
4/17/2011 11:04:22 PM #

Would you equal to spot a invitee occupation on my diary?

replica tiffany jewelry
replica tiffany jewelry Malta
1/10/2012 10:22:50 PM #

The wedding ring wear in hand or wear in The heart? http://www.discounttiffanyoutlet.com/ replica tiffany jewelry is carefully done to act the role of, www.discounttiffanyoutlet.com/tiffany-bangles/ tiffany bangle give you the most sincere love, www.cheaptiffanystyle.com/tiffany-accessories/ tiffany accessories give you warm care, www.discounttiffanyoutlet.com/tiffany-rings/ tiffany rings to give you a blessing.

fafafa
fafafa Slovenia
4/23/2012 2:41:17 AM #

http://www.christianlouboutinukk.org   christian louboutin
http://www.tiffanyuks.org   tiffany uk
http://www.gucciukbeltuk.org   gucci belt
http://www.frsaclouisvuittonsac.com   louis vuitton sac
http://www.chanelukoutletuks.org   chanel outlet
http://www.burberryukoutletuk.org   burberry
http://www.poloralphlaurenuko.org   ralph lauren uk

feel
feel People's Republic of China
5/3/2012 12:27:40 AM #


Cheap NFL,NBA,MLB,NHL [url=http://www.annajerseys.com/]China Jerseys[/url],[url=http://www.annajerseys.com/]China Jerseys Paypal[/url] For Sale online.[url=www.annajerseys.com/...ke-nfl-jerseys-c-1_478.html]2012 Nike NFL Jerseys[/url],All Our Jerseys Are Sewn On and Directly From [url=www.annajerseys.com/...d-patriots-c-1_478_498.html]Nike New England Patriots Jerseys[/url] Factory2012 [url=www.annajerseys.com/.../...fl-jerseys-c-1_478.html]Nike NFL Jerseys China[/url] Wholesale,Wholesale [url=www.annajerseys.com/...falo-bills-c-1_478_482.html]Nike Buffalo Bills Jerseys[/url] From,The high quality Nike NFL Jerseys are  [url=www.annajerseys.com/...oston-bruins-c-124_133.html]Boston Bruins Jerseys[/url] great gifts [url=www.annajerseys.com/...o-blackhawks-c-124_137.html]Chicago Blackhawks Jerseys China[/url] for Arizona Cardinals  [url=www.annajerseys.com/...e-seahawks-c-1_478_507.html]Nike Seattle Seahawks Jerseys[/url].Welcome to our [url=http://www.annajerseys.com/]http://www.annajerseys.com/[/url] to pick [url=www.annajerseys.com/...hia-eagles-c-1_478_503.html]Nike Philadelphia Eagles Jerseys[/url] the hotest [url=www.annajerseys.com/.../...fl-jerseys-c-1_478.html]2012 New NFL Jerseys[/url].
[/pre]

  Best Converse Online Store From UK,Sale 2011 Newest [url=www.sale-converse.com/...rse-flag-shoes-c-130.html]Converse Flag Shoes[/url],Black Converse,[url=www.sale-converse.com/...ewest-converse-c-135.html]Converse 2012[/url],[url=http://www.sale-converse.com/]Cheap Converse[/url] Navy Converse And So on [url=www.sale-converse.com/...-hitop-uk-flag-p-899.html]Converse 2011 All Star Shoes Hi-top UK Flag[/url],[url=http://www.sale-converse.com/]Converse UK[/url],[url=www.sale-converse.com/...rica-flag-red-p-1037.html]Converse 2011 All Star Shoes Hi-top America Flag Red[/url]Free Shipping
[/pre]

We are professional jerseys manufacturer from china,wholesal sports [url=http://www.anna-jersey.com/nfl-jerseys-c-773.html]NFL jerseys China[/url],[url=http://www.anna-jersey.com/nhl-jerseys-c-882.html]NHL Jerseys China[/url],[url=http://www.anna-jersey.com/nba-jerseys-c-821.html]NBA Jerseys China[/url],[url=http://www.anna-jersey.com/mlb-jerseys-c-694.html]MLB Jerseys China[/url],[url=http://www.anna-jersey.com/]China Jerseys[/url],  [url=www.anna-jersey.com/...nike-nfl-jerseys-c-943.html]2012 Nike NFL Jerseys[/url],[url=www.anna-jersey.com/...chicago-cubs-c-694_702.html]Chicago Cubs Jerseys China[/url], Free Shipping
[/pre]
[pre]
,[url=http://www.china4jersey.com/nhl-jerseys-c-512.html]NHL Jerseys China[/url] Paypal,[url=http://www.china4jersey.com/]China Jerseys Paypal[/url] factory,Sewn On Jerseys,Accept Paypal,Free Shipping,[url=www.china4jersey.com/nike-nfl-2012-c-638.html]Nike NFL Jerseys Paypal[/url]   ,Nike Jerseys,[url=www.china4jersey.com/nike-nfl-2012-c-638.html]NFL Jerseys 2012[/url],[url=www.china4jersey.com/nike-nfl-2012-c-638.html]Nike Jerseys China[/url],[url=www.china4jersey.com/nike-nfl-2012-c-638.html]Wholesale Nike Jerseys China[/url][url=http://www.nikenflchina.com/]Nike NFL Jerseys[/url],[url=http://www.nikenflchina.com/]New NFL Jerseys 2012[/url],[url=http://www.nikenflchina.com/]Nike NFL Jerseys China[/url],[url=www.nikenflchina.com/...nike-nfl-jerseys-c-73.html]2012 Nike NFL Jerseys[/url],[url=www.china4jersey.com/...ton-red-sox-c-398_401.html]Boston Red Sox[/url]Professional Online Wholesale Shop From China,We are Chinese Jerseys Manufactruer,Wholesale All Team Of Nike NFL Jerseys,2012 New NFL Jeresys,NFL Jerseys China,Nike Jerseys China,New NFL Jerseys,All Jerseys are embroidered on,Free Shipping To World Wide
[/pre]

Professional Converse Online Store From UK,[url=http://www.converse2you.com/]Converse UK[/url] Shop,Sale 2012 Newest Converse All Stars,Converse Flag Shoes,[url=http://www.converse2you.com/]Cheap Converse[/url],[url=http://www.converse2you.com/]Converse Sale[/url],[url=www.converse2you.com/index.php]Converse Flag Shoes[/url] ,[url=www.converse2you.com/index.php]New Converse 2012[/url] Free Shipping To World Wide.
[/pre]

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5