Thứ Sáu, 20 tháng 12, 2013

Javascript clone object

cloneObj = function(obj){
  if(ko.isWriteableObservable(obj)) return ko.observable(obj()); //this is the trick
  if(obj === null || typeof obj !== 'object') return obj;

  var temp = obj.constructor(); // give temp the original obj's constructor
  for (var key in obj) {
    temp[key] = cloneObj(obj[key]);
  }

  return temp;
};
(http://stackoverflow.com/questions/14055279/javascript-clone-object-with-knockoutjs-observable-properties?rq=1)