Your list
variable will be private to f()
unless you do one of two things.
First, you could try returning list
from f()
so that you can then get to the properties you need.
function f() {
var list = [{name: 'test'}, {name: 'test2'}];
return list;
}
var f = f();
f[0] // {name: 'test'};
f['test'] // will return undefined; we'll come back to this
Second, and I think this option is probably what you're looking for as you tagged the question with 'oop', you could make f()
a constructor:
function f() {
this.list = [{name: 'test'}, {name: 'test2'}];
}
var f1 = new f();
f1['list'][0] // {name: 'test'};
f1.list[0] // will also return {name: 'test'};
f1.list['test'] // still will return undefined...
...
The reason you will not be able to access a value using ['test']
or ['test2']
is because they are your values, whereas ordinarily the values are what we want to access in an object using the key (in this case ['name']
or .name
). So what you might want is something like this:
f1.list[0].name // will return 'test'
f1.list[1].name // will return 'test2'
Hope this clears up the confusion.