jQuery深拷贝
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>jQuery深拷贝</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
$(function () {
var myAction = {};
$.extend(myAction, {
initPage: function (pointA, pointB) {
var myTest = {
a: 'a',
b: 'b'
};
var myTest2 = jQuery.extend({}, myTest);
myTest2.b = 'b2';
console.log(myTest.b === 'b2');//false
var myArr = ;
var myArr2 = jQuery.extend([], myArr);
myArr2 = 3;
console.log(myArr === 3);//false
var myArrObj = [{a: 1}, {a: 2}];
var myArrObj2 = jQuery.extend([], myArrObj);
myArrObj2.a = 3;
console.log(myArrObj.a === 3);//true 注意:未能实现深拷贝
var myArrObj = [{a: 1}, {a: 2}];
var myArrObj2 = jQuery.extend(true, [], myArrObj);
myArrObj2.a = 3;
console.log(myArrObj.a === 3);//false
}
});
var init = function () {
myAction.initPage();
}();
});
</script>
<script>
</script>
</body>
</html>
文档来源:51CTO技术博客https://blog.51cto.com/u_15315190/3202269
页:
[1]