简体   繁体   中英

What's the fastest way to get all the initial values of a variable? JS

I want to get the initial values (What I first defined it to be) of all the properties for one of my objects.

I want to do this at the "end" of my game.

I could just copy and paste the properties but the object is pretty big and im wondering if this would slow the site down a bit?

Thanks!

Code example:

var MyObject = {

    nestedObject1 : {
        //...
    },

    nestedObject2 : {
        value1 : "Hi",
        value2 : 1
    },

    objectProperty : 2637

}

How would I get all (Or maybe excluding one of the nested objects) of the properties to their intial value?

I don't quite fully understand how you want to get the initial values, at the end of your running. But probably the easiest way to do this is to just serialize your object to JSON using JSON.stringify .

var original = JSON.stringify(myObject)

Then at the end of your application when you want access to this original object again you can rehydrate it to a new JavaScript object:

original = JSON.parse(original);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM