1

function Add(a, b){
 this.a = a, this.b = b;
 return this.a + this.b;
}

var add5 = new Add(2, 3); // ? add5 = 5

console.log(add);

"typeof add5" is Object, and "add5 instanceof Add" is true.

My question is:

  1. On this case, how can I return value 5 that's I want?
  2. In the function Add, I explicitly return 5, why do I get return Add instance?
  3. Does new Function() only return this instance?
soarinblue
  • 1,517
  • 3
  • 21
  • 30

4 Answers4

2

1. On this case, how can I return value 5 that's I want?

With a simple function and with direct call without the new operator.

function add(a, b) {
    return a + b;
}

document.write(add(2, 3));

2. In the function Add, I explicitly return 5, why do I get return Add instance?

3. Does new Function() only return this instance?

You get the instance, because of the new operator.

3.The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)

According to this, a new instance does not return a primitive data type. You can only overwrite the return value (this) with an object.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • _(Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)_ I have a doubt with this line. Then why didn't it return a value in OP's case? – Rajaprabhu Aravindasamy Mar 31 '16 at 06:00
  • This answered the 2nd, 3rd question, thanks a lot. – soarinblue Mar 31 '16 at 06:22
  • @RajaprabhuAravindasamy: Because the returned value was not an object. – Felix Kling Mar 31 '16 at 14:01
  • @FelixKling Yeah I read the algorithm to clear that. But I still confused about that. I have returned null from a constructor, though an object was returned instead of null. typeof null is object. right? – Rajaprabhu Aravindasamy Mar 31 '16 at 14:10
  • @RajaprabhuAravindasamy: while `typeof null` returns the string "object", `null` really is a primitive value of type Null. – Felix Kling Mar 31 '16 at 14:22
1

Isn't this much simpler ...

function Add(a, b){
            return a + b;
}

var add = Add(2, 3);    // ? add = 5
console.log(add);

Or maybe this if you want a method related Instance

function AddNumbers(a, b){
    this.a = a;
    this.b = b;
    return this;

}
AddNumbers.prototype.add = function(){
      var sum  = this.a+this.b;
      console.log(sum);
      return sum;
}

var addObject = new AddNumbers(2, 3);   // ? add = 5
addObject.add();
damitj07
  • 2,689
  • 1
  • 21
  • 40
1

The new operator is used to create an object. In your case, add5.a and add5.b are populated with 2 and 3 respectively.

You should do the following.

function Add(a, b){
    this.a = a, this.b = b;
    this.result = function(){
        return this.a + this.b;
    }
}

and use it like

var add5 = new Add(2, 3);   // ? add5 = 5

console.log(add5.result());
U.P
  • 7,357
  • 7
  • 39
  • 61
0

You have to remove the 'new' keyword since that creates a new object out of that function. Just call the function the following way.

function Add(a, b){
 this.a = a, this.b = b;
 return this.a + this.b;
}

var add = Add(2, 3); // ? add = 5

console.log(add);