1

Hi I am new to javascript. I am looking at big javascript code already working fine. It contains following statements :

function(a){
  return this.prt = a,this;
}

and in the client code, they are using it as

obj.a(34).a(54)

I want to know what's happening here ? Does javascript allows returning of multiple values.

Pardoning for may be such a silly question. I have googled out, but couldn't find any good references.

Thanks in advance.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97

2 Answers2

2

I tried running these lines of code on console.

this.name = "yasser";
function demo(){
    return this.name = "neel", 10;
}

And here is what I got,

this.name evaluates to 'neel' and demo() fn returns 10.

So no you cannot return multiple values from javascript. You can return multiple values combining them into one object.

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

I looked at this post How does this return statement with multiple values (variables) works?. However, it's for c language, but I think the rule for comma operator applier here also. Thus, return a,b will result into evaluation of a and then b and returning result of b's evaluation.

Community
  • 1
  • 1
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
  • That's correct. In your example, it's evaluating `this.prt = a` (i.e. assigning the value of `a` to `this.prt`), then it's evaluating `this`, and the result of the entire expression is the last thing evaluated, `this`, which is then returned. – blm Dec 09 '15 at 07:31