Javascript 从函数返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12059478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:02:04  来源:igfitidea点击:

Return array from function

javascriptarraysfunction

提问by Aido

--Solved by Elliot B. Thanks! May also take int account the other modifications.

--已由 Elliot B 解决。谢谢!也可以考虑其他修改。

Here is the result. Thanks, everyone, for the speedy answers! http://dl.dropbox.com/u/18785762/Rust/index.html

这是结果。谢谢大家,快速回答!http://dl.dropbox.com/u/18785762/Rust/index.html

I'm writing a game in javascript, and I want to keep the files for matching block IDs to files in a seperate .js file from the map compiler, so that I can edit things easily. However, the IDs are stored in an array, and I can't seem to get it to use the return function properly. Any help?

我正在用 javascript 编写游戏,并且我想将用于将块 ID 与文件匹配的文件保留在来自地图编译器的单独 .js 文件中,以便我可以轻松地进行编辑。但是,ID 存储在一个数组中,我似乎无法让它正确使用返回函数。有什么帮助吗?

drawmap.js:

drawmap.js:

function drawmap() {

    var images = BlockID();

    var level = [
    "ssssssssssssssssssssss",
    "sgggggggggCCCCCdddddss",
    "ssssssssss     sssssss"
    ];

    var top = 100;
    var left = 100;
    var mytop = top;
    var myleft = left;
    for (y=0; y<level.length; ++y) {
        var row = level[y];
        for (x=0; x < row.length; ++x) {
            var c = row.charAt(x);
            if(c != ' ') {
                img_create(images[c], mytop, myleft);
            }
            mytop += 13;
            myleft += 27;
        }
        mytop = top + (y+1)*13;
        myleft = left - (y+1)*27;
    }
}

mapread.js:

地图读取.js:

function BlockID() {
    var IDs = new Array();
        images['s'] = "Images/Block_01.png";
        images['g'] = "Images/Block_02.png";
        images['C'] = "Images/Block_03.png";
        images['d'] = "Images/Block_04.png";
    return IDs;
}

回答by Elliot B.

At a minimum, change this:

至少,改变这个:

function BlockID() {
    var IDs = new Array();
        images['s'] = "Images/Block_01.png";
        images['g'] = "Images/Block_02.png";
        images['C'] = "Images/Block_03.png";
        images['d'] = "Images/Block_04.png";
    return IDs;
}

To this:

对此:

function BlockID() {
    var IDs = new Object();
        IDs['s'] = "Images/Block_01.png";
        IDs['g'] = "Images/Block_02.png";
        IDs['C'] = "Images/Block_03.png";
        IDs['d'] = "Images/Block_04.png";
    return IDs;
}

There are a couple fixes to point out. First, imagesis not defined in your original function, so assigning property values to it will throw an error. We correct that by changing imagesto IDs. Second, you want to return an Object, not an Array. An object can be assigned property values akin to an associative array or hash -- an array cannot. So we change the declaration of var IDs = new Array();to var IDs = new Object();.

有几个修复要指出。首先,images未在您的原始函数中定义,因此为其分配属性值会引发错误。我们通过更改images为 来纠正这一点IDs其次,你想返回一个Object,而不是一个Array。可以为对象分配类似于关联数组或散列的属性值——数组不能。所以我们将声明更改var IDs = new Array();var IDs = new Object();

After those changes your code will run fine, but it can be simplified further. You can use shorthand notation (i.e., object literal property value shorthand) to create the object and return it immediately:

在这些更改之后,您的代码将运行良好,但可以进一步简化。您可以使用速记符号(即对象文字属性值速记)来创建对象并立即返回它:

function BlockID() {
    return {
            "s":"Images/Block_01.png"
            ,"g":"Images/Block_02.png"
            ,"C":"Images/Block_03.png"
            ,"d":"Images/Block_04.png"
    };
}

回答by Bergi

Your BlockIDfunction uses the undefined variable images, which will lead to an error. Also, you should not use an Arrayhere - JavaScripts key-value-maps are plain objects:

您的BlockID函数使用了 undefined 变量images,这会导致错误。此外,你不应该Array在这里使用- JavaScript 的键值映射是普通对象:

function BlockID() {
    return {
        "s": "Images/Block_01.png",
        "g": "Images/Block_02.png",
        "C": "Images/Block_03.png",
        "d": "Images/Block_04.png"
    };
}

回答by mplungjan

neater:

更整洁:

function BlockID() {
  return {
    "s":"Images/Block_01.png",
    "g":"Images/Block_02.png",
    "C":"Images/Block_03.png",
    "d":"Images/Block_04.png"
   }
}

or just

要不就

var images = {
  "s":"Images/Block_01.png",
  "g":"Images/Block_02.png",
  "C":"Images/Block_03.png",
  "d":"Images/Block_04.png"
}