简体   繁体   English

获取变量的所有初始值的最快方法是什么? JS

[英]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 . 但是,执行此操作的最简单方法可能是使用JSON.stringify将对象序列化为JSON。

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: 然后,在应用程序结尾,当您想再次访问该原始对象时,可以将其重新水化为新的JavaScript对象:

original = JSON.parse(original);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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