Robin Mantley wrote:When I have to write a custom function (or read someone else's):
a) How would I know I need to pass parameters to the function?
b) How would I know what the other persons' parameters represent?
c) Is passing parameters the same as defining variables within the function (using "var")?
I didn't look at your examples closely due to the lack of proper indentation. Please be sure code is properly formatted when posting. Kudos for using code tags, though!
But most of your confusion stems from the fact that you think the alert() function accepts more than one parameter. It doesn't. ."
Also, using alert() as a debugging tool is pretty poor. I'm not sure why you are focusing on it"
Documentation. Failing that, read the code.
If the function needs information, that information is generally passed as parameters"
Why would you want to redefine a parameter name with a local variable?
Your examples aren't serving to help understand your confusion because they rely upon a false assumption (that alert() is a good way to test things, and accepts multiple parameters).
Robin Mantley wrote:I bought one of your books last year! Secrets of the Javascript Ninja! Is there a section in that book where you discuss function parameters?
I didn't go any further to find other documentation on how to format the code in the posts
I don't understand why, however in example 1 and 2, the first argument is not returning any value.
I followed the link you posted, but I must be missing that information on the page.
I was reading where the author wrote: "Why did I use alert in examples ...
As far as the other person's parameters, I ran into a situation where the code was not well written, nor was there any documentation.
I tried using Chrome Dev tools to debug to find the values of the parameters, but was unable to. This is another reason I am asking about understanding parameters.
Would you please explain to me in more detail what kind of information the function needs and does it have to be as parameters in parentheses? I think this is where I am getting stuck.
Why would you want to redefine a parameter name with a local variable?
That is one of my questions. Are the local variables the parameters?
If so, I was assigning a value to parameter (or local variable) to pass to the function. Wait a minute, so I don't pass the parameter to the function?
I don't know why it is so hard for me to understand this concept of parameters.
For example:Bear wrote: When a parameter is defined for a function, it is available within the function by name.
Robin Mantley wrote:Please let me know if I am understanding this correctly:
Parameters are passed to a function when I don't know what their values will be -- but the arguments passed to the function when it is called are the values of those parameters.
The arguments (values) also must be passed in the same order as the parameters (right?)
and the parameters should be named in such a way that describes what information the value (argument) represents.
Variables are listed in the function (not using the same names as the parameters) when I want to assign a value to them in the function.
I did not write this code but had to debug it
To me, it looks like a config object is being created with properties that are the parameters
Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
What do those parameters have to do with the config object? Nothing. So when looking at the config object, you shouldn't be thinking about parameters at all. There's nothing there to indicate anything at all to do with parameters.
Now following that code, there is a function declared that has parameters that use the same names as properties in the config object. Why? I don't know. This is obviously not a complete code segment so I have no ides how this function may be used in the future. But the fact that a completely unrelated function to the config object uses names that are the same has no significance to the config object itself.
The second case is why I said earlier that giving the default values was unnecessary. The 'pass an object' in pattern is fairly common in JavaScript, the function has a set of defaults defined as an object then the argument is merged into that default. This approach has the advantage that you can add your parameters to the object in any order and, if you want to accept the default, you just don't add that property as opposed to calling a function with multiple arguments where it is awkward to pass the (say) only the first, third and fifth arguments.
As far as your problem is concerned I would guess that showSiblings is not working because that value is not getting passed into MDV.gNav.Generator.init as part of that single object parameter.
Robin Mantley wrote:
In lines 20 - 24, are these the default values for the parameters and the properties in the config object?
Here is where I began to think of the parameters in the config object as arguments to a function:
Can you now see the beginning of my confusion about variables and parameters? Since those "elements" are named the same, it appears to me that in one case, they are being passed to a function as parameters, in another case, they are being given a value within that function, and in another case, are being passed as arguments when the function is called.
From my usage of jQuery and/or javascript, I have an understanding of how to pass arguments when the function is called, but what I don't quite understand is how to create a function properly that will need to use parameters in the first place.
Some functions don't have any parameters passed to it, for example the getDateOpened method:
That is a simple example, yes, but could this have been written by passing a parameter?
Don't get me started about those stupid light bulbs. |