Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var proto = Object.getPrototypeOf
- , keys = Object.keys
- , class_of = {}.toString
- , arrayp = Array.isArray
- function objp(object) {
- return class_of.call(object) == '[object Object]'
- }
- function copy_array(object, deep) {
- return object.map(function(value) {
- return copy(value, deep) })
- }
- function copy_obj(object) { var result
- result = Object.create(proto(object))
- keys(object).forEach(function(key) {
- result[key] = copy(object[key], true) })
- return result
- }
- function copy(object, deep) {
- return arrayp(object)? copy_array(object, deep)
- : objp(object)? copy_obj (object, deep)
- : /* by value */ object
- }
- var x = {a:1, b:2}
- var y = Object.create(x); y.c = 3;
- var z = {x: x, y: y}
- var a = Object.create(z);
- a.list = [[1, 2], [3,[4,[5]]]]
- var _ = copy(a, true)
- a.list[1][0] = 4
- console.log(_.list[1][0] === 3)
- _ = copy(z, true)
- x.a = 2
- console.log(_.x.a === 1)
Add Comment
Please, Sign In to add comment