admin管理员组文章数量:1417096
Minimum example:
function test() {
console.log(arguments.join(','));
}
test(1,2,3);
I then get:
TypeError: undefined is not a function
However, when I do the same for an array:
console.log([1,2,3].join(','));
I get
"1,2,3"
As expected.
What's wrong with arugments? It's suppose to be an array:
(function () {
console.log(typeof [] == typeof arguments)
})();
true
Minimum example:
function test() {
console.log(arguments.join(','));
}
test(1,2,3);
I then get:
TypeError: undefined is not a function
However, when I do the same for an array:
console.log([1,2,3].join(','));
I get
"1,2,3"
As expected.
What's wrong with arugments? It's suppose to be an array:
(function () {
console.log(typeof [] == typeof arguments)
})();
Share Improve this question asked Aug 21, 2014 at 0:32 JasonJason 9759 silver badges9 bronze badges 0true
2 Answers
Reset to default 6Arguments is not an array.
(function(){
console.log(typeof arguments);
})();
// 'object'
It is an array-like structure with a length and numeric properties, but it is not actually an array. If you want to, you may use the array function on it though.
function test() {
console.log(Array.prototype.join.call(arguments, ','));
// OR make a new array from its values.
var args = Array.prototype.slice.call(arguments);
console.log(args.join(','));
}
test(1,2,3);
Note, your example works because array
is not a type. typeof [] === 'object'
also. You can however check if an object is an array by using
Array.isArray(arguments) // false
Array.isArray([]) // true
The problem is that arguments
is NOT a javascript Array per se. It behaves like an array in some ways but no in others.
Why don't you try converting it into an pure javascript array. This can be done the following way:
(function () {
var args = Array.prototype.slice.call(arguments, 0);
console.log(typeof [] === typeof args);
}());
本文标签: javascriptCan39t join() function argumentsTypeError undefined is not a functionStack Overflow
版权声明:本文标题:javascript - Can't .join() function arguments - TypeError: undefined is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244984a2649514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论