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

前端 未结 8 1295
鱼传尺愫
鱼传尺愫 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: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, "/");
    

提交回复
热议问题