2018-01-26 15:18:56 +08:00
|
|
|
|
|
|
|
|
|
### 方法的注释
|
|
|
|
|
|
2018-02-01 22:58:37 +08:00
|
|
|
|
方法写完之后(注意,一定要先写完整),我们在方法的前面输入`/**`,然后回车,会发现,注释的格式会自动补齐。
|
|
|
|
|
|
|
|
|
|
比如:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
/**
|
|
|
|
|
* 功能:给定元素查找他的第一个元素子节点,并返回
|
|
|
|
|
* @param ele
|
|
|
|
|
* @returns {Element|*|Node}
|
|
|
|
|
*/
|
|
|
|
|
function getFirstNode(ele){
|
|
|
|
|
var node = ele.firstElementChild || ele.firstChild;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 15:18:56 +08:00
|
|
|
|
### 断点调试
|
|
|
|
|
|
|
|
|
|
(1)先让程序运行一遍。
|
|
|
|
|
|
|
|
|
|
(2)f12,弹出代码调试工具
|
|
|
|
|
|
|
|
|
|
(3)打断点:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180124_2035.png)
|
|
|
|
|
|
|
|
|
|
然后刷新页面。
|
|
|
|
|
|
|
|
|
|
(4)一步步调试,每点击一次,执行一步:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180124_2036.png)
|
|
|
|
|
|
|
|
|
|
(5)监视变量:
|
|
|
|
|
|
|
|
|
|
当然,也可以添加变量或者表达式到监视窗口。操作如下:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180124_2037.png)
|
|
|
|
|
|
|
|
|
|
上图中,选择变量或表达式,然后右键add to watch.
|
|
|
|
|
|
|
|
|
|
然后监视窗口:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180124_2038.png)
|
|
|
|
|
|
|
|
|
|
|
2019-11-23 11:16:39 +08:00
|
|
|
|
### 2019-05-20-给数组、对象赋值
|
|
|
|
|
|
|
|
|
|
**数组赋值的正确写法**:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
this.todayList.splice(0, 0, ...dataList);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**对象赋值的正确写法**:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
Object.assign(this.dataObj, dataObj);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上方代码中,是将`dataObj` 的值追加到`this.dataObj`中。如果对象里属性名相同,会被覆盖。
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
|
|
|
|
|
2019-11-26 19:49:07 +08:00
|
|
|
|
### 2019-11-25-在新的窗口中打开url
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-11-26 19:49:07 +08:00
|
|
|
|
在原来的窗体中跳转到新页面:
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-11-26 19:49:07 +08:00
|
|
|
|
```javascript
|
|
|
|
|
window.location.href="要跳转的新页面";
|
|
|
|
|
```
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-11-26 19:49:07 +08:00
|
|
|
|
在新窗体中打开新页面:
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-11-26 19:49:07 +08:00
|
|
|
|
```javascript
|
|
|
|
|
window.open('你所要跳转的新页面');
|
|
|
|
|
```
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
|
|
|
|
|
2019-12-10 22:39:08 +08:00
|
|
|
|
### 2019-12-10-JavaScript 新特性:Optional Chaining(可选链式调用)语法
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-12-10 22:39:08 +08:00
|
|
|
|
以往写代码,我们一般都这么写:
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-12-10 22:39:08 +08:00
|
|
|
|
```javascript
|
|
|
|
|
if (result && result.user && result.user.name && result.user.name.length) {
|
|
|
|
|
console.log('qianguyihao');
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
有了 Optinal Chain 语法之后,就简洁很多了,可以这么写:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
if (result?.user?.name?.length) {
|
|
|
|
|
console.log('qianguyihao');
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参考链接:
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-12-10 22:39:08 +08:00
|
|
|
|
- 了解 JavaScript 新特性:Optional Chaining:<https://www.infoq.cn/article/2JDORgXrU6VmZ7jlyuFD>
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
2019-12-10 22:39:08 +08:00
|
|
|
|
- 原文链接: https://v8.dev/features/optional-chaining
|
2018-01-26 15:18:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|