Why can't I call an array method on a function's arguments?

前端 未结 8 1278
鱼传尺愫
鱼传尺愫 2020-12-03 05:07

I have a function that can accept any number of arguments...

const getSearchFields = () => {        
    const joined = arguments.join(\'/\'); 
};
         


        
相关标签:
8条回答
  • 2020-12-03 05:27

    arguments is not a JavaScript array; it's a special object that doesn't have a join method.

    0 讨论(0)
  • 2020-12-03 05:27

    If your are getting while posting http request in angular4 user HttpHeaders. Please check below answer. After usring HttpHeaders my issue is resolved.

    this.headers  = new HttpHeaders({ 'Authorization': token1, 'X-IBM-Client-Id': 'ffffdffffdd' });
    

    values.join is not a function in the file http.es5.js Angular 4

    0 讨论(0)
  • 2020-12-03 05:30

    As all said above, the arguments object is not really an array, but you can apply array functions directly, by accessing the Array.prototype and executing them with apply or call to change the context:

    var argsString = Array.prototype.join.call(arguments, '/');
    

    Edit: Since the original question asked almost 9 years ago was updated to use ES6 syntax, I feel the need to provide an ES6 answer now the language provides native constructs to handle this kind of situations.

    The ES6 standard introduces the spread operator, which you can easily use to receive the arguments as an array object, for example:

    function sum(...numbers) {
      return numbers.reduce((sum, value) => sum + value);
    }
    
    console.log(sum(1,2,3,4)); // 10

    Applied to your example:

    const getSearchFields = (...fields) => {        
      const joined = fields.join('/'); 
    };
    

    You can also use the spread operator to do function calls, let's say you have an array, and you want to call a function, passing each element of the array as an argument of the function call, now you can simply:

    function sum(a, b, c, d) {
      return a + b + c + d;
    }
    
    var array = [1, 2, 3, 4];
    console.log(sum(...array)); // 10

    Also you can expect some arguments and have the rest of the arguments passed as the last parameter defined:

    function processList(first, ...rest) {
      // ...
    }
    

    And there are more uses of the spread operator that go beyond the scope of this question, for example object copying, spreading arrays in arrays, etc...

    0 讨论(0)
  • 2020-12-03 05:30

    arguments is not really an array.

    try this:

    var args = [].splice.call(arguments,0);
    var argsString = args.join('/');
    
    0 讨论(0)
  • 2020-12-03 05:36

    arguments is a pseudo-array, not a real one. The join method is available for arrays.

    You'll need to cheat:

    var convertedArray = [];
    
    for(var i = 0; i < arguments.length; ++i)
    {
     convertedArray.push(arguments[i]);
    }
    
    var argsString = convertedArray.join('/');
    

    Similar to other posts, you can do the following as shorthand:

    var argsString = Array.prototype.join.call(arguments, "/");
    
    0 讨论(0)
  • 2020-12-03 05:44

    If you are using jQuery 1.2+, you can use $.makeArray():

    Convert an array-like object into a true JavaScript array.

    From the example in the question, just use this line

    var args = $.makeArray(arguments);
    
    0 讨论(0)
提交回复
热议问题