List Info

Thread: Function scope mystery




Function scope mystery
user name
2006-04-03 18:15:13
I encountered this example in a beginning Python book that I
have
duplicated in JavaScript. Can someone explain why this
works?
Specifically, how is the parameter "number"
being stored? How are two
parameters being passed?

function multiple(factor) {
	function multiplyBy(number) {
		return number * factor;
	}
	return multiplyBy;
}
var double = multiple(2);

alert(double(5));

[this returns 10]

Is it that the previous parameter (2) becomes
"number" and is stored as
part of double? I've done a lot of fairly advanced
scripting but this
simple example has revealed an area I'm completely ignorant
of. Any
light you might shed on this would be greatly appreciated.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at http://
groups.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---

Function scope mystery
user name
2006-04-04 08:40:44

Dylan_Foliomedia wrote:
<snip>
> Can someone explain why this works?
> Specifically, how is the parameter "number"
being stored? How
> are two parameters being passed?
>
> function multiple(factor) {
> 	function multiplyBy(number) {
> 		return number * factor;
> 	}
> 	return multiplyBy;
> }
> var double = multiple(2);
>
> alert(double(5));
>
> [this returns 10]
>
> Is it that the previous parameter (2) becomes
"number" and is stored as
> part of double? I've done a lot of fairly advanced
scripting but this
> simple example has revealed an area I'm completely
ignorant of. Any
> light you might shed on this would be greatly
appreciated.

See:-

<URL: 
http://www.jibbering.com/faq/faq_notes/closures.html
>

Richard.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at http://
groups.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )