Hi Russ, and forgive my english which is not my native language.
I am afraid that somebody here didn't get your point and got lost into
speculation. Pointed ears is one of the biggest pain in the neck ever
seen online period, he answers to nearly everything he sees, he pops up
everywhere, continuously, year after year, on a nearly daily basis, on
nearly 50% of the threads, 99.99% of the times having nothing to say
(the 0.01% is a statistical dispersion), full of formalism, counting in
every single hair he sees on a cheek, and being often full of disdain
(see his answer to Matt), so no wonder he ended up with an entirely new
thread quite unrelated with your question.
Be warned, he has a few friends. They'll show up here promptly.
You're a newbie? If (if) so you want to understand, right?
Your question is:
======
totally newbie question (...)
Inside the function, how do I assign a value to the variable named
myVarName
======
Got it.
Ok, firts it seems to show some confusion that can be quite understood
if as you hint you are a newbie. You are RIGHT, maybe you still do not
know how much, that myVarName is a variable, YET when we define
variables inside the round brackets of a function, we call them
ARGUMENTS, not 'variables', though they ARE such INDEED. Get used to
this.
Ok, why arguments?
To answer this, why functions?
Functions in digital codes derive their name from MATH.
f(x)
in math means that something named 'f' is a FUNCTION of something named
'x': that is, IF x varies, f varies with it (HOW; well, math doesn't
say that with THAT statement, YET codes do: within the CURLY brackets).
It can also be written, in math:
f(x, y)
which now means that the thing called f, whatever it could be, changes
depending on variations no longer only upon something called x,
whatever it could be (try to abstract as much as you can) but also
depending on something ELSE called y.
NOT orthodox at all, but to make you understand:
speed (time, space)
Got it now? :-)
If you take notice, that is the same notation used in informatic
functions:
function aname (x, y) {/*here the type of VARIATIONS that shall
occur*/}
or, say
function speed(time, space){return space/time;}
those that are passed in between round brackets are its ARGUMENTS,
namely the parameter(s) upon whose VARIATIONS, the function may return
a VARIED outcome.
It is true that DIGITAL (NOT mathematical) functions can be run also
without arguments. This because, of course, there is a point where
informatic departs from mathematics and takes its own turn and path.
YET, it starts from the same grounds.
So, I want you for a start to understand that a function is called a
function because in its theoretical roots, it should change its OUTCOME
depending on how its ARGUMENTS vary, EXACTLY like a Mathematical
function would: f(x,y).
How these arguments vary? Exactly as you ask, by ASSIGNING to them some
values
Now your question: HOW do you ASSIGN exactly them, namely how do you
INTRODUCE in those SIGNATURES the ACTUAL variations?
When you assign a variable name, for now in a general way later we'll
see within a function, it may be benefical to discriminate about its
components. Let's review, though you may still know.
The variable name: this, as you already know for sure, is written
WITHOUT quotes, and can be whatever name which is not a keyword in the
given language, so say:
foo
now you give to it a value, namely you perform what is called an
ASSIGNMENT:
foo=
what follows is the VALUE. Can be either primitive data type( string,
namely in between quotes, or number, or boolean, or even another
variable previously initialized, or an object or array which latter is
no longer properly 'primitive' to some degree - some may speculate it
is)
foo="hallo";
YET you should prefix in javascript the keyword var
var foo="hallo";
Get this habit.
Note that var defines the scope of a variable. If you use var, the
variable you initialize belongs to the scope where it has been
initialized, namely can be viewed and ACCESSED by all the other code
components that RESIDE WITHIN that SCOPE where it was INITIALIZED.
So,
var foo="hallo"
would be visible to EVERYTHING ELSE in your code, because you
initialized it outside any block of code, in the global scope of your
script that is.
YET if you INSIDE a function do like:
function dunno(){
var foo="hallo"
}
NOW, having used var, foo is visible ONLY within the function named
dunno.
If you would have omitted the prefix var, THEN it would have been
visible as a GLOBAL scope again. It's just a RULE in javascript (for a
newbie it's better to see it like that): if you use var, you define a
scope: WHENEVER you do not use var, the DEFAULT scope is always meant
as the GLOBAL window, NO MATTER where you initialized it.
If you initialize a variable within a function or even a LOOP inside a
function, and you use the keyword var, such variable is VISIBLE and
ACCESSIBLE only and exclusively within the nearest outermost block that
ENCAPSULATES it, got it?
If you do NOT use the keyword var, then whatever variable, NO matter
where it has been initialized, shall belong to the GLOBAL scope.
So, get used to use var to limit the scope of your variable from
tampering with an unnecessarily wide scope, or even dabfgerously
OVERWRITE possible variables with the same name in some outer scope.
More:
A statement like
var foo;
is called a DECLARATION.
a statement like
var foo = "hallo"
namely assigning ALSO the value in the same code line, is called an
INITIALIZATION.
Why all this? Because i want you to UNDERSTAND that those LOGICS FULLY
apply also for variables passed to a function, only the operate a
little bit in "DISGUISE": but if you can see that they are the SAME
thing as above, chances are you understand this once and for all.
Now, to answer your point, when you have arguments in an array
definition like yours
function DoSomething(strVarName) { }
that strVarName is the same as a DECLARATION of a variable.
Please read this line again: that strVarName is the same as a
DECLARATION of a variable.
But it has been added no assignment yet, as you may see yourself.
There are languages, like PHP but NOT Javascriopt, where it would even
be possible to perform INITIALIZATION for ARGUMENTS of a function:
function dunno($foo='hallo'){}
that in PHP means that the argument $dunno has a DEFAULT value which is
'hallo' in CASE the argument is not passed upon INVOKING the function.
This is NOT possible in javascript. In javascript whenever you have a
function and its arguments, the latter can only be DECLARED, never
initialized right ON THE SPOT as for instance PHP could.
So when does this damn assignment of the RELATIVE VALUE occurs?
Upon CALLING the function.
function DoSomething(strVarName) {alert(strVarName);}
//above your function declaration
DoSomething("hallo world"); //assigned!!!!
above, now that you have invoked or called your function DoSomething,
what has happened is the SAME, in the background, than having firstly
DECLARED a variable name whose SCOPE is AUTOMATICALLY within the
FUNCTION scope, and THEN upon CALLING you have performed the
ASSIGNMENT. That is, upon calling
DoSomething("hallo world");
what you have done in the BACKGROUND is like:
var strVarName="hallo world";
Every time you call the function, thus, you can perform a NEW
assignment.
Your sentence, Russ, that goes
========
Inside the function, how do I assign a value to the variable named
myVarName
(i.e. the variable whose name is the value of strVarName)?
========
The way you assign a value to the variable named myVarName (which when
it is a variable inside a function round brackets is called an
ARGUMENT) is the one I just showed to you: by passing the value when
you CALL the function.
This is your answer.
I don't know whether this makes things clearer to you, but it seems at
least a more newbie friendly or oriented explanation, which takes the
time a newbie needs to learn something that RIGHTLY still escapes his
or her grasp. We have all been there so don't worry - well, except of
course Pointed Ears and a couple of others which you will soon meet if
you attend here long enough to be soon disgusted by them.
Good luck Russ, you're doing fine, and asking the right questions.
ciao
Alberto
http://www.unitedscripters.com/