1

I'm just trying to get a better understanding/confirmation of function arguments In the function seen here:

function newFunction(data, status){

would the function apply specifically to data and status variables?

I don't quite understand how the arguments work.

alex
  • 479,566
  • 201
  • 878
  • 984
Brant Barton
  • 458
  • 6
  • 18
  • 4
    I think you need more context .. there is no jQuery in your question, but JavaScript functions work just like subroutines in most programming languages do. – Explosion Pills Jan 08 '13 at 23:38
  • This sounds like more of a general programming question. Nothing specific to JavaScript or jQuery here... – elclanrs Jan 08 '13 at 23:40
  • 1
    @elclanrs It's more of a "didn't read a tutorial/book" situation .. (but no, it's not general programming because that is so basic and then there are some JavaScript-specific rules beyond any form of "unified" function-application operation which itself can vary greatly) –  Jan 08 '13 at 23:42
  • @pst Well.. I did read the tutorial.. it's just that anonymous functions were so commonly used up until this point. now it's a bit hard to find exactly where functions were introduced to see exactly what they said about it – Brant Barton Jan 09 '13 at 06:01
  • @BrantBarton For reference: all functions in JavaScript work the same way - that is, a *function-object* is invoked. That is, the expression `fn(a,b,..)` first *evaluates* `fn` to a *function-object* and then applies ("calls") said function-object with the appropriate context and supplied arguments. It's the exact same mechanism as anonymous functions. (Consider "TypeError: .. is not a function" when `fn` does not evaluate to a function-object.) –  Jan 09 '13 at 06:02

3 Answers3

3

Basic Scenario

When a function is defined, the () area is used for inputs. These inputs are mapped to what data is sent in.

function newFunction(data, status){
}
newFunction(1,2);

In this scenario, data will be assigned the value of 1, and status will be assigned the value of 2 for the scope of newFunction.

Missmatched inputs

However, it does not always directly map. If fewer arguments are sent, then the unassigned input variables become undefined.

function newFunction(data, status){
}
newFunction(1);

In this scenario, data will be assigned the value of 1, and status will be assigned the value of undefined for the scope of newFunction.

arguments object

Inside of the scope of newFunction, there is also access to the array like object called arguments.

function newFunction(data, status)
{
 var args = arguments;
}
newFunction(1);

In this scenario, the variable args will hold the arguments object. There you can check args.length to see that only 1 argument was sent. args[0] will give you that argument value, being 1 in this case.

Function object

A function can be made into an object with the new keyword. By using the new keyword on a function, a Function object is made. Once the Function object is made, this may be used to refer to the current Function object instead of the global window.

function newFunction(data,status){
 if( this instanceof newFunction ){
  //use as a Function object
  this.data = data;
 }else{
  //use as a normal function
 }
}
var myNewFunction = new newFunction(1);

In this scenario, myNewFunction now holds a reference to a Function object, and can be accessed as an object through dot notation or indexing. myNewFunction["data"] and myNewFunction.data now both hold the value of 1.

Travis J
  • 81,153
  • 41
  • 202
  • 273
1

This means that the function accepts both data and status parameters. The programmer calling the funciton (you) is required for providing the parameters when calling the function (also known as arguments.)

Let's look at a simplified example:

function add(x, y) {
  return x + y;
}

The function here takes two numbers, adds them together, and returns the result. Now, as a programmer, you have the benefit of adding two numbers whenever you want without having to duplicate the functionality or copy/paste whenever you need your application to perform the same functionality.

Best of all, another programmer you work with can add two numbers together without worrying about how to do it, they can just call your function.

EDIT: Calling the function works as follows

var num1 = 10;
var num2 = 15;
var z = add(num1, num2); //z = 25
Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
  • 2
    ...or it should do. It doesn't have to if it doesn't want to! – Lee Taylor Jan 08 '13 at 23:39
  • @RobertGrenier so these variables are created here and undefined presently? – Brant Barton Jan 08 '13 at 23:40
  • @pst so basically this step just creates a variable, and then I can uses/define it within the function, yes? – Brant Barton Jan 08 '13 at 23:43
  • I think you're asking about scope: read [this other SO question](http://stackoverflow.com/questions/500431/javascript-variable-scope) for a nice answer. – Matt Jan 08 '13 at 23:43
  • Nit: parameters are what appear in a function declaration and can be used like variables from within the function body; arguments are the actual values (fsvo) that are passed to the function –  Jan 08 '13 at 23:45
  • @pst thank you.. THAT answered my question.. I thought they were called paramaters, but then saw something else that made me doubt it. – Brant Barton Jan 08 '13 at 23:48
  • @RobertGreiner it might be clearer to give the parameters different variable names than the calling arguments, to clarify the difference. – cmbuckley Jan 08 '13 at 23:48
0
function newFunction(data, status){ ... function code ... }

This is the Javascript definition of a function called newFunction. You can pass arguments to a function. These are variables, either numbers or strings, with which the function is supposed to do something. Of course the function behaviour/output depends on the arguments you give it.

http://www.quirksmode.org/js/function.html

B4LB0
  • 1
  • 2