JS CLONE使用法

  • 时间:2018-06-06 01:46 作者:myersguo 来源:myersguo 阅读:943
  • 扫一扫,手机访问
摘要:js 代码:a=5;b=a;b=4;console.log(a,b);结果输出:5 4而:a={}b=ab.title="title"console.log(a,b);If you use an = statement to assign a value to a var with an objec

js 代码:

a=5;

b=a;

b=4;

console.log(a,b);

结果输出:

5 4

而:

a={}

b=a

b.title="title"

console.log(a,b);

If you use an = statement to assign a value to a var with an object on the right side, javascript will not copy but reference the object

对象的赋值,是传递的引使用,假如想要传值,则能用jquery:

$.extend({}, obj);

例子2:

a = {

'title': 'title',

'child': {

'info': {

'title': 'child title',

}

}

}

b = $.extend({}, a.child);

b.info.title="new title";

console.log(a.child.info.title, b.info.title);

结果输出:new title new title

这是由于:

The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.

第一个参数传true,进行 deepcopy,即对递归copy,对象的值也被copy了。

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】2FA验证器 验证码如何登录(2024-04-01 20:18)
【系统环境|】怎么做才能建设好外贸网站?(2023-12-20 10:05)
【系统环境|数据库】 潮玩宇宙游戏道具收集方法(2023-12-12 16:13)
【系统环境|】遥遥领先!青否数字人直播系统5.0发布,支持真人接管实时驱动!(2023-10-12 17:31)
【系统环境|服务器应用】克隆自己的数字人形象需要几步?(2023-09-20 17:13)
【系统环境|】Tiktok登录教程(2023-02-13 14:17)
【系统环境|】ZORRO佐罗软件安装教程及一键新机使用方法详细简介(2023-02-10 21:56)
【系统环境|】阿里云 centos 云盘扩容命令(2023-01-10 16:35)
【系统环境|】补单系统搭建补单源码搭建(2022-05-18 11:35)
【系统环境|服务器应用】高端显卡再度登上热搜,竟然是因为“断崖式”的降价(2022-04-12 19:47)
手机二维码手机访问领取大礼包
返回顶部