简体   繁体   中英

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

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);

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());

您必须使用如下括号来调用该函数:

console.log(test.equation());

尝试

console.log(test.equation());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM