0
function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

// now we can easily make all of our rabbits
rabbit1 = new Rabbit("fluffy");
rabbit2 = new Rabbit("happy");
rabbit3 = new Rabbit("sleepy");

console.log(rabbit1.describeMyself());
console.log(rabbit2.describeMyself());
console.log(rabbit3.describeMyself());

The output:

I am a fluffy rabbit
undefined
I am a happy rabbit
undefined
I am a sleepy rabbit
undefined

Would it stop the undefined if I were to execute it as a .js file instead of a console? Or would it be the same?

  • 1
    When the code is run in browser console, the `describeMyself()` will return `undefined` as nothing is returned from that function. – Tushar Mar 14 '16 at 03:25
  • *"It's working correctly, but it outputs undefined?"* - So in fact it is working *incorrectly*? – nnnnnn Mar 14 '16 at 03:25
  • Here's improved code which **defines function on prototype**: `function Rabbit(adjective) { this.adjective = adjective; } Rabbit.prototype.describeMyself = function() { return "I am a " + this.adjective + " rabbit"; }; // now we can easily make all of our rabbits rabbit1 = new Rabbit("fluffy"); rabbit2 = new Rabbit("happy"); rabbit3 = new Rabbit("sleepy"); console.log(rabbit1.describeMyself()); console.log(rabbit2.describeMyself()); console.log(rabbit3.describeMyself());` – Tushar Mar 14 '16 at 03:27

3 Answers3

1

You are trying to console.log a function which will return nothing so result is shown as undefined. You need to just call the function as you already have console.log within it.. Try this

rabbit1.describeMyself(); rabbit2.describeMyself(); rabbit3.describeMyself();

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
0

describeMyself() doesn't return anything explicitly so it returns undefined for each console.log(rabbit1.describeMyself()) implicitly

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • How can I fix this issue? Do I add return; in the function. – XTImpossible Mar 14 '16 at 03:27
  • @XTImpossible if you just want to print the message in `describeMyself()`, you don'y need `console.log(rabbit1.describeMyself())` , just call `describeMyself()` directly or if u want to do `console.log(rabbit1.describeMyself())`, then return the string from `describeMyself()` rather then doing `console.log()` in `describeMyself()` – Ramanlfc Mar 14 '16 at 03:30
0

Your code is equivalent to this

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
        return undefined; // <-- without a return statement, undefined will be returned.
    };
}

// now we can easily make all of our rabbits
rabbit1 = new Rabbit("fluffy");

var result = rabbit1.describeMyself(); // <-- the message logged to console and return undefined
console.log(result); // and print undefined again

So the solution is return string from the method


function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        return "I am a " + this.adjective + " rabbit"); // <-- return a string
    };
}

// now we can easily make all of our rabbits
rabbit1 = new Rabbit("fluffy");

console.log(rabbit1.describeMyself()); // <-- log the string value

or remove extra logs

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

// now we can easily make all of our rabbits
rabbit1 = new Rabbit("fluffy");

rabbit1.describeMyself(); // <-- this method will print to console and return undefined
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143