Calling a function without parentheses returns whole function as a string

前端 未结 6 1179
一生所求
一生所求 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 01:58

    You must use () to call a function in JavaScript.

    If you do not use parenthesis, you are simply referencing the function, which can be useful in a case where you would like to pass a function to another code block for deferred execution.

    In your situation, because you wish to call add right away, you should be sure to use ()

    obj.add();
    

提交回复
热议问题