the murmurous sea

JS: The Arguments Object 본문

#dev/개념정리

JS: The Arguments Object

lunacer 2020. 5. 14. 12:50

▼Prior knowledge 

더보기

Prarmeters and Arguments
: Function parameters are the names listed in the function definition.
: Function arguments are the real values passed to (and received by) the function.

Parameter Rules
: JavaScript function definitions do not specify data types for parameters.
: JavaScript functions do not perform type checking on the passed arguments.
: JavaScript functions do not check the number of arguments received.

 

Parameter Defaults
If a function is called with missing arguments (less than declared), the missing values are set to: undefined
Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter

 

1. JavaScript functions: A built-in object
2. The arguments object is not an Array. It is similar, but lacks all Array properties except length. 

   : An Array-like▼ object accessible inside functions that contains the values of the arguments passed to that function.

더보기

“Array-like” means that arguments has a length property and properties indexed from zero, but it doesn't have Array's built-in methods like forEach(), map() and pop().

3. However, it can be converted to a real Array
   : As you can do with any Array-like object, you can use ES2015's Array.from() method or spread syntax to convert arguments to a real Array:
4. The arguments object is useful for functions called with more arguments than they are formally declared to accept.
   : This technique is useful for functions that can be passed a variable number of arguments.

   : If a function is called with too many arguments(more than declared), these arguments can be reached using the arguments object.

5. The arguments object is a local variable available within all non-arrow functions.
6. Can refer to a function's arguments inside that function by using its arguments object.
7. It has entries for each argument the function was called with, with the first entry's index at 0.

8. contains an array of the arguments used when the function was called (invoked).

9. Arguments are Passed by Value
   : The function only gets to know the values, not the argument's locations.
   : If a function changes an argument's value, it does not change the parameter's original value.

   : Changes to arguments are not visible (reflected) outside the function.

더보기

Objects are Passed by Reference
In JavaScript, object references are values.
Because of this, objects will behave like they are passed by reference:
If a function changes an object property, it changes the original value.
Changes to object properties are visible (reflected) outside the function.
(from w3school doc)

10. typeof

   : The type of individual arguments can be determined by indexing arguments.

   : The typeof operator returns 'object' when used with arguments.

 

 

[Syntax]

arguments

 

Difference between rest parameters and the arguments object▼

더보기

There are three main differences between rest parameters and the arguments object:

1. rest parameters are only the ones that haven't been given a separate name (i.e. formally defined in function expression), while the arguments object contains all arguments passed to the function;
2. the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
3. the arguments object has additional functionality specific to itself (like the callee property).

Check rest parameters

 


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments 

https://www.w3schools.com/js/js_function_parameters.asp

 

'#dev > 개념정리' 카테고리의 다른 글

생성자 함수 (empty)  (0) 2020.05.14
JS: split() vs. join()  (0) 2020.05.14
JS: How to remove duplicates from an Array  (0) 2020.05.13
JS: String() vs. toString() and valueOf()  (0) 2020.05.13
JS: call() and apply()  (0) 2020.05.13
Comments