使用JavaScript从函数内访问数组

时间:2021-06-11 15:53:03

I searched the forum but didn't see a similar question or answer for the question I am about to ask, however if it is duplicated please provide a link and I will view the answer in that post.

我搜索了论坛,但没有看到类似的问题或答案我要问的问题,但是如果它是重复的,请提供一个链接,我将在该帖子中查看答案。

Is it possible to access objects in a array from within a closure/ function? I am attempting to do so for the purpose of experimentation.

是否可以从闭包/函数中访问数组中的对象?我试图这样做是为了实验。

Here is the code I put together in firebug. I am receive 'undefined'.

这是我在firebug中放在一起的代码。我收到'未定义'。

var checkers = [1, "string", null];

var i, txt = "";

function myFunction(checkers){
    for(var i = 0; i < checkers.length; i++ ){
        txt += checkers[i] + " ";
        console.log(txt)
    }
}

myFunction(checkers);

3 个解决方案

#1


1  

Absolutely possible, one of JavaScript's strenghts.

绝对可能,JavaScript的优势之一。

Your issue is that you aren't calling your function. Also you don't need a parameter to myFunction since it closes over the context and knows about checkers.

您的问题是您没有调用您的功能。此外,您不需要myFunction的参数,因为它关闭了上下文并了解了检查器。

Just delete the paramter and after your code add myFunction() to call it.

只需删除参数,然后在代码后添加myFunction()来调用它。

var checkers = [1, "string", null];

var i, txt = "";

function myFunction(){
    for(var i = 0; i < checkers.length; i++ ){
        txt += checkers[i] + " ";
        console.log(txt)
    }
}

myFunction();

The reason you are getting undefined is because theres not value returned to the console when declaring a function, just like you'd get undefined if you ran:

您未定义的原因是因为在声明函数时没有返回到控制台的值,就像您在运行时未定义一样:

var t = 'asd'

#2


0  

var checkers = [1, "string", null];

var i, txt = "";

function myFunction(checkers){
    for(var i = 0; i < checkers.length; i++ ){
        txt += checkers[i] + " ";
        console.log(txt)
    }
}

myFunction(checkers);

#3


0  

I just ran it and I get:

我跑了它然后得到:

1
1 string
1 string null 
undefined

Since the function doesn't return anything, Firebug displays undefined at the end.

由于该函数不返回任何内容,因此Firebug最后显示为undefined。

#1


1  

Absolutely possible, one of JavaScript's strenghts.

绝对可能,JavaScript的优势之一。

Your issue is that you aren't calling your function. Also you don't need a parameter to myFunction since it closes over the context and knows about checkers.

您的问题是您没有调用您的功能。此外,您不需要myFunction的参数,因为它关闭了上下文并了解了检查器。

Just delete the paramter and after your code add myFunction() to call it.

只需删除参数,然后在代码后添加myFunction()来调用它。

var checkers = [1, "string", null];

var i, txt = "";

function myFunction(){
    for(var i = 0; i < checkers.length; i++ ){
        txt += checkers[i] + " ";
        console.log(txt)
    }
}

myFunction();

The reason you are getting undefined is because theres not value returned to the console when declaring a function, just like you'd get undefined if you ran:

您未定义的原因是因为在声明函数时没有返回到控制台的值,就像您在运行时未定义一样:

var t = 'asd'

#2


0  

var checkers = [1, "string", null];

var i, txt = "";

function myFunction(checkers){
    for(var i = 0; i < checkers.length; i++ ){
        txt += checkers[i] + " ";
        console.log(txt)
    }
}

myFunction(checkers);

#3


0  

I just ran it and I get:

我跑了它然后得到:

1
1 string
1 string null 
undefined

Since the function doesn't return anything, Firebug displays undefined at the end.

由于该函数不返回任何内容,因此Firebug最后显示为undefined。