clone.js 496 B

1234567891011121314151617181920212223
  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. }