ECMA随手记
Object.fromEntries使用
let aa= [
['foo', 'bar'],
['baz', 42]
];
console.log(Object.fromEntries(aa))
输出:{baz:42,foo:'bar'}
语法使用
1.可选链(用于可选的属性访问)
let x = foo?.bar.baz();
也就是说,当定义了 foo 时,将计算 foo.bar. baz();但如果 foo 为 null 或 undefined,
程序就会停止运行并只返回 undefined;
等效下面这种写法。
let x = (foo === null || foo === undefined) ? undefined :foo.bar.baz();
2.空值合并(这个功能——?? 运算符可以在处理 null 或 undefined 时“回退”到一个默认值上)
let x = foo ?? bar();
这是一种新的途径来表示值 foo“存在”时会被使用;但当它为null或undefined 时,就在其位置计算bar().
等效于下面的写法。
let x = (foo !== null && foo !== undefined) ?
foo :
bar();
3.管道操作符(是单参数函数调用的语法糖)
let url = "%21" |> decodeURI;
传统语法,等效的代码是这样的:
let url = decodeURI("%21");
使用管道操作符的条件:bable7 以上
yarn add @babel/plugin-proposal-pipeline-operator
bable配置:plugins:[
[
"@babel/plugin-proposal-pipeline-operator",
{
"proposal": "smart"
}
]
]
该插件应使用proposal选项是必需的,并且应为以下之一:
"minimal" "smart" "fsharp"
creact-react-app脚手架使用绝对路径
1.相对路径的使用方式
import img from '../../../imagrs/bg.png'
2.绝对路径使用方式
import {classDecorator} from '@/component/wxShare'
配置:
在webpack.config.js文件中
alias: {
+ '@': path.resolve(__dirname, '../src'),
}
根目录下新建文件jsconfig.json
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
ECMA数据类型和值
*Undefined 类型
Undefined类型只有一个值,称为 undefined。任何尚未分配值的变量都具有该值undefined。
*null 类型
Null类型只有一个值,称为 null。
*Boolean 类型
布尔类型表示具有两个值的逻辑实体,称为 true 和 false。
*String 类型
具体可以参照:
[MDN](https://developer.mozilla.org/zhCN/docs/Web/JavaScript
/Reference/Global_Objects/String/prototype)
* Symbol 类型
Symbol 类型是可以用作对象属性键的所有非字符串值的集合,每个可能的符号值都是唯一且不可变的,
每个符号值都不可变地保存一个名为[[description]]的关联值,该值Undefined或是一个String值。
*Number 类型
console.log(4**2) /=> 16相当于4的平方
4**2 写法等价于 Math.pow (4,2)
左移位运算符(<<)
x << y
例如:y<<8,是将Y换算成二进制数后,向左平移8位,左边移出的被舍弃,右边补零
(y<<8左移8位是2进制的8位,相当于十进制数乘以256,不能采用对十进数进行左移进行计算)
*Object 类型