user1496436
user1496436

Reputation:

How to pass arguments inside function?

I started to learn Jquery and but I am having trouble understanding function parameters:( If you look my first code and run it: my script WILL work (WITHOUT parameters). And if you look my second code
(WITH parameters) and run it: second script WILL ALSO WORK!!

My first question: Did I correctly set parameter in my second script?
Second question: How can I check is my parameter set or being passed correctly to my function?
P.S. Sorry for being NOOB and THANK YOU!!

   //First code (WITHOUT PARAMETERS!!!) 
   $(document).ready(function () {
       var addclass = $('p:first');

       function AddClass() {
           addclass.addClass('first');
           if (addclass.is($('.first'))) {
               alert('here');
           }
           else {
               alert('not here');
           }
       }
       $('.button').click(function () {
           AddClass(addclass);
       });
   });
   //Second code (WITH PARAMETERS)
   $(document).ready(function () {
       var addclass = $('p:first');

       function AddClass(addclass) {
           addclass.addClass('first');
           if (addclass.is($('.first'))) {
               alert('here');
           }
           else {
               alert('not here');
           }
       }
       $('.button').click(function () {
           AddClass(addclass);
       });
   });

Upvotes: 1

Views: 218

Answers (2)

Miguel Sanchez Gonzalez
Miguel Sanchez Gonzalez

Reputation: 9713

Yes you passed correctly the arguments to your second function, to see the arguments passed to your function can see them in two ways:

individually using the array notation

function AddClass(addClass) {
    console.log(arguments[0]); // This will show you the first parameter
    // Some code...
}

With the jQuery function $.each();

function AddClass(addClass) {
    $.each(arguments, function (key, value) {
        console.log(value); // Output the value of all of your arguments
    });
}

In this case don't matter if you pass or not the parameters because the addClass variable that you access and modify in the function AddClass are in the same scope.

You should take a look to this post JavaScriptVariableScope to understand what is happening in your actual code and why your two samples of code works properly.

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

your second arguments is right and to check any arguments passed to a function use:

arguments.length

for example:

function myfunc() {
   alert( arguments.length ); //output: 1, because you passed one argument

   // and to get that

   var myarg = arguments[0];
}

myfunc(someVar);

If you don't pass any arguments then:

function myfunc() {
   alert( arguments.length ); //output: 0, because you passed no argument
}

myfunc();

For your function:

  function AddClass(addclass) {

                      ^-- declaring addclass is not necessary if you use like following

       // checking for argument

       if( arguments.length ) {
         // arguments[0] will give you addclass
         // you may not write addclass to function like above
       }

       addclass.addClass('first');
       if (addclass.is($('.first'))) {
           alert('here');
       }
       else {
           alert('not here');
       }
   }

Call function with addclass arguments:

   $('.button').click(function () {
       AddClass(addclass);
   });

Upvotes: 2

Related Questions