2

I wanted to clone original object and function without reference, is my code consider the correct way to clone object and function?

var apple = new function() {
    this.type = "macintosh";
    this.color = "red";
}


function aaa() {
        return this.color + ' ' + this.type + ' apple';
    };

var a = JSON.parse(JSON.stringify(apple))
var b = 
JSON.parse(JSON.stringify(apple));

console.log(a)


a.getInfo = aaa

b.getInfo = aaa

a.color='green' // only a is green color

console.log(a.getInfo())

console.log(b.getInfo())
3
  • At the time of "cloning" your object didn't even contain the function Commented Jul 25, 2017 at 10:10
  • Yes, I shown an example which function is not in the object but every questions I found didn't seem to mention how to clone function existed in the object and discard by JSON parse. Commented Jul 25, 2017 at 10:24
  • So do you care about the functions now or not? Then adjust your example appropriately. Commented Jul 25, 2017 at 10:28

2 Answers 2

1

Try this function:

var clone = function (object) {
  // Copy everything that is not an object
  if (object == null || typeof(object) !== 'object') {
    return object
  }
  // Calling constructor
  var temp = new object.constructor()

  // Recursively cloning children
  for (var key in object) {
    temp[key] = clone(object[key])
  }

  return temp
}

Test:

var test = { a: 0, b: function () { console.log(1) } }
var cloned = clone(test)

https://jsfiddle.net/feshcdLe/1/

Sign up to request clarification or add additional context in comments.

3 Comments

Only a is copied b function is not copied?
@JamesLei did you try it ?
I tried it, b didn't get copy, only a? Could you have a JSFiddle as I'm on mobile.
0

For cloning objects, you can use Object.assign and set the first argument as an empty object. For example

const clone = Object.assign({}, apple.call({}));
const result = aaa.call(clone);
console.log(result);
//=> red macintosh apple;

I made use of Function.call here simply because I don't know if you meant to access the global this or a different scope or what. If you do know what your this is referring to, then you can simply do.

const clone = Object.assign({}, this);
const result = aaa();
console.log(result);

MDN Object.assign

MDN Function.call

6 Comments

That's not what MDN says. It copies the values of the original object. It runs getters too. It only references if the original object contains references.
True, correction, it useful for simple object, I tried to add function into the original object and assign, it didn't get to clone the function to a new object as I like to.
Ah I see. I misunderstood the question then. Perhaps this solution provided by the same assign documentation does what you need it to? Look to the completeAssign function. developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
Almost there, except, I could only use "get bar()" and not "set bar(..)"?
Did the cloned object have a setter defined for bar?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.