码迷,mamicode.com
首页 > Web开发 > 详细

JS 复制对象

时间:2016-09-02 13:08:07      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

<%@ 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 = {
                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 复制对象

标签:

原文地址:http://www.cnblogs.com/linhuide/p/5832799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!