Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Looping with map:

players = [new Player('foo'), new Player('bar'), new Player('baz')];

var i = 42;
players.map(function (p) {

    p_ref = p;
    p_ref.life = i;
    p.print();
    i++;
});

function Player(name) {
    this.name = name;
    this.life = 10;

    this.print = function () {
        console.log("Player " + this.name + " has " + this.life + " life.");
    };
}

Looping with for in:

players = [new Player('foo'), new Player('bar'), new Player('baz')];

var i = 42;
for (p in players) {
    p_ref = players[p];

    p_ref.life = i;
    p_ref.print();
    i++;
}

function Player(name) {
    this.name = name;
    this.life = 10;

    this.print = function () {
        console.log("Player " + this.name + " has " + this.life + " life.");
    };
}

Quick interview question:

What does this output ?

hash = {foo: "bar", baz: "qux"};
hash2 = hash;
hash2.baz = "toto";
console.log(hash);
console.log(hash2);

nb = 2;
nb2 = nb;
nb2 = 3;
console.log(nb);
console.log(nb2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment