Skip to main content

2-13

Array()

Array(5) // 會產生出 [ <5 empty items> ],雖然 console.log 出來會顯示 undefined,此時就算用 map() 遞迴也不會有效果 Array(5).fill() // 將陣列填滿後就不會是 empty items,此時就可以使用 map() 等方法遞迴 Array.from({length: 10}, (_, i) => i + 1) // 也可以用 Array.from 帶入只有長度的物件與 callback 來建立。

Git submodule

在主專案下新增 submodule:

git submodule add <repo url> <folder>

在主專案內修改 submodule 的東西後並 commit submodule 後, 主專案的 git 就會紀錄目前 submodule 是用這個 commit (HASH)。

更新 submodule 到目前主專案紀錄的 submodule commit (HASH) 上:

git submodule update

若是還沒有拉過 submodule,是第一次拉:

git submodule update —init

也可以在第一次 clone 主專案時: recurse-submodules 可以將主專案內的 submodules 也一併拉下來

git clone <repo url> --recurse-submodules

日後 submodule branch 有更新後,主專案內使用的 submodule 版本還會是在當初紀錄的 commit (Hash)上,需要:加上 --remote 來更新到最新的 branch 版本

git submodule update --recursive --remote

recursive 代表如果遞迴執行,Submodule 中如果有 Submodule,則一併更新。 remote 使用 .gitmodules 記錄的遠端分支來更新,而不是主專案所記錄的 HASH。

Replace 字串模板小技巧

function likes (names) {
const copyNames = names.slice(0, 3);
const templates = [
'no one likes this',
'{name} likes this',
'{name} and {name} like this',
'{name}, {name} and {name} like this',
'{name}, {name} and {n} others like this'
];
const idx = Math.min(names.length, 4);

return templates[idx].replace(/{name}|{n}/g, function (val) {
return val === '{name}' ? copyNames.shift() : names.length - 2;
});
}