Use console.log() to print the method of an object

- 1 answer

Ad

I'm trying to get the following to print out -1, but instead it just displays the function.

function Builder(first, second) {
  this.first = first;
  this.second = second;
  this.equation = function(){
    return this.first - this.second;
  };
}
var test = new Builder(2,3);
console.log(test.equation);
Ad

Answer

Ad

Since equation is a method, you need to put paren after it to call the method.

function Builder(first, second) {
    this.first = first;
    this.second = second;
    this.equation = function(){
        return this.first - this.second;
    };
}

var test = new Builder(2,3);
console.log(test.equation());
Ad
source: stackoverflow.com
Ad