插入代码时,JavaScript“提示”命令不起作用

时间:2015-10-12 18:21:59

标签: javascript

我正在尝试创建一个搜索文本块的项目,然后将某些值推送到对象的属性,但每当我将一个变量放在ingamePrices对象内部时,就在这个文本块的底部附近,

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);

它会导致“提示”命令停止工作。有谁知道为什么,或者如何解决它?

1 个答案:

答案 0 :(得分:0)

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 };
})()