查找對象屬性的四種方法
1.Object.keys(..)會返回一個數組,包含所有可枚舉屬性( enumerable: true) 2.Object.getOwnPropertyNames(...)會返回一個數組,包含所有屬性,無論它們是否可枚舉 注:Object.keys(..)和Object.getOwnPropertyNames(..)都只會查找對象直接包含的屬性。 3.in操作符會檢查屬性是否在
1.Object.keys(..)會返回一個數組,包含所有可枚舉屬性( enumerable: true) 2.Object.getOwnPropertyNames(...)會返回一個數組,包含所有屬性,無論它們是否可枚舉 注:Object.keys(..)和Object.getOwnPropertyNames(..)都只會查找對象直接包含的屬性。 3.in操作符會檢查屬性是否在
CommonJS和ESM (ES6模塊)都是JavaScript模塊標準,但是它們有一些區別,主要包括以下幾個方面: 語法差異:CommonJS使用require語法引入模塊,而ESM使用import語法引入模塊。 加載方式:CommonJS使用同步加載方式,即遇到require就執行代碼,並等待結果返回後再繼續執行;而ESM使用異步加載方式,它是通過Promise的方式異步加載模塊,遇到i
async/await是ES2017帶來的異步編程語法糖,實現了讓異步代碼看起來像同步代碼的效果。 async/await 的本質是基於Promise,它並不是一種新的異步處理方式,而是Promise 的一種新的語法封裝升級。 下面是 async/await 的實現原理: async 函數本質上是一個 Generator 函數,返回一個 Promise 對象。 await 表達式本質上是一個 Pr
Jest 是當下最主流的前端測試框架 首先初始化ts環境 yarn add typescript --dev npx tsc --init 第二步:安裝ts下的jest yarn add jest @types/jest --dev 第三步:新建tests文件夾 tests/index.spec.ts it('init',()={ expect(true).toB
將一個數組轉換為一棵樹可以通過遞歸實現。假設我們有一個包含父節點與子節點關係的數組,如下所示: const arr = [ { id: 1, name: 'A', parent_id: null }, { id: 2, name: 'B', parent_id: 1 }, { id: 3, name: 'C', parent_id: 2 }, { id: 4, na
倒序排列數組的方法如下: 使用reverse()方法: const arr = [1, 2, 3, 4, 5]; const reversedArr = arr.reverse(); console.log(reversedArr); //[5, 4, 3, 2, 1] 使用for循環和unshift()方法: const arr = [1, 2, 3, 4, 5]; const re