Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Robin Mantley

+ Follow
since May 23, 2014
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Robin Mantley

I have this knockout.js code and want to know why the "this" keyword is being passed in the "fullName" function:


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.

Thanks, Rob. I do understand what you have written and I appreciate how you explained why the 'config' object was created and how this type of object can be used as arguments in the function.

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.

Yes, I would think that would work, but there is nothing in the code that defines what would happen if the showSiblings parameter/argument is false. So since its default is true, its unnecessary to add it in the function call as you said above. The fact that that conditional code is missing from the entire function is what was confusing me, along with this being advanced code (for me).

Thanks again!
Thank you, Bear. Again you are making things much more clear to me and appreciate your time and patience! The function that contains this code is extremely long (over 500 lines) and I didn't want to post it all. Here is more detail:

But yes, you are right, there are several functions within the function (which starts on line 5) where the config object is created (and in one of those inner functions, config.showChildren or config.parentUrl are being used), and because of this, I began to get confused with parameters and variables because they are share the same name...

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.

Here is where I began to think of the parameters in the config object as arguments to a function:

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.

Here is how the function is being used in the future. The following function is being called within the HTML -- the arguments past to this function:

I thought I did and here, again, is why in my initial examples above, I was assigning values to the parameters.

In lines 20 - 24, are these the default values for the parameters and the properties in the config object?

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.

Bear, based on your advice to me, I know now that I am interpreting this all wrong (and again thanks for helping me), but can you see how this code can be misinterpreted by someone still learning?

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?

Thank you for your continued help. I promise I am not trying to confuse anyone.
Thanks, Bear. Yes, your last example and the explanations to my comments and questions you have given are very helpful. After reading your posts a few times and going back and forth on my examples, I am beginning to see what I have been mixing up. 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'm hoping I will never see a real-world example of assigning a value to parameter (or local variable) to pass to the function. But would this represent a real-world example (this is just snippet showing parameters with values -- I did not write this code but had to debug it to find out why showSiblings was not working) To me, it looks like a config object is being created with properties that are the parameters and those parameters are assigned values. Is this a good example?

I didn't post this code initially because I don't want to give the impression that I just want someone to give me the answer, so I was trying to give examples of how I was interpreting the code. I really want to know how to find the answer so that I can use that kind of knowledge in other situations. This is a real learning process for me and I want to understand more than anything.

Thanks again very much for your help!
Thank you, Bear for the friendly welcome and your detailed reply. I liked the tagline about greenhorns. 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 finish reading it.

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!

I indented the code and updated comments, I'm sorry I missed that. I read the terms and conditions when I joined, and all it stated was "be nice". I didn't go any further to find other documentation on how to format the code in the posts, but I should have known better.

I don't understand why, however in example 1 and 2, the first argument is not returning any value. And in example 4, I'm getting an error even though I'm declaring and assigning the variable as I did in the other examples. Example 3 is logging all args correctly. Could you help me understand this?

But most of your confusion stems from the fact that you think the alert() function accepts more than one parameter. It doesn't. ."

I changed the code from using the alert() to console.log() function. I didn't realize alert() only takes one argument. I followed the link you posted, but I must be missing that information on the page.

Also, using alert() as a debugging tool is pretty poor. I'm not sure why you are focusing on it"

I was using the alert() function to follow the example in a sitepoint book I am using. Also, I must have mixed up this book with another book I was reading where the author wrote: "Why did I use alert in examples ... it works reliably in every browser... not necessarily ideal...". The author probably didn't pass more than one argument to the alert() function though, so I was wrong to use alert() instead of console.log(). I usually use the console.

Documentation. Failing that, read the code.

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.

If the function needs information, that information is generally passed as 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?

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).

My examples are so rudimentary, but at this stage of my learning, I did that on purpose to understand what I'm doing and why (baby steps). I made the variables local to the function using the parameter names. This again is my confusion and why I am posting this question. I don't know why it is so hard for me to understand this concept of parameters. I really need help...

Thanks again.
Hi all,

If this has been already asked, please feel free to post the link. If not, here is my problem:

While I can understand simple examples of passing parameters to functions, i.e.:

I'm having trouble applying it when the function is more complex, i.e.: contains several parameters representing different "types" of arguments values.

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")?

Example 1:

Example 2:

Example 3:

Example 4:
I thought I was understanding the basics of javascript and have done much reading and online tutorials. Trouble is, the examples of functions that pass parameters already provides the parameters needed, so I don't really have to think about whether or not they are needed -- but I want to understand! Please help me. I've taken tests and quizzes and done very well. I don't say that to brag, but with humility of a student seriously seeking to understand javascript. For some reason, book are not enough. With all due respect, I don't want to just pass tests or only write snippets of code using jQuery.

Please, I'm not asking anyone to just hand me the code, but if code is given, help me to understand if I should use parameters, and the proper scenario to use them or not. My examples above are things I've been testing to see if I understand how to use parameters, but I am hitting a brick wall...

I use the jQuery api (which uses selector/element parameters passed to the jquery function to create a jquery object). But if I create a custom function, I don't pass any parameters to the function, unless I'm using the jquery api and need to pass the selectors.

Thanks, I am grateful for your help. If my examples are hard to understand, I apologize. I know this post is long, but it is the first one, and hopefully going forward I will be more succinct now that you have some history of my quandary.