企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### `Object.freeze()` `Object.freeze()` 方法可以冻结一个对象,冻结指的是不能向这个对象添加新的属性,不能修改其已有属性的值,不能删除已有属性,以及不能修改该对象已有属性的可枚举性、可配置性、可写性。也就是说,这个对象永远是不可变的。该方法返回被冻结的对象。 * 浅冻结 * 深冻结 > https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze ### `Object.defineProperty(obj,prop,descriptor)` > 直接在一个对象上定义一个新的属性,或者修改一个对象的现有属性,并返回这个对象. #### 参数 * `obj`:目标对象 * `prop`:待定义或修改的属性名称 * `descriptor`:待定义或修改属性的描述符 #### 示例 ```js var bValue; var o = {}; // 创建一个新对象 Object.defineProperty(o, "b", { get : function(){ return bValue; }, set : function(newValue){ bValue = newValue; }, enumerable : true, configurable : true, writable:true//为false时代表属性"不可写" }); ``` ### `IIFE` 即**立即执行函数表达式** ### How to convert a `Set` to an `Array`? ``` let setVal = new Set(['b','c','b','a']); //1.Array.from console.log(Array.from(setVal));//[b,c,a] //2.spread console.log([...setVal]) ``` ### js取随机数 ```js Math.random() ``` ### 函数列表参数`arguments` 首先,它是一个类数组对象`Array-Like Object`,就是拥有`length`属性,但不能使用数组方法(`forEach`,`map`); * 将`arguments`对象转换为数组对象 ``` //1 arguments = [].slice.call(arguments); //2 arguments = Array.from(arguments) //3 [..arguments] ``` * [链接](https://segmentfault.com/a/1190000008620953) ### `commonjs`中`exports`与`module.exports`的区别 `exports`对象是通过形参的方式传入的,直接赋值形参会改变形参的引用,但不能改变作用域外的值. 而`module.exports`不改变形参的 * * * * * ### ES5内置函数`Array.prototype.reduce()` >[info] The `reduce()` method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. 该方法是个累加器,适合将数组中的元素从左到右减少到一个单一的值。 >[success] `array.reduce(function(total, currentValue, currentIndex, arr), initialValue)` * `total`:必需,初始值或计算后的返回值; * `currentValue`:必需,当前元素; * `currentIndex`:可选,当前元素的索引; * `arr` :可选,当前元素所属的数组对象。 ```js const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10 // 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15 ``` #### `reduceRight()` >[info] The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. ```js const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight( (previousValue, currentValue) => previousValue.concat(currentValue) ); console.log(array1); // expected output: Array [4, 5, 2, 3, 0, 1] ``` ### 操作位符 | 运算符 | 用法 | 描述 | | --- | --- | --- | | 按位与( AND) | `a & b` | 对于每一个比特位,只有两个操作数相应的比特位都是1时,结果才为1,否则为0。 | | 按位或(OR) | `a | b` | 对于每一个比特位,当两个操作数相应的比特位至少有一个1时,结果为1,否则为0。 | | 按位异或(XOR) | `a ^ b` | 对于每一个比特位,当两个操作数相应的比特位有且只有一个1时,结果为1,否则为0。 | | 按位非(NOT) | `~ a` | 反转操作数的比特位,即0变成1,1变成0。 | | 左移(Left shift) | `a << b` | 将 a 的二进制形式向左移 b (< 32) 比特位,右边用0填充。 | | 有符号右移 | `a >> b` | 将 a 的二进制表示向右移 b (< 32) 位,丢弃被移出的位。 | | 无符号右移 | `a >>> b` | 将 a 的二进制表示向右移 b (< 32) 位,丢弃被移出的位,并使用 0 在左侧填充。 | ### 冷知识`+` ```js console.log(+'2');//2 console.log(+false)//0 console.log(+function(){console.log('inner')});//NaN console.log(+function(){console.log('inner')}());//"inner" console.log(+[])//0 console.log(+{})//NaN console.log(+[2])//2 ``` >[success] 作用:将任何值转换为数字 ### `Object.create(null)`与`{}` `Object.create(null)`创建的对象没有任何属性和原型链,高度可定制,避免了原型链上同名方法被覆盖的尴尬情况; [https://www.imooc.com/article/26080](https://www.imooc.com/article/26080) ### Linux下执行`npm install node-sass`报创建文件夹没有权限 > `npm install --unsafe-perm` 解决 ​ 就是说 npm 出于安全考虑不支持以 root 用户运行,即使你用 root 用户身份运行了,npm 会自动转成一个叫 nobody 的用户来运行,而这个用户几乎没有任何权限。这样的话如果你脚本里有一些需要权限的操作,比如写文件(尤其是写 `/root/.node-gyp`),就会崩掉了。 为了避免这种情况,要么按照 npm 的规矩来,专门建一个用于运行 npm 的高权限用户;要么加 --unsafe-perm 参数,这样就不会切换到 nobody 上,运行时是哪个用户就是哪个用户,即使是 root。 - [参考链接1](https://blog.csdn.net/u014069688/article/details/84327190) - [unsafe-perm](https://docs.npmjs.com/misc/config#unsafe-perm) ### Docker相关操作 ```bash # 列出所有容器 docker ps -a # 停止容器 docker stop containerId # 删除容器 docker rm containerId # 进入容器 docker exec -it containerId bash ``` ### NPM带秘钥下载 ```bash npm install -g canoe-cli --registry=https://ipd-artifactory.cloudwalk.work/artifactory/api/npm/all-dept_npm-virtual/ --auth=YWxsLWRlcHRfYWxsLXJlcG8tdHlwZV9yZWFkX3VzZXI6QVA3R1gzMkJncFJhTEVYMVhpVUN1ck5oaWVx ``` ### ts找不到vue模块 创建`vue-shim.d.ts`,声明vue ```js declare module '*.vue' { import Vue from 'vue' export default Vue } ``` ### npm还原registry npm config set registry https://registry.npmjs.org ### ts中找不到自定义组件的模块 `tsconfig.json`中`types`中添加声明: ```js "types": [ "webpack-env", "./node_modules/vue-ui-components-template/types" ], ``` ### `vuex`中`action`传入多个参数 已知Vuex中通过actions提交mutations要通过context.commit(mutations,object)的方式来完成 然而commit中只能传入两个参数,第一个就是mutations,第二个就是要传入的参数 > 然而如果这么写的话就会报错:context.commit(mutations,item,num) 所以多个参数,需要通过字典的方式传入 ### `Uncaught SyntaxError: Unexpected token <` 解决方法:publicPath 不要写成相对路径`'./'` 要写成绝对路径 `/` ### 暗黑模式 `mix-blend-mode: difference`