插入代码时JavaScript“提示”命令不起作用
我试图创建一个项目,搜索一块文本,然后将某些值推送到一个对象的属性中,但是每当我将一个变量放在ingamePrices对象中靠近文本块底部的位置时插入代码时JavaScript“提示”命令不起作用,javascript,Javascript,我试图创建一个项目,搜索一块文本,然后将某些值推送到一个对象的属性中,但是每当我将一个变量放在ingamePrices对象中靠近文本块底部的位置时 var testPrompt = prompt("Let's figure out how this works"); var rawUSDValue = 0.125; function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, stran
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
};
document.write(testPrompt);
所以是这样的
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
var testVariable = "sampleString";
};
document.write(testPrompt);
它会导致提示命令停止工作。有人知道为什么,或者如何修复它吗
var ingamePrices =
{
var testVariable = "sampleString";
};
这可能是对以下两件事之一的尝试:对象文字,或您想象中包含testVariable的块语法。对象文字包含键和值,它们不包含任意表达式或变量定义。作为对象文字,这应该是
var ingamePrices =
{
testVariable: "sampleString"
};
或者,如果确实需要testVariable作为此对象某些内容的上下文,那么:
var testVariable = "sampleString",
ingamePrices =
{
blah: [testVariable, "a use of testVariable"]
};
如果您正在寻找块语法和词法变量,那么JavaScript没有它们。它只有全局变量和函数变量。这意味着像这样的案例成为一个自动执行的功能,纯粹是为了提供范围:
var ingamePrices = (function() {
var testVariable = "sampleString";
...
return { blah: testVariable };
})()
无效的javascript:var ingamePrices={var testVariable=sampleString;};var foo={var是一个语法错误。那么我必须在ingamePrices对象之外生成变量,还是必须执行其他操作?我不知道您在这里尝试执行什么操作。