Javascript 对象内部的(在此为var o)
我使用p5.js库制作了一个小沙盒 : 我正在尝试实现一种处理侧边选项的方法,这些选项使用键盘快捷键进行切换 这就是我试图做的 :Javascript 对象内部的(在此为var o),javascript,oop,object,foreach,Javascript,Oop,Object,Foreach,我使用p5.js库制作了一个小沙盒 : 我正在尝试实现一种处理侧边选项的方法,这些选项使用键盘快捷键进行切换 这就是我试图做的 : var options = { Option: function(name, value, shortcut) { this.name = name; this.shortcut = shortcut; this.value = value; this.show = function
var options =
{
Option: function(name, value, shortcut)
{
this.name = name;
this.shortcut = shortcut;
this.value = value;
this.show = function ()
{
var texte = createElement("span",this.name + " : " + this.shortcut + "<br />");
texte.parent("options");
texte.id(this.name);
}
},
toggle: function(shortcut)
{
for (var o in this)
{
console.log(o);
if (o.shortcut == shortcut)
{
o.value = !o.value;
changeSideText("#gravity",gravity);
addText("Toggled gravity");
}
}
}
};
当我调用options.toggle函数时,console.log(o)
会给我“选项”“toggle”。但是我想得到的是for(这里是var o)
来给我对象选项的属性列表,在本例中是重力和paintBackground
我该怎么做
谢谢 创建Option实例时,它不在变量options中,而是在提供的变量中
var gravity = new options.Option("gravity", false,"G");
创建位于重力变量下的选项的实例
var gravity = new options.Option("gravity", false,"G");
您的迭代器for(本例中为var o)使用对象方法的正确输出对选项属性进行迭代
如果希望代码在options变量中存储Option的新实例,可以修改如下代码
var options =
{
instances: [],
Option: function(name, value, shortcut)
{
this.name = name;
this.shortcut = shortcut;
this.value = value;
this.show = function ()
{
var texte = createElement("span",this.name + " : " + this.shortcut + "<br />");
texte.parent("options");
texte.id(this.name);
}
options.instances.push(this);
},
toggle: function(shortcut)
{
for (var i in this.instances)
{
console.log(this.instances[i]);
if (this.instances[i].shortcut == shortcut)
{
this.instances[i].value = !this.instances[i].value;
changeSideText("#gravity",gravity);
addText("Toggled gravity");
}
}
}
};
var选项=
{
实例:[],
选项:函数(名称、值、快捷方式)
{
this.name=名称;
this.shortcut=快捷方式;
这个值=值;
this.show=函数()
{
var texte=createElement(“span”,this.name+) : " + 此快捷方式为“
”;
text.母公司(“期权”);
texte.id(this.name);
}
options.instances.push(这个);
},
切换:功能(快捷方式)
{
for(此.instances中的变量i)
{
console.log(this.instances[i]);
if(this.instances[i].shortcut==快捷方式)
{
this.instances[i].value=!this.instances[i].value;
changeSideText(“重力”,重力);
addText(“切换重力”);
}
}
}
};
这是你想做的例子,但我不认为这是一个可靠的设计模式。
< P>当你创建一个选项的实例时,它不被保存在变量选项中,而是在提供的变量中。var gravity = new options.Option("gravity", false,"G");
创建位于重力变量下的选项的实例
var gravity = new options.Option("gravity", false,"G");
您的迭代器for(本例中为var o)使用对象方法的正确输出对选项属性进行迭代
如果希望代码在options变量中存储Option的新实例,可以修改如下代码
var options =
{
instances: [],
Option: function(name, value, shortcut)
{
this.name = name;
this.shortcut = shortcut;
this.value = value;
this.show = function ()
{
var texte = createElement("span",this.name + " : " + this.shortcut + "<br />");
texte.parent("options");
texte.id(this.name);
}
options.instances.push(this);
},
toggle: function(shortcut)
{
for (var i in this.instances)
{
console.log(this.instances[i]);
if (this.instances[i].shortcut == shortcut)
{
this.instances[i].value = !this.instances[i].value;
changeSideText("#gravity",gravity);
addText("Toggled gravity");
}
}
}
};
var选项=
{
实例:[],
选项:函数(名称、值、快捷方式)
{
this.name=名称;
this.shortcut=快捷方式;
这个值=值;
this.show=函数()
{
var texte=createElement(“span”,this.name+) : “+this.shortcut+”
“;
text.母公司(“期权”);
texte.id(this.name);
}
options.instances.push(这个);
},
切换:功能(快捷方式)
{
for(此.instances中的变量i)
{
console.log(this.instances[i]);
if(this.instances[i].shortcut==快捷方式)
{
this.instances[i].value=!this.instances[i].value;
changeSideText(“重力”,重力);
addText(“切换重力”);
}
}
}
};
这是你想做的例子,但我不认为这是一个可靠的设计模式。
是的,除了我现在明白了什么是错……而且,是的,我对这个设计也不满意,我认为从一开始它就有缺陷,因为它会求助于这种技巧。你有什么建议可以帮助我吗?改进那个设计?是的,除了我现在理解错了什么…而且,是的,我也不满意这个设计,我认为它从一开始就是有缺陷的,因为它迫使我采取这种伎俩。你有什么建议来帮助我改进那个设计吗?重力
和绘画背景
都是易变的s、 不是选项
对象的属性。你为什么认为它们是?重力
和画图背景
是普通变量,而不是选项
对象的属性。你为什么认为它们是?