gpt4 book ai didi

属性中的 JavaScript var

转载 作者:行者123 更新时间:2023-12-02 17:04:46 25 4
gpt4 key购买 nike

我在下面的代码中添加 JS 变量“vpath”时遇到问题:

var vpath = myarr[0];
jwplayer("player").setup({
flashplayer: "player.swf",
config:"config.xml",
autostart: false,
allowscriptaccess: "always",
height: 500,
width: 625,
plugins: {
'http//www.test.com/'+ vpath +'/proxy.swf': {link:"http://www.test.com/link.php"},
'http//www.test.com/image.swf': {link:"http://www.test.com/image.jpg"}
}
});

属性 id 后面有语法错误:缺失:。现在有人该怎么做吗?谢谢。

最佳答案

您不能在 object literals 内的属性名称中使用变量(精确的语法和语义在 §11.1.5 中指定)。解决方案是首先使用静态属性定义对象,然后向对象添加动态属性。

就您而言,我建议单独创建 plugins 对象,如下所示:

var vpath = myarr[0];
var plugins = {};
plugins['http//www.test.com/image.swf'] = {link:"http://www.test.com/image.jpg"}
plugins['http//www.test.com/'+ vpath +'/proxy.swf'] = {link:"http://www.test.com/link.php"};
jwplayer("player").setup({
flashplayer: "player.swf",
config:"config.xml",
autostart: false,
allowscriptaccess: "always",
height: 500,
width: 625,
plugins: plugins
});

另一种替代方案(在某些情况下可能有用)是使用 IIFE :

var vpath = myarr[0];
jwplayer("player").setup({
flashplayer: "player.swf",
config:"config.xml",
autostart: false,
allowscriptaccess: "always",
height: 500,
width: 625,
plugins: (function() {
var obj = {};
obj['http//www.test.com/image.swf'] = {link:"http://www.test.com/image.jpg"}
obj['http//www.test.com/'+ vpath +'/proxy.swf'] = {link:"http://www.test.com/link.php"};
})()
});

关于属性中的 JavaScript var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352464/

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