jQuery的延长VS角延伸 [英] jquery extend vs angular extend

查看:151
本文介绍了jQuery的延长VS角延伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这两个扩展功能之间的区别?

What is the difference between these two extend functions?

  angular.extend(a,b);
  $.extend(a,b);

虽然jquery.extend是有据可查的angular.extend缺乏细节和那里的说明不提供答案。 (<一href=\"https://docs.angularjs.org/api/ng/function/angular.extend\">https://docs.angularjs.org/api/ng/function/angular.extend).

是否angular.extend还提供深层副本?

Does angular.extend also provide deep copy?

推荐答案

angular.extend jQuery.extend 是的非常相似。他们都做的的属性副本从一个​​或多个源对象到目标对象。因此,例如:

angular.extend and jQuery.extend are very similar. They both do a shallow property copy from one or more source objects to a destination object. So for instance:

var src = {foo: "bar", baz: {}};
var dst = {};
whatever.extend(dst, src);
console.log(dst.foo);             // "bar"
console.log(dst.baz === src.baz); // "true", it's a shallow copy, both
                                  // point to same object

angular.copy 提供的复制:

var src = {foo: "bar", baz: {}};
var dst = angular.copy(src);
console.log(dst.baz === src.baz); // "false", it's a deep copy, they point
                                  // to different objects.

再回到延长:我只能检测到一个显著的差异,这是jQuery的延长允许您指定只有一个对象,在这种情况下,的jQuery 本身是目标。

Getting back to extend: I only see one significant difference, which is that jQuery's extend allows you to specify just one object, in which case jQuery itself is the target.

共同点:


  • 这是一个浅拷贝。所以,如果的src 有一个引用一个对象的属性 P DST 将得到一个属性 p 这是指的相同的对象(而不是对象的副本)。

  • It's a shallow copy. So if src has a property p that refers to an object, dst will get a property p that refers to the same object (not a copy of the object).

他们都返回目标对象。

他们都支持多个源对象。

They both support multiple source objects.

他们俩都多源对象的的,所以最后的源对象的情况下,将赢多个源对象具有相同的属性名称。

They both do the multiple source objects in order, and so the last source object will "win" in case more than one source object has the same property name.

测试页:现场复制 | 实时源

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<meta charset=utf-8 />
<title>Extend!</title>
</head>
<body>
  <script>
    (function() {
      "use strict";
      var src1, src2, dst, rv;

      src1 = {
        a: "I'm a in src1",
        b: {name: "I'm the name property in b"},
        c: "I'm c in src1"
      };
      src2 = {
        c: "I'm c in src2"
      };

      // Shallow copy test
      dst = {};
      angular.extend(dst, src1);
      display("angular shallow copy? " + (dst.b === src1.b));
      dst = {};
      jQuery.extend(dst, src1);
      display("jQuery shallow copy? " + (dst.b === src1.b));
      $("<hr>").appendTo(document.body);

      // Return value test
      dst = {};
      rv = angular.extend(dst, src1);
      display("angular returns dst? " + (rv === dst));
      dst = {};
      rv = jQuery.extend(dst, src1);
      display("jQuery returns dst? " + (rv === dst));
      $("<hr>").appendTo(document.body);

      // Multiple source test
      dst = {};
      rv = angular.extend(dst, src1, src2);
      display("angular does multiple in order? " +
                  (dst.c === src2.c));
      dst = {};
      rv = jQuery.extend(dst, src1, src2);
      display("jQuery does multiple in order? " +
                  (dst.c === src2.c));

      function display(msg) {
        $("<p>").html(String(msg)).appendTo(document.body);
      }
    })();
  </script>
</body>
</html>

这篇关于jQuery的延长VS角延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆