clone.js 401 B

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