Page 1 of 1
Multiple return statements
Posted: Thu Jun 05, 2014 3:34 pm
by Mardonis
I'm having an issue with a piece of code that i have in a function. Using Netbeans with jslint.
Code: Select all
function addItem(e) {
'use strict';
var t = {},
n = e.name;
return $.extend(t, this.defaults), $.extend(t, this.list[n]), t.life = t.hitPoints, $.extend(t, e), t;
}
Where the first "," and "$" are, I am getting a error that says: Expected ';' and instead saw ",". I have looked up on how to do multiple return statments and it says you can use commas so I'm at a loss on what I'm doing wrong. Would I need to make more variables and assign them to the varius return statements and then just return the multple variables? Also would that work for easablity of reading it by doing that?
Thanks
Mardonis
Re: Multiple return statements
Posted: Thu Jun 05, 2014 9:19 pm
by srachit
Re: Multiple return statements
Posted: Fri Jun 06, 2014 1:55 am
by rockinliam
Best to return an array of your data, nice and simple, in other languages you might use a struct or something like that.
Re: Multiple return statements
Posted: Fri Jun 06, 2014 2:54 am
by Jackolantern
As others have mentioned, this is not possible. You cannot use commas in return statements in Javascript this way. You will either need to return an array or an object with the values.
Re: Multiple return statements
Posted: Fri Jun 06, 2014 12:40 pm
by Mardonis
Will get on it. Thank you for the good info.
Re: Multiple return statements
Posted: Fri Jun 06, 2014 1:55 pm
by hallsofvallhalla
I am going to be a little goofy and give another way of doing things. Not saying it is the right way but just for sh&ts and giggles, i need a break from staring at C# and Linq.
Code: Select all
var ReturnedResult = GetResult();
var Results = ReturnedResult.split(",");
function GetResult()
{
var First = "First";
var Second = "Second";
var Third = "Third";
var FinalyResult = First + "," + Second + "," + Third;
return FinalResult
}
Re: Multiple return statements
Posted: Fri Jun 06, 2014 2:08 pm
by Mardonis
Thank you for this example. I'll take a look in doing this one also.
Re: Multiple return statements
Posted: Fri Jun 06, 2014 2:10 pm
by hallsofvallhalla
i would definitely use a single array over the way I did it.
Re: Multiple return statements
Posted: Sat Jun 07, 2014 12:21 am
by Jackolantern
Yeah, I wouldn't suggest to do it that way at all haha

Re: Multiple return statements
Posted: Fri Jun 20, 2014 2:25 am
by Chris
That doesn't look right unless it were Python, but that's not Python. Make a list with square brackets.
Code: Select all
return [$.extend(t, this.defaults), $.extend(t, this.list[n]), t.life = t.hitPoints, $.extend(t, e), t];