Immers
Immers

Reputation: 339

javascript function parameter naming requirements

I must have read about arguments in functions and how they are variables and/or objects and pass value a million times and still real life examples keep throwing me off. The keyword "this" in this example points to the object of the function, being the form element. OK got that.

Now the function outside the window.onload event handler has a parameter named "frm". My question is whether it's necessary to give this parameter the same name as the id of the form element that will be passed to it (in this case 'frm')? I thought this didn't matter. For all it mattered one could put anything inside the argument just as long as it's reused as a local variable inside of the function itself.

window.onload = function() {
    // validation for submit button
    document.frmFlight.onsubmit = function() {
        //console.log("what is 'this'?: "+this);
        return validate(this);
    }       
}

//----------------------------------------------------------
function validate(frm) {
    var valid = true;
    return valid;
}   

Upvotes: 0

Views: 272

Answers (5)

Wayne
Wayne

Reputation: 60414

Function parameter names are arbitrary. The name you choose does not have any relationship to the type of thing later passed to the function.

My question is, is it needed to call the argument the form element's id in this case 'frm'?

It sounds like you're asking whether the name of the parameter in "validate" needs to match the ID of the DOM element that will be passed to it. (I would re-state this to use a word less overloaded than "call".)

The answer is no.

The general concept to understand is that your function has no awareness of the types of things that will be passed to it. Your function "validate" could take an argument of any type at run-time. The name of the parameter is irrelevant.

Upvotes: 1

Luke Bennett
Luke Bennett

Reputation: 32896

I don't know whether this is relevant to your question but given your reference to the 'this' keyword, if you were to do the following in your onsubmit handler:

return validate.call(this);

Then within your validate function, 'this' would refer to the form e.g.:

function validate(){
    var valid = this.elements[0].length > 0;
    return valid;
} 

Furthermore, in this situation, you could also do:

document.frmFlight.onsubmit = validate;

Upvotes: 0

Andre
Andre

Reputation: 1107

Yeah, your question isn't too clear, but the parameter name (in this case frm) is just a reference to something you will pass in. Example:

function validate(frm){
  return true;
}

validate('Bob')
validate(3)

Upvotes: 0

Cfreak
Cfreak

Reputation: 19309

frm refers to the variable passed to that function. In your example this object from the onLoad function. In the validate function it can be called anything you want.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074208

It's not clear exactly what you're asking, but the name you give the argument in the function signature doesn't relate in any way to the name of anything you might be passing into the function. It can be (just about) anything you want it to be. These are all equivalent:

function validate(frm) {
    // ...
}

function validate(theForm) {
    // ...
}

function validate(foo) {
    // ...
}

Your use above is just fine, because when you set up an event handler in the way you have (by assigning a function to the onsubmit property), when the event fires, the function will be called such that this references the DOM element for the form. Assuming that's what the validate function uses its argument for (regardless of what it calls it), you're in good shape.

Upvotes: 1

Related Questions