suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Angular 的 copy 函数,看不太懂,求分析下

function copy(source, destination, stackSource, stackDest) {
  if (isWindow(source) || isScope(source)) {
    throw ngMinErr('cpws',
      "Can't copy! Making copies of Window or Scope instances is not supported.");
  }

  if (!destination) {
    destination = source;
    if (source) {
      if (isArray(source)) {
        destination = copy(source, [], stackSource, stackDest);
      } else if (isDate(source)) {
        destination = new Date(source.getTime());
      } else if (isRegExp(source)) {
        destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
        destination.lastIndex = source.lastIndex;
      } else if (isObject(source)) {
        destination = copy(source, {}, stackSource, stackDest);
      }
    }
  } else {
    if (source === destination) throw ngMinErr('cpi',
      "Can't copy! Source and destination are identical.");

    stackSource = stackSource || [];
    stackDest = stackDest || [];

    if (isObject(source)) {
      var index = indexOf(stackSource, source);
      if (index !== -1) return stackDest[index];

      stackSource.push(source);
      stackDest.push(destination);
    }

    var result;
    if (isArray(source)) {
      destination.length = 0;
      for ( var i = 0; i < source.length; i++) {
        result = copy(source[i], null, stackSource, stackDest);
        if (isObject(source[i])) {
          stackSource.push(source[i]);
          stackDest.push(result);
        }
        destination.push(result);
      }
    } else {
      var h = destination.$$hashKey;
      if (isArray(destination)) {
        destination.length = 0;
      } else {
        forEach(destination, function(value, key) {
          delete destination[key];
        });
      }
      for ( var key in source) {
        result = copy(source[key], null, stackSource, stackDest);
        if (isObject(source[key])) {
          stackSource.push(source[key]);
          stackDest.push(result);
        }
        destination[key] = result;
      }
      setHashKey(destination,h);
    }

  }
  return destination;
}
天蓬老师天蓬老师2816 Tage vor1521

Antworte allen(24)Ich werde antworten

  • PHPz

    PHPz2017-04-11 09:03:25

    一般这么长的代码都不会有人看, 真的. 不信你等等看.

    Antwort
    0
  • PHPz

    PHPz2017-04-11 09:03:25

    功力不够,先学习下js基础吧

    Antwort
    0
  • PHP中文网

    PHP中文网2017-04-11 09:03:25

    一般这么长的代码都不会有人看, 真的. 不信你等等看.

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-04-11 09:03:25

    一群菜笔。。。

    Antwort
    0
  • StornierenAntwort