diff --git a/学习文档.md b/学习文档.md index e69de29..516fe27 100644 --- a/学习文档.md +++ b/学习文档.md @@ -0,0 +1,23 @@ +Question : + +![img](http://ideapp/assets/icons/edit-icon.png) + +current_userInfo?.apiToken 这是什么写法? + +Answer : + +![img](http://ideapp/assets/icons/refresh.png) + + + +![Share icon](http://ideapp/assets/images/sharing-icons/share.png) + +这是可选链语法,它是 JavaScript ES2020 中新增的一种语法。可选链语法允许在进行属性访问、函数调用和数组索引操作时,对前面的变量进行空值(null 或 undefined)判断,以避免出现 TypeError 错误。具体来说,如果访问的对象不存在,则不会继续访问其属性,而是直接返回 undefined。这种语法的一般形式为: + +``` +object?.property +object?.method() +array?.[index] +``` + +在这里, `current_userInfo?.apiToken` 表示对 `current_userInfo` 对象进行访问,如果 `current_userInfo` 不存在或其 `apiToken` 属性不存在,则直接返回 `undefined` 。这种写法可以避免因空值导致的程序崩溃,并且可以编写更简洁、安全和可读性更好的代码。 \ No newline at end of file