{"action":"created","issue":{"url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/57","repository_url":"https://api.github.com/repos/Wscats/Good-text-Share","labels_url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/57/labels{/name}","comments_url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/57/comments","events_url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/57/events","html_url":"https://github.com/Wscats/Good-text-Share/issues/57","id":182706992,"number":57,"title":"Javascript深浅拷贝","user":{"login":"Wscats","id":17243165,"avatar_url":"https://avatars.githubusercontent.com/u/17243165?v=3","gravatar_id":"","url":"https://api.github.com/users/Wscats","html_url":"https://github.com/Wscats","followers_url":"https://api.github.com/users/Wscats/followers","following_url":"https://api.github.com/users/Wscats/following{/other_user}","gists_url":"https://api.github.com/users/Wscats/gists{/gist_id}","starred_url":"https://api.github.com/users/Wscats/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wscats/subscriptions","organizations_url":"https://api.github.com/users/Wscats/orgs","repos_url":"https://api.github.com/users/Wscats/repos","events_url":"https://api.github.com/users/Wscats/events{/privacy}","received_events_url":"https://api.github.com/users/Wscats/received_events","type":"User","site_admin":false},"labels":[{"id":358552104,"url":"https://api.github.com/repos/Wscats/Good-text-Share/labels/notes","name":"notes","color":"d4c5f9","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-10-13T06:35:55Z","updated_at":"2016-12-22T02:11:22Z","closed_at":null,"body":"Javascript有五种**基本数据类型**（也就是简单数据类型），它们分别是：Undefined，Null，Boolean，Number和String。还含有一种复杂数据类型，就是**对象**\r\n\r\n注意Undefined和Null的区别，Undefined类型只有一个值，就是undefined，Null类型也只有一个值，也就是null\r\n**Undefined**其实就是已声明未赋值的变量输出的结果\r\n**null**其实就是一个不存在的对象的结果\r\n\r\n```\r\nvar c;\r\nconsole.log(c)//undefined\r\n\r\nconsole.log(document.getElementById('wsscat'))//没有id为wsscat的节点,输出null\r\n```\r\n\r\n简单的数据类型和复杂的数据类型有以下重要的区别\r\n### 对于简单数据类型\r\n\r\n它们值在占据了内存中固定大小的空间，并被保存在栈内存中。当一个变量向另一个变量复制基本类型的值，会创建这个值的一个副本，还有就是不能给基本数据类型的值添加属性\r\n\r\n```\r\nvar a = 1;\r\nvar b = a;\r\na.attr = 'wsscat';\r\nconsole.log(a.attr)//undefined\r\n```\r\n\r\n上面代码中a就是简单数据类型（Number），b就是a的副本，它们两者都占有不同位置但相等的内存空间\r\n### 对于复杂的数据类型\r\n\r\n复杂的数据类型即引用类型，它的值是对象，保存在堆内存中，包含引用类型值的变量实际上包含的并不是对象本身，而是一个指向该对象的指针。从一个变量向另一个变量复制引用类型的值，复制的其实是指针，因此两个变量最终都指向同一个对象。\r\n\r\n```\r\nvar obj = {\r\n            name:'wsscat',\r\n            age:0\r\n        }\r\n        var obj2 = obj;\r\n        obj2['c'] = 5;\r\n        console.log(obj);//Object {name: \"wsscat\", age: 0, c: 5}\r\n        console.log(obj2);////Object {name: \"wsscat\", age: 0, c: 5}\r\n```\r\n\r\n![qq20161016-0](https://cloud.githubusercontent.com/assets/17243165/19415247/22b46910-939d-11e6-8ac3-e8aa8e5d7b32.png)\r\n\r\n我们可以看到obj赋值给obj2后，当我们更改其中一个对象的属性值，两个对象都发生了改变，究其原因局势因为obj和obj2这两个变量都指向同一个指针，赋值只是复制了指针，所以当我们改变其中一个的值就会影响另外一个变量的值\r\n### 浅拷贝\r\n\r\n其实这段代码就是浅拷贝，有时候我们只是想备份数组，但是只是简单让它赋给一个变量，改变其中一个，另外一个就紧跟着改变，但很多时候这不是我们想要的\r\n\r\n```\r\nvar obj = {\r\n            name:'wsscat',\r\n            age:0\r\n        }\r\n        var obj2 = obj;\r\n        obj2['c'] = 5;\r\n        console.log(obj);//Object {name: \"wsscat\", age: 0, c: 5}\r\n        console.log(obj2);////Object {name: \"wsscat\", age: 0, c: 5}\r\n```\r\n### 深拷贝\r\n\r\n_数组_\r\n对于数组我们可以使用`slice()`和`concat()`方法来解决上面的问题\r\n**slice**\r\n\r\n```\r\nvar arr = ['wsscat', 'autumns', 'winds'];\r\n        var arrCopy = arr.slice(0);\r\n        arrCopy[0] = 'tacssw'\r\n        console.log(arr)//['wsscat', 'autumns', 'winds']\r\n        console.log(arrCopy)//['tacssw', 'autumns', 'winds']\r\n```\r\n\r\n**concat**\r\n\r\n```\r\nvar arr = ['wsscat', 'autumns', 'winds'];\r\n        var arrCopy = arr.concat();\r\n        arrCopy[0] = 'tacssw'\r\n        console.log(arr)//['wsscat', 'autumns', 'winds']\r\n        console.log(arrCopy)//['tacssw', 'autumns', 'winds']\r\n```\r\n\r\n_对象_\r\n对象我们可以定义一个新的对象并遍历新的属性上去实现深拷贝\r\n\r\n```\r\nvar obj = {\r\n            name:'wsscat',\r\n            age:0\r\n        }\r\n\r\n        var obj2 = new Object();\r\n        obj2.name = obj.name;\r\n        obj2.age = obj.age\r\n\r\n        obj.name = 'autumns';\r\n        console.log(obj);//Object {name: \"autumns\", age: 0}\r\n        console.log(obj2);//Object {name: \"wsscat\", age: 0}\r\n```\r\n\r\n当然我们可以封装好一个方法来处理对象的深拷贝，代码如下\r\n\r\n```\r\nvar obj = {\r\n            name: 'wsscat',\r\n            age: 0\r\n        }\r\n        var deepCopy = function(source) {\r\n            var result = {};\r\n            for(var key in source) {\r\n                if(typeof source[key] === 'object') {\r\n                    result[key] = deepCopy(source[key])\r\n                } else {\r\n                    result[key] = source[key]\r\n                }\r\n            }\r\n            return result;\r\n        }\r\n        var obj3 = deepCopy(obj)\r\n        obj.name = 'autumns';\r\n        console.log(obj);//Object {name: \"autumns\", age: 0}\r\n        console.log(obj3);//Object {name: \"wsscat\", age: 0}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/comments/268700177","html_url":"https://github.com/Wscats/Good-text-Share/issues/57#issuecomment-268700177","issue_url":"https://api.github.com/repos/Wscats/Good-text-Share/issues/57","id":268700177,"user":{"login":"yeyuqiudeng","id":10278227,"avatar_url":"https://avatars.githubusercontent.com/u/10278227?v=3","gravatar_id":"","url":"https://api.github.com/users/yeyuqiudeng","html_url":"https://github.com/yeyuqiudeng","followers_url":"https://api.github.com/users/yeyuqiudeng/followers","following_url":"https://api.github.com/users/yeyuqiudeng/following{/other_user}","gists_url":"https://api.github.com/users/yeyuqiudeng/gists{/gist_id}","starred_url":"https://api.github.com/users/yeyuqiudeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yeyuqiudeng/subscriptions","organizations_url":"https://api.github.com/users/yeyuqiudeng/orgs","repos_url":"https://api.github.com/users/yeyuqiudeng/repos","events_url":"https://api.github.com/users/yeyuqiudeng/events{/privacy}","received_events_url":"https://api.github.com/users/yeyuqiudeng/received_events","type":"User","site_admin":false},"created_at":"2016-12-22T02:11:22Z","updated_at":"2016-12-22T02:11:22Z","body":"![image](https://cloud.githubusercontent.com/assets/10278227/21412945/f18925f8-c82e-11e6-8996-892bfea262d9.png)\r\nconcat 和slice对对象数组没有效果的？"}}
{"actor":{"display_login":"yeyuqiudeng"}}