fr
fr

Question from the Javascript test

Which of the following code will log the message 'baz'?

Hard

Either the following code:

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

    this.identity = function() {
        console.log('foo');
    }
}

Player.identity = function identity() {
    console.log('bar');
}

Player.prototype.identity = function identity() {
    console.log('baz');
}

let myObj = Player('qux'); // Create a player
myObj.identity();

What will be the logged message?

Author: Jean-marie CléryStatus: PublishedQuestion passed 2221 times
Edit
2
Community Evaluations
developer avatar
CĂ©dric
06/01/2025
Rhaaaaaa bien joué !!! TrÚs sournoise cette question ! Bien évidement, je suis tombé dedans... il manquait le "new" à la déclaration, puisqu'ici myObj n'est que le résultat de l'appel à la fonction et non un objet :D