我知道JS中的字符串可以分成多行,但是矩阵呢?
$('#map').gameMap({map:[[{"tile":"grass_0","object":""},{"tile":"grass_0","object":""},{"tile":"grass_3","object":""}],[{"tile":"grass_0","object":""},{"tile":"grass_3","object":""},{"tile":"grass_0","object":""}],[{"tile":"grass_1","object":""},{"tile":"grass_0","object":""},{"tile":"grass_2","object":""}]],xpos:-1,ypos:-1,mapsize:3});
我的主管喜欢看到包含在单个页面上的代码,而无需滚动查看冗长的行。
答案 0 :(得分:3)
当然它可以...你可以随意分解它,只要你不在弦的中间断裂。
$('#map').gameMap(
{map:[
[
{"tile":"grass_0","object":""},
{"tile":"grass_0","object":""},
{"tile":"grass_3","object":""}
],
[
{"tile":"grass_0","object":""},
{"tile":"grass_3","object":""},
{"tile":"grass_0","object":""}
],
[
{"tile":"grass_1","object":""},
{"tile":"grass_0","object":""},
{"tile":"grass_2","object":""}
]
],
xpos:-1,
ypos:-1,
mapsize:3
}
);
答案 1 :(得分:2)
JavaScript没有矩阵数据结构。你在这里有一个对象文字({ map : ... }
),它包含一个对象文字数组的数组。
除此之外,正如其他人所指出的那样,你当然可以把它分解成多行,这也让你有机会添加一些合理的缩进,这将使它在未来更容易阅读和修改。就个人而言,我是comma-first style的粉丝,我认为它具有最可读性和最不可能(程序员)错误(即缺少逗号)以及对齐冒号(:
)和健康剂量空白:
$('#map').gameMap(
{ map : [ [ { "tile" : "grass_0", "object" : "" }
, { "tile" : "grass_0", "object" : "" }
, { "tile" : "grass_3", "object" : "" }
]
, [ { "tile" : "grass_0", "object" : "" }
, { "tile" : "grass_3", "object" : "" }
, { "tile" : "grass_0", "object" : "" }
]
, [ /* and so on... */
]
]
, xpos : -1
, ypos : -1
, mapsize : 3
}
);
答案 2 :(得分:1)
也许是这样的:
$('#map').gameMap({map:[[{"tile":"grass_0","object":""},
{"tile":"grass_0","object":""},
{"tile":"grass_3","object":""}],
[{"tile":"grass_0","object":""},
{"tile":"grass_3","object":""},
{"tile":"grass_0","object":""}],
[{"tile":"grass_1","object":""},
{"tile":"grass_0","object":""},
{"tile":"grass_2","object":""}]],
xpos:-1,
ypos:-1,
mapsize:3});
答案 3 :(得分:1)
您可以使用jsfiddle来整理JavaScript代码。这是它的输出:
$('#map').gameMap({
map: [[{
"tile": "grass_0",
"object": ""},
{
"tile": "grass_0",
"object": ""},
{
"tile": "grass_3",
"object": ""}], [{
"tile": "grass_0",
"object": ""},
{
"tile": "grass_3",
"object": ""},
{
"tile": "grass_0",
"object": ""}], [{
"tile": "grass_1",
"object": ""},
{
"tile": "grass_0",
"object": ""},
{
"tile": "grass_2",
"object": ""}]],
xpos: -1,
ypos: -1,
mapsize: 3
});