示例

  1. var target = {
  2. company:"dcloud",
  3. product:{
  4. mui:"小巧、高效"
  5. }
  6. }
  7. var obj1 = {
  8. city:"beijing",
  9. product:{
  10. HBuilder:"飞一样的编码"
  11. }
  12. }
  13. mui.extend(target,obj1);
  14. //输出:{"company":"dcloud","product":{"HBuilder":"飞一样的编码"},"city":"beijing"}
  15. console.log(JSON.stringify(target));

从如上输出可以看到,product节点下的mui被替换成了HBuilder,因为默认仅合并对象根节点下的key、value;如果想深度合并,则可以传入deep参数,如下:

  1. var target = {
  2. company:"dcloud",
  3. product:{
  4. mui:"小巧、高效"
  5. }
  6. }
  7. var obj1 = {
  8. city:"beijing",
  9. product:{
  10. HBuilder:"飞一样的编码"
  11. }
  12. }
  13. //支持深度合并
  14. mui.extend(true,target,obj1);
  15. //输出:{"company":"dcloud","product":{"mui":"小巧、高效","HBuilder":"飞一样的编码"},"city":"beijing"}
  16. console.log(JSON.stringify(target));

扩展阅读

代码块激活字符:

mextend