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

来源:互联网 发布:俄罗斯经济崩溃 知乎 编辑:程序博客网 时间:2024/06/07 03:18

使用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是一个对象,那么会抛出异常)

    • 未完 …
0 0
原创粉丝点击