0

I'm having difficulties to create new instance from an object in javascript. Try to read some answer, but still no luck.

var general_obj = {
    title : '',
    count : 0,
    details : [],
    status : 'OK'
}

function processData(input){
    var result = Object.create(general_obj);

    return result;
}

I wanted to create new instance from general_obj. I'm expecting the result would have the same structure as general_obj, whereby in my case the return become only

{}

instead of:

{
    title : '',
    count : 0,
    details : [],
    status : 'OK'
}

How can I achieve it ?

5
  • @Tushar editted. thanks Commented May 5, 2017 at 7:31
  • Your code works. It's just that the inherited properties are not logged. Try console.log(result.count) - it's 0. Commented May 5, 2017 at 7:32
  • editted to be more specific.. sorry for incomplete code Commented May 5, 2017 at 7:40
  • I see you've reverted my conversion to snippet. I'm sorry if I didn't understand your intention. Commented May 5, 2017 at 7:41
  • Gerry - Depends what you mean by "instance". If you mean "copy" then Object.create() is the wrong method. If you mean "has a link to general_obj as its prototype" then that's what your code is doing. Commented May 5, 2017 at 7:44

3 Answers 3

3

You may want to use Object.assign({}, general_obj) to create a new object, but with all fields copied from general_obj.

Keep in mind that this is a shallow copy however, and both the new instance and old one would be pointing to the same array through the details field.


The Object.create function allows you to specify a prototype for the object that is created, which would result in similarish behaviour to what you want functionally, will result in you seeing the results you're observing.

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

1 Comment

yeah, I did try this assign, however the details [] keep increasing. I think because of "pointing to the same array" as you mentioned. Thanks !
0

Try the following instead:

function general_obj(title, count, details, status) {
    this.title = title;
    this.count = count;
    this.details = details;
    this.status = status;
}

Then instantiate with:

var result = new general_obj("", 0, [], "OK");

3 Comments

OK, and how would you create another instance based on the first one?
Is this expected from OP??
"I wanted to create new instance from general_obj. I'm expecting the result would have the same structure as general_obj..."
-1
var general_obj = {
    title : '',
    count : 0,
    details : [],
    status : 'OK'
}

function processData(input){
    var result= (JSON.parse(JSON.stringify(general_obj)));

    result.title = 'two';
    result.count = 10;
    result.details.push({
        "in-hand": 4,
        "warehouse": 6
    });
}

With json you can clone your object...

5 Comments

Note that this doesn't work if the object has methods. Or circular references.
I know, but in this case can be an easy way to approach
Thanks for this, but I don't understand. How come stringify and object and parse it again become a clone ?
There's no need to serialize and deserialize the object just so you get a new instance. These are fast operations, true, but compared to the alternatives - Object.assign, {...} syntax or just copying field by field in a for loop they're quite expensive and unwarranted.
You're welcome. Stringify make a string rappresentation of your object in Json. Next you pass this string in json.parse that create a js object from a Json string. The result is you have a new js object equal to the startin object. As @nnnnnn say, this approach work only if your starting obj is "flat", without methods or circular references. In that case you need to crate a class, and instantiate that class.

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.