global-scope


Access Global scoped variable inside nested function Javascript


I was trying to understand Function Scope vs Gobal Scope, with the below example:
<script>
// The following variables are defined in the global scope
var num1 = 2,
num2 = 3,
name = "Chamahk";
// This function is defined in the global scope
function multiply() {
return num1 * num2;
}
console.log(multiply()); // Returns 60
// A nested function example
function getScore () {
var num1 = 20,
num2 = 30;
function add() {
var num1 = 200,
num2 = 300;
return name + " scored " + (num1 * num2);
}
return add();
}
console.log(getScore()); // Returns "Chamahk scored 60000"
</script>
I googled and found that Global Scoped variable can be accessed using this. changing the return code to
return name + " scored " + (this.num1 * this.num2);
gave output as "Chamahk scored 6", means this is accessing the global var num1 and num2.
This is clear, but what i wanted to know, how to access the num1 and num2 declared in getScore() function
.i.e. to get the output as 600.
I have to say your question is a little confusing.
Generally you would want to use parameters to your functions, to pass values around.
The reason you are getting 60000, is because in your add function you are setting num1, and num2 to 200 and 300.
If you changed your add function to take num1, and num2 as parameters, and not redeclare them, you could pass in your values in getScore();
e.g return add(num1, num2);
<script>
// The following variables are defined in the global scope
var num1 = 2,
num2 = 3,
name = "Chamahk";
// This function is defined in the global scope
function multiply(num1, num2) {
return num1 * num2;
}
console.log(multiply(num1, num2)); // Returns 6!!!
// A nested function example
function getScore () {
var num1 = 20,
num2 = 30;
function add(num1, num2) {
return name + " scored " + (num1 * num2);
}
return add(num1, num2);
}
console.log(getScore()); // Returns "Chamahk scored 60000"
</script>

Related Links

Access Global scoped variable inside nested function Javascript

Categories

HOME
svn
lambda
functional-programming
iverilog
google-play-services
swift2
haxe
javafx-8
mithril.js
responsive-design
hystrix
sap-fiori
google-cloud-logging
uima
spam
spring-shell
opam
azure-machine-learning
gollum-wiki
fluent-migrator
sybase-ase
react-dnd
spreadsheetgear
iwebbrowser2
spring-annotations
sparkle
ibm-connections
autodesk-model-derivative
python-2.x
nest
char-pointer
netstat
stackpanel
jslint
variable-variables
assemblies
windows-store
jshint
gcal
adblock
jgrapht
zend-server
thread-exceptions
powerpoint-2013
odp.net-managed
polymerfire
cortana
juniper-network-connect
chicagoboss
bjam
bootstrap-wysiwyg
whitespace-language
python-curses
mathematical-expressions
baseadapter
mod-auth-openidc
lifetime
flurry-analytics
otl
maven-central
distributed-r
network-printers
mfmessagecomposeview
commenting
miglayout
meteor-collections
django-facebook
hints
xs
flv
cmath
contextswitchdeadlock
gprof
playorm
nsmutablestring
monocross
zend-server-ce
haiku
cewolf
rtd
asp.net-mvc-views
eaccelerator
j2mepolish
spring-modules
source-code-protection
gui-designer
remote-working

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App