Skip to content

Instantly share code, notes, and snippets.

@pinouchon
Last active December 22, 2015 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinouchon/6404327 to your computer and use it in GitHub Desktop.
Save pinouchon/6404327 to your computer and use it in GitHub Desktop.

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