0

The following is my javascript code for accessing a private method. But it is not working. I receive a TypeError: string is not a function message. Can anyone please help me?

Here is my code:

function Boy(firstName,lastName) {
    this.fisrtName = firstName;
    this.lastName = lastName ;
    var ladyLove = "Angelina";

    var returnLove = function() {
        return ladyLove;
    };

    this.sayLoud = function(){
        return returnLove();
    };
}

var achilles = new Boy("Bradley","Pitt");
var sayNow = achilles.sayLoud();
console.log(sayNow());
Ramvignesh
  • 210
  • 6
  • 16

3 Answers3

3

sayLoud() returns Angelina - which is a String, not a function.

You probably just want to go for:

console.log(sayNow);
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
RienNeVaPlu͢s
  • 7,442
  • 6
  • 43
  • 77
2

Instead of using a string as a function, you should use console.log(sayNow);

Explained:

var achilles = new Boy("Bradley","Pitt"); // Will create a new object
var sayNow = achilles.sayLoud(); // call sayLoud(), return string
console.log(sayNow); // output the string
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
0

try this

var achilles = new Boy("Bradley","Pitt");
var sayNow = achilles.sayLoud; 
console.log(sayNow());
volkinc
  • 2,143
  • 1
  • 15
  • 19