/** * Creates a clone of the input (or this) object.
* To avoid unexpected results, one should not use this function to clone arrays.
* If you want to be able to clone object with null prototype, remove the * corresponding statement below (see the comments). * @method clone * @param {Object} obj * @returns {Object|Boolean} Returns a new object or false if the input value is not a valid object. */ clone: function (obj) { var donor; if (obj) { if (typeof obj !== 'object') { return false; // not an object } donor = obj; } else { donor = this; } if (!donor.__proto__) { // remove this statement to be able to clone object with __proto__ === null return false; // has not prototype } if (typeof donor.__proto__ !== 'object') { return false; // prototype is not an object } let result = Object.create(donor.__proto__); for (let key in donor) { if (!donor.hasOwnProperty(key)) { continue; } result[key] = donor[key]; } return result; }