> For the complete documentation index, see [llms.txt](https://lizh.gitbook.io/knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lizh.gitbook.io/knowledge/javascript-xi-lie/00-ji-chu-zhi-shi.md).

# 基础知识

## Js预处理和各种提升

```js
var c = 1;
function c (c) {
    console.log(c);
    var c = 3;
}
c(2);
//TypeError: c is not a function
```

预解析各种提升后，实际执行：

```js
var c;
c = function (c) {
    console.log(c);
    var c = 3;
};
c = 1;
c(2);
```

## 移动端判断

```js
const ua = navigator.userAgent.toLowerCase()
if (ua.match(/MicroMessenger/i) === "micromessenger") {}
if ( /android/i.test(ua)){}
if ( /ipad|iphone|mac/i.test(ua)){}                         
```

## JQuery中attr和prop的区别

`attr` 和 `prop` 分别是单词 `attribute` 和 `property` 的缩写。在 `JQuery` 中， `attribute` 表示 HTML 文档节点的属性，`property` 表示 JS 对象的属性。

标签自带的属性为称为**原生属性**，如 `checked、select、disabled`，有 `true、false` 两个属性。

* **attr()：** 设置和获取原生属性。

  用户点击选中表单元素，用 `attr()` 方法无法将其取消，不论 `“”、false、null` 都不行；用户点击取消选中后，用 `attr()` 方法无法将其选中。

  通过 `attr()` 给 `checked` 赋值为`false|true`， 用 `attr( )` 获取的值为 `"true|false"` 字符串；赋值为`null`，`attr()` 获取值为 `false` 布尔值；赋值为 `""`，`attr( )` 获取值为 `true` 布尔值。
* **prop( )：** 获取值 `checked、select、disabled` 为 `true|false` 布尔值。如果 `select` 的 `option` 定义为 `disabled`，则不能用 `val( )` 获取其 `value` 值，可用 `prop("value")`。

  不要使用 `removeProp( )` 来删除原生的属性。这将完全移除该属性，一旦移除，不能再次被添加到元素上，可使用 `prop( )` 设置为 `false` 代替。

`checkbox、radio、select` 元素的属性 `checked` 和 `selected`。使用 `attr()` 和 `prop()` 取的值有差异：

```html
<input id="chk1" type="checkbox" /> <!--不选中-->
<input id="chk2" type="checkbox" checked="checked"  /> <!--选中-->
```

```js
//prop()取值
$("#chk1").prop("checked") === false  
$("#chk2").prop("checked") === true  

//attr()取值
$("#chk1").attr("checked") === undefined
$("#chk2").attr("checked") === "checked"
```
