TypeError: invalid arguments

The JavaScript exception "invalid arguments" occurs when typed array constructors are provided with a wrong argument.

Message

TypeError: invalid arguments (Firefox)
					

Error type

TypeError

What went wrong?

Typed array constructors require either

  • a length,
  • another typed array,
  • array-like objects,
  • iterable objects or
  • an ArrayBuffer object

to create a new typed array. Other constructor arguments will not create a valid typed array.

范例

No strings in typed arrays

Typed arrays, for example a Uint8Array , can't be constructed from a string. In fact, strings can't be in typed arrays at all.

var ta = new Uint8Array("nope");
// TypeError: invalid arguments
					

Different ways to create a valid Uint8Array :

// From a length
var uint8 = new Uint8Array(2);
uint8[0] = 42;
console.log(uint8[0]); // 42
console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1
// From an array
var arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31
// From another TypedArray
var x = new Uint8Array([21, 31]);
var y = new Uint8Array(x);
console.log(y[0]); // 21
// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8Array(buffer, 1, 4);
// From an iterable
var iterable = function*(){ yield* [1,2,3]; }();
var uint8 = new Uint8Array(iterable);
// Uint8Array[1, 2, 3]
					

另请参阅

Metadata

  1. JavaScript
  2. 教程:
  3. 完整初学者
    1. JavaScript 基础
    2. JavaScript 第一步
    3. JavaScript 构建块
    4. 引入 JavaScript 对象
  4. JavaScript 指南
    1. 介绍
    2. 语法和类型
    3. 控制流程和错误处理
    4. 循环和迭代
    5. 函数
    6. 表达式和运算符
    7. 数字和日期
    8. 文本格式
    9. 正则表达式
    10. Indexed collections
    11. Keyed collections
    12. Working with objects
    13. 对象模型的细节
    14. Using promises
    15. 迭代器和生成器
    16. Meta programming
    17. JavaScript 模块
  5. 中间体
    1. 引入 JavaScript 对象
    2. 客户端侧 Web API
    3. 重新介绍 JavaScript
    4. JavaScript 数据结构
    5. 相等比较和相同
    6. 闭包
  6. 高级
    1. 继承和原型链
    2. 严格模式
    3. JavaScript 类型数组
    4. 内存管理
    5. 并发模型和事件循环
  7. 参考:
  8. 内置对象
    1. AggregateError
    2. Array
    3. ArrayBuffer
    4. AsyncFunction
    5. Atomics
    6. BigInt
    7. BigInt64Array
    8. BigUint64Array
    9. Boolean
    10. DataView
    11. Date
    12. Error
    13. EvalError
    14. Float32Array
    15. Float64Array
    16. Function
    17. Generator
    18. GeneratorFunction
    19. Infinity
    20. Int16Array
    21. Int32Array
    22. Int8Array
    23. InternalError
    24. Intl
    25. JSON
    26. Map
    27. Math
    28. NaN
    29. Number
    30. Object
    31. Promise
    32. Proxy
    33. RangeError
    34. ReferenceError
    35. Reflect
    36. RegExp
    37. Set
    38. SharedArrayBuffer
    39. String
    40. Symbol
    41. SyntaxError
    42. TypeError
    43. TypedArray
    44. URIError
    45. Uint16Array
    46. Uint32Array
    47. Uint8Array
    48. Uint8ClampedArray
    49. WeakMap
    50. WeakSet
    51. WebAssembly
    52. decodeURI()
    53. decodeURIComponent()
    54. encodeURI()
    55. encodeURIComponent()
    56. escape()
    57. eval()
    58. globalThis
    59. isFinite()
    60. isNaN()
    61. null
    62. parseFloat()
    63. parseInt()
    64. undefined
    65. unescape()
    66. uneval()
  9. 表达式 & 运算符
    1. 算术运算符
    2. 赋值运算符
    3. Bitwise operators
    4. 逗号运算符
    5. Comparison operators
    6. 条件 (三元) 运算符
    7. Destructuring assignment
    8. Function expression
    9. Grouping operator
    10. Logical operators
    11. Nullish coalescing operator
    12. Object initializer
    13. 运算符优先级
    14. Optional chaining
    15. Pipeline operator
    16. 特性访问器
    17. 传播句法
    18. 异步函数表达式
    19. await
    20. class expression
    21. delete operator
    22. function* 表达式
    23. in operator
    24. instanceof
    25. new operator
    26. new.target
    27. super
    28. this
    29. typeof
    30. void 运算符
    31. yield
    32. yield*
  10. 语句 & 声明
    1. async function
    2. block
    3. break
    4. class
    5. const
    6. continue
    7. debugger
    8. default
    9. do...while
    10. empty
    11. export
    12. for
    13. for await...of
    14. for...in
    15. for...of
    16. 函数声明
    17. function*
    18. if...else
    19. import
    20. import.meta
    21. label
    22. let
    23. return
    24. switch
    25. throw
    26. try...catch
    27. var
    28. while
    29. with
  11. 函数
    1. 箭头函数表达式
    2. 默认参数
    3. 方法定义
    4. 其余参数
    5. 自变量对象
    6. getter
    7. setter
    1. Class fields
    2. 构造函数
    3. extends
    4. static
  12. 错误
    1. Error: Permission denied to access property "x"
    2. InternalError: too much recursion
    3. RangeError: argument is not a valid code point
    4. RangeError: invalid array length
    5. RangeError: invalid date
    6. RangeError: precision is out of range
    7. RangeError: radix must be an integer
    8. RangeError: repeat count must be less than infinity
    9. RangeError: repeat count must be non-negative
    10. ReferenceError: "x" is not defined
    11. ReferenceError: assignment to undeclared variable "x"
    12. ReferenceError: can't access lexical declaration`X' before initialization
    13. ReferenceError: deprecated caller or arguments usage
    14. ReferenceError: invalid assignment left-hand side
    15. ReferenceError: reference to undefined property "x"
    16. SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
    17. SyntaxError: "use strict" not allowed in function with non-simple parameters
    18. SyntaxError: "x" is a reserved identifier
    19. SyntaxError: JSON.parse: bad parsing
    20. SyntaxError: Malformed formal parameter
    21. SyntaxError: Unexpected token
    22. SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
    23. SyntaxError: a declaration in the head of a for-of loop can't have an initializer
    24. SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
    25. SyntaxError: for-in loop head declarations may not have initializers
    26. SyntaxError: function statement requires a name
    27. SyntaxError: identifier starts immediately after numeric literal
    28. SyntaxError: illegal character
    29. SyntaxError: invalid regular expression flag "x"
    30. SyntaxError: missing ) after argument list
    31. SyntaxError: missing ) after condition
    32. SyntaxError: missing : after property id
    33. SyntaxError: missing ; before statement
    34. SyntaxError: missing = in const declaration
    35. SyntaxError: missing ] after element list
    36. SyntaxError: missing formal parameter
    37. SyntaxError: missing name after . operator
    38. SyntaxError: missing variable name
    39. SyntaxError: missing } after function body
    40. SyntaxError: missing } after property list
    41. SyntaxError: redeclaration of formal parameter "x"
    42. SyntaxError: return not in function
    43. SyntaxError: test for equality (==) mistyped as assignment (=)?
    44. SyntaxError: unterminated string literal
    45. TypeError: "x" has no properties
    46. TypeError: "x" is (not) "y"
    47. TypeError: "x" is not a constructor
    48. TypeError: "x" is not a function
    49. TypeError: "x" is not a non-null object
    50. TypeError: "x" is read-only
    51. TypeError: 'x' is not iterable
    52. TypeError: More arguments needed
    53. TypeError: Reduce of empty array with no initial value
    54. TypeError: X.prototype.y called on incompatible type
    55. TypeError: can't access dead object
    56. TypeError: can't access property "x" of "y"
    57. TypeError: can't assign to property "x" on "y": not an object
    58. TypeError: can't define property "x": "obj" is not extensible
    59. TypeError: can't delete non-configurable array element
    60. TypeError: can't redefine non-configurable property "x"
    61. TypeError: cannot use 'in' operator to search for 'x' in 'y'
    62. TypeError: cyclic object value
    63. TypeError: invalid 'instanceof' operand 'x'
    64. TypeError: invalid Array.prototype.sort argument
    65. TypeError: invalid arguments
    66. TypeError: invalid assignment to const "x"
    67. TypeError: property "x" is non-configurable and can't be deleted
    68. TypeError: setting getter-only property "x"
    69. TypeError: variable "x" redeclares argument
    70. URIError: malformed URI sequence
    71. Warning: -file- is being assigned a //# sourceMappingURL, but already has one
    72. Warning: 08/09 is not a legal ECMA-262 octal constant
    73. Warning: Date.prototype.toLocaleFormat is deprecated
    74. Warning: JavaScript 1.6's for-each-in loops are deprecated
    75. Warning: String.x is deprecated; use String.prototype.x instead
    76. Warning: expression closures are deprecated
    77. Warning: unreachable code after return statement
  13. 杂项
    1. JavaScript technologies overview
    2. 词汇语法
    3. JavaScript 数据结构
    4. Enumerability and ownership of properties
    5. Iteration protocols
    6. 严格模式
    7. Transitioning to strict mode
    8. Template literals
    9. 弃用特征