Calling a function without parentheses returns whole function as a string

前端 未结 6 1125
一生所求
一生所求 2021-01-19 01:10

I created a JavaScript object like this:

var obj = {
  a: 10,
  b: 20,
  add: function(){
     return this.a + this.b;
  }
};

I executed the

6条回答
  •  野的像风
    2021-01-19 02:02

    Calling a function requires the () because they are the function invocation operator. To execute a function you will always need to include parentheses.

    When you call ambes.add you are returning the function object itself. If you do so inside console.log() or concatenate it onto a string JavaScript will return the function definition as a complete string which explains the first output you receive.

提交回复
热议问题