angular 中一些常用的小方法之详解API

本文深入探讨Angular中的三个关键方法:angular.merge实现深拷贝并扩展对象;angular.extend用于扩展对象但不支持深拷贝;angular.copy创建对象或数组的深拷贝。通过实例对比不同方法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用angular进行开发,其中有一些好用的小方法,在这里进行梳理一下。

常用小方法:

  1. angular.merge 用于深拷贝对象,官方的一句话:

    Deeply extends the destination object dst by copying own enumerable properties from the src object(s) to dst. You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular.merge({}, object1, object2).

    Unlike extend(), merge() recursively descends into object properties of source objects, performing a deep copy.

    从src对象中拷贝可枚举的属性到目标对象dst中,用于深度扩展目标对象. 你可以指定多个src对象,如果想要保存原对象,那么传递一个空对象作为目标对象 如: angular.erge({},object1,object2);

    不像extend函数,merge 对原对象的属性进行递归,执行一次深拷贝

    • 举例说明问题:

      // merge 
      var obj1 = {"name":"obj1name","age":25};
      var obj2 = {"name":"obj2name","weight":75};
      
      var res = angular.merge(obj1,obj2);
      console.log('obj1');
      console.log(obj1);
      
      console.log('obj2');
      console.log(obj2);
      
      console.log('res');
      console.log(res); 
      
      /* 上面输出内容为:
          obj1
          Object {name: "obj2name", age: 25, weight: 75}
          obj2
          Object {name: "obj2name", weight: 75}
          res
          Object {name: "obj2name", age: 25, weight: 75}
          由上可见 merge 改变了目标对象的值
      */
      

      此方法用于对象的深拷贝,拷贝自己可枚举的属性,如果存在则替换,不存在追加。

          var obj3 = {"name":"obj3name","color":"red"};
          var obj4 = {"name":"obj4name","size":43};
      
          var res = angular.merge({},obj3,obj4); // 此处添加一个空对象
      
          console.log('obj3');
          console.log(obj3);
      
          console.log('obj4');
          console.log(obj4);
      
          console.log('res');
          console.log(res); 
      
          /* 上面输出内容为:
              obj3
              Object {name: "obj3name", color: "red"}
              obj4
              Object {name: "obj4name", size: 43}
              res
              Object {name: "obj4name", color: "red", size: 43}
              由此可见,添加了一个空对象,阻止了对原对象的更改。
          */
  2. angular.extend

    引用官方的一句话:

    Extends the destination object dst by copying own enumerable properties from the src object(s) to dst. You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular.extend({}, object1, object2).

    Note: Keep in mind that angular.extend does not support recursive merge (deep copy). Use angular.merge for this.

    从src对象中拷贝自己的可枚举属性到dst,用来扩展目标对象dst。可指定多个src 对象。 如果想要保存原对象,可以传一个空对象作为目标, 如: var object = angular.extend({},object1,object2);

    注意: extend 方法不支持递归式深拷贝,如果需要,请使用 angular.merge

    直接上代码:

    var a = {"group":1,"dept":"computerTech"};
    var b = {"group":2,"age":30};
    
    var res = angular.extend(b,a);
    console.log(a); // Object {group: 1, dept: "computerTech"}
    console.log(b); // Object {group: 1, age: 30, dept: "computerTech"}
    console.log(res); // Object {group: 1, age: 30, dept: "computerTech"}
    // 右上可见 extend 改造了目标对象

    同样可以通过一个空对象参数,不让其改变原始对象的值

  3. angular.copy

    引用官方语言:

    Creates a deep copy of source, which should be an object or an array.(创造source的深拷贝,其中source应该时对象或者数组)

    If no destination is supplied, a copy of the object or array is created.(如果没有destination,那么这个对象或数组的拷贝被创建)

    If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.(如果提供了destination,针对数组,所有的元素,针对对象,所有的属性都会被删除,然后从source里拷贝所有的元素或者属性进来)

    If source is not an object or array (inc. null and undefined), source is returned.(如果source不是一个对象或数组,比如null或者undefined,那么会返回source,备注:这个需要自己写写测试代码,就知道了)

    If source is identical to destination an exception will be thrown.(如果source和destination是一个对象,那么会抛出异常)

    • 未完 …
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值