首页 > 代码库 > JS 复制对象

JS 复制对象

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script type="text/javascript">        function clone(myObj) {            if (typeof (myObj) != object) return myObj;            if (myObj == null) return myObj;            var myNewObj = new Object;            for (var i in myObj)                myNewObj[i] = clone(myObj[i]);            return myNewObj;        }        function test() {            var data =http://www.mamicode.com/ {                Id: 1,                Text: "lin"            };            var dataCopy = {};            dataCopy = clone(data);            var strDataCopy = JSON.stringify(dataCopy);            alert(strDataCopy);        }    </script></head><body>    <form id="form1" runat="server">        <div>            <button onclick="test()">测试</button>        </div>    </form></body></html>

 

JS 复制对象