gpt4 book ai didi

javascript - 自动创建 Javascript 对象并将其存储在另一个对象中

转载 作者:行者123 更新时间:2023-11-28 02:03:07 25 4
gpt4 key购买 nike

所以我想创建一个 JavaScript 函数,它能够生成名称,然后将其设置为变量,然后创建一个对象。在下面的代码中,我想出了一个输入表单的示例,尽管我只显示了我的 Javascript。假设用户已经输入过一次数据来创建咖啡对象。阅读评论

function object(a,b){
this.validate = 1
this.one = a
this.two = b
if(z == "undefined"){
var z = 0;
}else{
z = z++;
gen(z);
}
}


/*
Need a function that is able to generate generate names and set them as a variable
This case for example the name of the object I'm trying to generate is coffee3
*/
function gen(x){
if(coffee.validate != "undefined"){
for(x, x < (x++), x++){
var y = x.toString();
y = "coffee" + y;

/*
Turning the string value stored in y currently, say coffee3 for example,
into a variable named coffee3 and gets new object("caramel", "milk")

Also a code to store an object inside the sessionStorage object.

if(typeof(Storage)!=="undefined")

to check storage first of course
*/

}
}else{
var coffee = new object("stuff", "input");
}
}

当然,我并没有完全使用这个例子。为了清楚起见,我使用它,我对这样一个函数的真正用途是基本的客户端存储输出,用于在我的服务器脚本上加载 CAS,该脚本返回更多客户端脚本并在服务器脚本上加载自定义 S(CC)AI-S/API浏览器。

最佳答案

这实际上是 JavaScript 最好的方面之一。

假设您将所有这些用户生成的对象保存到一个通用列表中,例如:

objList = {};
objList.coffee = new object("stuff", "input");

有趣的是,现在您可以通过两种方式读取 coffee:使用 objList.coffee 和使用 objList['coffee']。写作也是如此。所以我们可以将示例重写为:

objList = {};
objList['coffee'] = new object("stuff", "input");

瞧!您现在基本上使用字符串来标识变量。嗯,有点 - 它们不再是变量,而是 JS 对象的属性。但仍然如此。

关于javascript - 自动创建 Javascript 对象并将其存储在另一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18174737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com