3

This is my first question, so please be gentle.

I'm trying to create an array of objects, where the object includes a method/function to retrieve data.

So eventually I don't want to use getFirstName(arrayName, arrayNumber), but instead use something like

aliens[x].getFirstName;

or

persons.first.getFirstName;

If it's not possible in Javascript, or possible in this manner, please suggest the next best thing.

var persons = [

{
    firstname : "John",
    lastname  : "Doe",
    age       : 50,
    eyecolor  : "blue",
},

{
    firstname : "Jane",
    lastname  : "Siyabonga",
    age       : 39,
    eyecolor  : "brown",
},
]

var aliens = [

{
    firstname : "Bob",
    lastname  : "the Alien",
    age       : 350,
    eyecolor  : "yellow",
},

{
    firstname : "xblanque",
    lastname  : "the Predator",
    age       : 19,
    eyecolor  : "red",
},

]

function getFirstName(arrayName, arrayNumber)
{
    var x = arrayName[arrayNumber].firstname;
    return x;
}

Array.prototype.getLastName=function()
{
    var x = this.lastname;
    return x;
}
j08691
  • 204,283
  • 31
  • 260
  • 272

2 Answers2

5

ECMAscript supports getter and setter methods. In any ES5 compatible implementation, we can use it like

var obj = {
    foo: 42,
    bar: 32,
    get getFoo() {
        return this.foo;
    },
    get sum() {
        return this.foo + this.bar;
    }
};

now we can acces it like

obj.getFoo;  // 42
obj.sum;     // 74

Another way to create those is ES5's Object.defineProperty. For instance:

Object.defineProperty(obj, 'getBar', {
    get: function() {
        return this.bar;
    }
});
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 3
    getters and setters are great! – coma May 06 '13 at 13:48
  • I have never heard of ECMAscript, as I am a self-taught programmer. I'm going to google it now, but I'd like to know if it's default in all/most browsers ? IF I were to use this, would it work ? – MisterBrownZA May 07 '13 at 08:37
  • @MisterBrownZA: ECMAscript and JavaScript are one of a kind. For the compatibility, it will work in all latest browsers 100%. It will also work in older versions for most browsers. Only legacy browsers like IE 6-7 might have a problem with this syntax. – jAndy May 08 '13 at 10:08
1

aliens[x].firstname; works fine. But, as @jAndy suggested, you can make use of getters and setters, like any OOP language.

function Alien( firstname, lastname, age, eyecolor) { // constructor

    // ...

    this.getFirstName = function() {
        return firstname;
    };
}

var aliens = [
    new Alien("Bob", "the Alien", 350, "yellow"),
    new Alien("xblanque", "the Predator", 19, "red")
];

console.log(aliens[0].getFirstName()); // will output "Bob"

Also, beware Array.prototype manipulation: you are adding getLastName method to any array present in your code.

Tasso Evangelista
  • 1,612
  • 1
  • 16
  • 23
  • That's what I was afraid of, and thought that it was wrong. Just to confirm, when you say new Alien("","","","") are you calling the function from above, or are you creating an object? – MisterBrownZA May 07 '13 at 08:35
  • @MisterBrownZA Creating a object, because I'm using `Alien` as constructor function. You must understand that in Javascript *everything* behaves like a object. About functions, you must pay attention to the concept of "closure". When a function is called with operator `new`, a object is allocated in closure of function and its reference is `this`. Also, `new` ensures that `this` will be returned. – Tasso Evangelista May 07 '13 at 12:38