clone.js 496 B

123456789101112131415161718192021222324
  1. 'use strict'
  2. module.exports = clone
  3. var getPrototypeOf = Object.getPrototypeOf || function (obj) {
  4. return obj.__proto__
  5. }
  6. function clone (obj) {
  7. if (obj === null || typeof obj !== 'object')
  8. return obj
  9. if (obj instanceof Object)
  10. var copy = { __proto__: getPrototypeOf(obj) }
  11. else
  12. var copy = Object.create(null)
  13. Object.getOwnPropertyNames(obj).forEach(function (key) {
  14. Object.defineProperty(copy, key, Object.getOwnPropertyDescriptor(obj, key))
  15. })
  16. return copy
  17. }