# JS常见问题概览

### Uncaught TypeError: undefined is not a function

Uncaught 表示错误没有被 catch 语句捕获，TypeError 是错误的名字，在这里表示是数据类型错误。undefined is not a function，这是 Error 的主信息，这里表示代码尝试使用 undefined ，把它当做一个函数。 这种错误，通常是执行了一个未定义的方法：

* 可能一：某个方法没有正确引入。如：this 的指向改变、异步引入问题；
* 可能二：浏览器兼容问题。如：IE 的 DOM 对某些 API 不支持；
* 可能三：ES6 没有转换。如：使用了 `findIndex` 等一些新 API，部分安卓机不支持。

### Uncaught ReferenceError: Invalid left-hand side in assignment

未捕获的引入错误：赋值语句左侧变量无效。比如：

```js
// 正确逻辑是比较func1的返回值。因少写了一个“=”，变成了无效的赋值语句。 
function func1 () {
    return 1
}
if (func1() = 1) {
    console.log('true')
}
```

## Uncaught SyntaxError: Invalid or unexpected token

未捕获的语法错误：无效或意外的符号。一般是出现中文符号或者语句不完整。比如：

```js
// 出现中文圆括号
function func1 () {
  console.log(1)
}
func1(）
```

类似的还有：

```js
// Uncaught SyntaxError: Unexpected end of input
let arr = [a]
arr[1
    
// Uncaught SyntaxError: Unexpected identifier
let objA = {
  a: 1
objA.a

// Uncaught SyntaxError: Unexpected end of JSON input
JSON.parse('')
```

## Uncaught TypeError: Converting circular structure to JSON

未捕获的语法错误：将循环结构转换为JSON。比如：

```js
const objA = {}
const objB = {
    objA: objA
}
objA.objB = objB
JSON.stringify(objB)
```

## Uncaught RangeError: Maximum call stack size exceeded

未捕获边界错误：超过最大调用堆栈大小 。一般是函数递归调用或者循环中没有加判断条件。比如：

```js
function countDown (n) {
    if (n < 0) {
        console.log('结束！')
    }
    console.log(n)
    n--
    countDown(n)
}
countDown(5)
```

### onbeforeunload事件无效

onbeforeunload 事件有兼容问题，及一些使用限制。详见 [JS基础知识03-onbeforeunload事件](broken://spaces/-M8fDLTBWl2H-MOzligj/pages/OjZuaUcOPpvucQ8QvyQk)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lizh.gitbook.io/knowledge/bugs/05js-chang-jian-wen-ti-gai-lan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
