Return Keyword in Javascript function.

jskfan
jskfan used Ask the Experts™
on
Return Keyword in Javascript function.
I have the code below that runs and gets the results as expected:
var person = function(name,age,state){
var temp={};
	temp.name = name;
	temp.age = age;
	temp.state = state;

temp.printperson=function(){
	document.write(this.name);
};
[b]return[/b] temp;
};

var person1=person('Omar','25','CS');

person1.printperson();

Open in new window


I would like to know what the keyword "return" does in the code above.

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014
Commented:
not matter the language, return put same thing in a stack. When a program call a function, printperson here, hit take what is on the top of the stack
in your case temp, an object :
{name: "Omar", age: "25", state: "CS", printperson: ƒ}

Open in new window

Author

Commented:
online definition :
In JavaScript, return statements cease execution in a function and return a value to the caller; JavaScript functions require an explicit return statement for returning the result of an expression (the value) from a function

in the example above, the RETURN keyword is already at the end of the function, and the RETURN does not return any value

the way i understand the code above is:

this will assign values to the parameters:
var person1=person('Omar','25','CS');

and this will call the function :
person1.printperson();
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
and the RETURN does not return any value

It is returning a value, it is returning the "temp" object .

So when you are calling the "person" function, you are passing in those 3 parameters and it is returning an object. That object (the one originally called "temp" inside the function) then gets assigned into the "person1" variable.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
probably this statement confused me:
temp.printperson=function(){
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
So are you ok with it now though, or you still need more clarification on that?

Basically, lines 7,8 & 9 of the above is just defining a function, and assigning that function to the "printperson" member of the "temp" object. It is this function that eventually gets called on line 15.

Author

Commented:
if someone can explain it line by line, that might make sense.

for instance here:
***var person1=person('Omar','25','CS');
 we are passing the Values to the first  Function()

****temp.name = name;
      temp.age = age;
      temp.state = state;

then the values got assigned to the elements of object Temp



****temp.printperson=function()
this is  "element" temp function instead of attribute

=========
now what values are in the temp object ,when using return temp;    ??

and what is this statement below doing ? is it calling the printperson() function
person1.printperson();

if I understand person1 is an instance of person, and not of the temp, how come it is calling printperson
*****
========================
to sum it up:
If someone can break it down line by line, and explain how those functions or objects are tied to each other, then that might help

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial