Calling a function without parentheses returns whole function as a string

前端 未结 6 1127
一生所求
一生所求 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:52

    You didn't execute the function with obj.add, you only looked it up in the object and the environment you're in happened to render the function as a string. You execute it by adding the parentheses.

提交回复
热议问题