0

我试图找出这段代码是如何工作的:

  <script>
  var object1 = {
      apple: 0,
      banana: {weight: 52, price: 100},
      cherry: 97
  };
  var object2 = {
     banana: {price: 200},
     durian: 100
  };

  /* merge object2 into object1 */
  $.extend(object1, object2);

  var printObj = typeof JSON != "undefined" 
       ? JSON.stringify : function(obj) {
    var arr = [];
  $.each(obj, function(key, val) {
      var next = key + ": ";
      next += $.isPlainObject(val) ? printObj(val) : val;
      arr.push( next );
  });
  return "{ " +  arr.join(", ") + " }";
  };

  $("#log").append( printObj(object1) );
  </script>

我明白什么stringify()isPlainObject()在做什么,我一直在测试代码,我意识到function(obj)没有使用?这真的很令人困惑。我不明白。

4

0 回答 0