dcsimg
www.webdeveloper.com
Results 1 to 9 of 9

Thread: Understanding Function Arguments

  1. #1
    Join Date
    Sep 2015
    Posts
    63

    Understanding Function Arguments

    This is a fragment from the book "Professional Javascript for Web Developers 3rd edition" talking about function arguments :

    -------------

    Another interesting behavior of arguments is that its values always stay in sync with the values of the corresponding named parameters.

    .... Because values in the arguments object are automatically reflected by the corresponding named arguments, the change to arguments[1] also changes the value of num2, so both have a value of 10. This doesnít mean that both access the same memory space, though; their memory spaces are separate but happen to be kept in sync. This effect goes only one way: changing the named argument does not result in a change to the corresponding value in arguments.

    -------------

    Question 1:
    I have tried an example of this, and I have found that it does goes the other way too. Changing the value of a named argument, does change the value of the corresponding value in the arguments object.

    You can check this example here: https://jsfiddle.net/brsastre/ujc6vhs9/1/


    Question 2:
    Is there any benefit on using the arguments object rather than the named values, as usual?


    Question 3:
    The book says:

    "...This happens because arguments in ECMAScript are represented as an array internally. there actually is an arguments object that can be accessed while inside a function to retrieve the values of each argument that was passed in." The arguments object acts like an array (though it isnít an instance of Array) in that you can access each argument using bracket notation.

    Is it an array or not?
    I doesn't seem to be an object, because I can get the values using arrays notation : arguments[0]



    Thanks in advance!
    Bruno.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,392
    Arrays are Objects of a sort, you can access an object or array that is referenced by a numerical index or an associative name
    Code:
    var obj = {};
    var ary = [];
    
    obj.cheese = "Stilton";
    ary['cheese'] = "Stilton";
    
    console.log("> "+obj.cheese);
    console.log("> "+ary.cheese);
    Arguments Object is useful if you are expecting a list of parameters and they may vary in length, you can mix the types, pass variable names of the expected parameters and use arguments object to fill in the blanks.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Mar 2011
    Posts
    124
    Because values in the arguments object are automatically reflected by the corresponding named arguments, the change to arguments[1] also changes the value of num2, so both have a value of 10. This doesn’t mean that both access the same memory space, though; their memory spaces are separate but happen to be kept in sync. This effect goes only one way: changing the named argument does not result in a change to the corresponding value in arguments.
    That's only true when you're using strict mode.

    Arguments is an 'array-like' objects, it is an object which has indexed elements and a length property. It doesn't have the array methods. You can create a real array form them by using one of the following statements
    Code:
    var array = Array.prototype.slice.call(arguments, 0);
    var array = [].slice.call(arguments, 0);
    var array = Array.from(arguments); // modern browsers
    And don't create arrays with associative properties. They are considered bad practice and may give unexpected results when using array methods on them.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,392
    Thats nonsense, if you weren't meant to use arrays with associative names, then it wouldn't be allowed, what you have said is the same as saying that an object should never use numeric indexing... but you can.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Mar 2011
    Posts
    124
    I didn't say it isn't allowed, I said it's bad practice. Creating 300 global variables like "var a-a, a-b, a-c, b-a, b-b, b-c, ...;" is allowed aswell. Doesn't help with maintainability though.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,392
    True but you have to remember that all this "Best Practice" and "Accepted Methods" that gets thrown in to the mix is also an opinion and just because its "Best" or "Accepted" doesn't mean it is actually "Best" thing to do.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  7. #7
    Join Date
    Sep 2015
    Posts
    63
    Quote Originally Posted by \\.\ View Post
    Arrays are Objects of a sort, you can access an object or array that is referenced by a numerical index or an associative name
    Code:
    var obj = {};
    var ary = [];
    
    obj.cheese = "Stilton";
    ary['cheese'] = "Stilton";
    
    console.log("> "+obj.cheese);
    console.log("> "+ary.cheese);
    Arguments Object is useful if you are expecting a list of parameters and they may vary in length, you can mix the types, pass variable names of the expected parameters and use arguments object to fill in the blanks.
    I see, thanks!

  8. #8
    Join Date
    Sep 2015
    Posts
    63
    How do I thank you? I don't see the 'give thanks' button...

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,392
    Well its only half the story...

    To iterate an array you can use different looping methods like the while and do..while and for loop through with a numerical counter
    To iterate an object, you use the for loop but use the in iterator method to access all numerical and associative names in the object.

    I recently watched a video that tested to see if array or lists were better, it all depended on what you were trying to do, for the most part arrays came out on top and one unusual outcome showed that traversing an array in reverse was often approaching twice as fast as traversing it sequentially.

    Sometimes it is better to create a JSON type object / literal string, it all depends on what you are trying to achieve.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center