2024年4月29日 星期一

初探 Intl.NumberFormat

 初探 Intl.NumberFormat

前言

  這次來學習透過 Intl.NumberFormat 顯示貨幣價值,在此做個紀錄。


內容

  範例如下

let number = 1234;
//
console.log( new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' 
  }).format( number) );//1.234,00 €
//
console.log( new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' 
  }).format( number) );//¥1,234
//
console.log( new Intl.NumberFormat('zh-TW', { style: 'currency', currency: 'TWD' 
  }).format( number) );//$1,234.00


使用的方法跟其他的 Intl 差不多,不過要注意 currency 這個參數,這個參數引響顯示幣值的符號,要輸入的代碼可以在 [ en.wikipedia.org ] ISO 4217 查詢,區域碼會引響的是貨幣符號在前後與是否顯示小術後兩位。


參考資料

[ developer.mozilla.org ] Intl.NumberFormat

[ en.wikipedia.org ] ISO 4217

2024年4月22日 星期一

改用凌晨的方式表達時間

 改用凌晨的方式表達時間

前言

  Intl.DateTimeFormat() 有參數可以改以凌晨的方式來表達時間,在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 16, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h11',
  dayPeriod : 'narrow',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 凌晨0:23:16.738
//
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h11',
  dayPeriod : 'short',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 凌晨0:23:16.738
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h11',
  dayPeriod : 'long',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 凌晨0:23:16.738


改變的方法是透過 dayPeriod ,參數有三種,分別是 narrow 、 short 與 long ,範例個別示範一次,要注意的是 dayPeriod  必須搭配 hourCycle 來使用,只有當 hourCycle  是 h11 與 h12 時才有作用,範例的結果三個都是一樣的,可能跟區域有關,換成 fr 的話,第三個會有不同。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor'


相關文章與資料

初探 Intl.DateTimeFormat

調整 Intl.DateTimeFormat 的格式

自訂 Intl.DateTimeFormat 的格式

調整 Intl.DateTimeFormat 的時間為 24 小時制

在 Intl.DateTimeFormat 顯示時區

2024年4月15日 星期一

在 Intl.DateTimeFormat 顯示時區

 在 Intl.DateTimeFormat 顯示時區

前言

  先前使用 Intl.DateTimeFormat 都沒顯示時區,要如何顯示呢?在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 16, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'short',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [GMT+8]
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'long',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [台北標準時間]
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'shortOffset',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [GMT+8]
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'longOffset',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [GMT+08:00]
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'shortGeneric',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [台灣時間]
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  hourCycle : 'h23',
  timeZoneName : 'longGeneric',
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  
}).format(date));//2020年12月21日 星期一 00:23:16.738 [台北標準時間]


透過 timeZoneName 可以顯示時區,但只能用於自訂格式,如果與 dateStyle 或 timeStyle 混用會報錯,有六種參數可以用, short 、 long 、shortOffset 、 longOffset 、 shortGeneric 與 longGeneric ,範例分別個示範一次。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor'


相關文章與資料

初探 Intl.DateTimeFormat

調整 Intl.DateTimeFormat 的格式

自訂 Intl.DateTimeFormat 的格式

調整 Intl.DateTimeFormat 的時間為 24 小時制

2024年4月8日 星期一

進階調整 Intl.DateTimeFormat 的時間為 24 小時制

 進階調整 Intl.DateTimeFormat 的時間為 24 小時制

前言

  在先前的 調整 Intl.DateTimeFormat 的時間為 24 小時制 介紹了調整為 24 小時制的方法,但調整的方法其實不只這一種,有另外一種調整法,在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 16, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
  hourCycle : 'h23',
}).format(date));//2020/12/21 00:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
  hourCycle : 'h24',
}).format(date));//2020/12/21 24:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
  hourCycle : 'h12',
}).format(date));//2020/12/21 凌晨12:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
  hourCycle : 'h11',
}).format(date));//2020/12/21 上午0:23


上次 調整 Intl.DateTimeFormat 的時間為 24 小時制 是透過 hour12 來調整,這次會使用 hourCycle ,有 4 種參數可以調,分別是 h11 、h12 、h23 與 h24 ,範例都分別示範一次,同為24 小時制有兩種顯示方式,一種顯示 00 ,另一種顯示 24 ,h11 與 h12 也是一樣。使用上要注意 hour12 與 hourCycle  最好不要同時使用,可能在不同瀏覽器會有不同的結果,hour12  為 True 可以用 h12 來替代,為 False 時使用 h24 來替代,這樣就可以完全用不到 hour12  ,也就不會發生混用的情形。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor'


相關文章與資料

初探 Intl.DateTimeFormat

調整 Intl.DateTimeFormat 的格式

自訂 Intl.DateTimeFormat 的格式

調整 Intl.DateTimeFormat 的時間為 24 小時制

2024年4月1日 星期一

調整 Intl.DateTimeFormat 的時間為 24 小時制

 調整 Intl.DateTimeFormat 的時間為 24 小時制

前言

  在先前示範 Intl.DateTimeFormat 都是採取 12小時制,會分上午或下午,要如何改成 24 小時制呢?在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 5, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
  hour12 : false
}).format(date));//2020/12/20 13:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3,
  formatMatcher:'best fit',
  hour12 : false
}).format(date));//2020年12月20日 星期日 13:23:16.738


透過第二個參數裡的 hour12 參數就可以調整成 24 小時制,該值預設是 true ,有舊是 12 小時制,將其設為 false 就是 24 小時制,此參數不管是常見的格式或自訂的格式都可以使用,範例分別都示範一次。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor'


相關文章與資料

初探 Intl.DateTimeFormat

調整 Intl.DateTimeFormat 的格式

自訂 Intl.DateTimeFormat 的格式

2024年3月25日 星期一

自訂 Intl.DateTimeFormat 的格式

 自訂 Intl.DateTimeFormat 的格式

前言

  在之前的 調整 Intl.DateTimeFormat 的格式 示範如何調整格式,但都是常見的制式化格式,這些格式都不會顯示秒,如果要顯示秒就要靠訂定格式,在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
}).format(date));//2020/12/20 上午11:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second : 'numeric',
  fractionalSecondDigits: 3
}).format(date));//2020年12月20日 星期日 上午11:23:16.738


要自訂格式一樣是透過第二個參數,直接輸入要顯示的部分的參數即可,不只可以顯示秒,還可以顯示千分之一秒還有星期幾,這些是制式化格是不會顯示的,範例先示範制式化的格式,接著在示範這次的自訂格式,有一點是要注意的,制式化格式的參數 dateStyle 與 timeStyle ,式不可以跟自訂的參數混用的!只要混用就會報錯。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor


相關文章與資料

初探 Intl.DateTimeFormat

調整 Intl.DateTimeFormat 的格式

2024年3月18日 星期一

關於 smart failure predicted 的處理

 關於 smart failure predicted 的處理

前言

  最近有硬碟損壞,開機時會出現 smart failure predicted ,本以為硬碟就這麼毀了,資料來不及複製有些可惜,但查了一下網路發現這個訊息不是指硬碟毀了,在此把學習的過程做個紀錄。


內容

   smart failure predicted 指的是 smart test 有問題,不是硬碟毀了,指的是硬碟"快"毀了,既然是"快"毀了代表其實它還沒毀壞,可以在 BIOS 裡把 smart test 的選項關閉後再試著開機,這樣就可以進到作業系統把"快"毀了的硬碟資料複製出來。具體的操作可以參照以下



雖然我的主機板也是 ASUS ,但 Smart test 的選項位置與影片中的選項不一樣,但只要記得找 smart test 這個字詞,它的選項會是開與關,把它設為關閉後就可以跳過 smart failure predicted ,解著就可以進到作業系統。這個"快"毀了的硬碟在複製時會和一般的硬碟差很多,有的時候複製很正常,有的檔案複製時會像卡住一樣要複製很久,我的 2T 硬碟複製 4 天還沒結束,如果要複製"快"毀了的硬碟要準備好可以長時間不關機的環境。


2024年3月11日 星期一

調整 Intl.DateTimeFormat 的格式

 調整 Intl.DateTimeFormat 的格式

前言

  在先前的 初探 Intl.DateTimeFormat 提到 Intl.DateTimeFormat 的基本用法,但只能顯示日期沒辦法顯示時間,要如何顯示時間呢?在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
  timeStyle: 'short',
}).format(date));//2020/12/20 上午11:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  timeStyle: 'short',
}).format(date));//上午11:23
//
console.log(new Intl.DateTimeFormat('zh-tw',{
  dateStyle: 'short',
}).format(date));//2020/12/20
//
console.log(new Intl.DateTimeFormat('en-us').format(date));//12/20/2020
//
console.log(new Intl.DateTimeFormat('en-us',{
  dateStyle: 'short',
}).format(date));//12/20/20
console.log(new Intl.DateTimeFormat('en-us',{
  dateStyle: 'medium',
}).format(date));//Dec 20, 2020


要顯示時間可以透過 Intl.DateTimeFormat() 的第二個參數來達成,第二個參數可以輸入 dateStyle 與  timeStyle ,兩個參數各別都可以輸入 short 、medium 、 long 與 full ,按字面上的意思就可以知道效果,範例開頭透過都輸入 short ,就可以得到同時有日期與時間的結果,如果只需要時間,就只輸入 timeStyle 參數,就可以得到只顯示時間的結果,同理,如果只顯示日期的話就只輸入 dateStyle 即可。

  範例最後三個使用是在說明在不輸入參數的顯示沒被法透過透過固定的 dateStyle  與 timeStyle  來取得 ,不輸入參數時是 12/20/2020 ,但不論 dateStyle 輸入 short 或 medium 都無法得到何不輸入的參數同樣結果。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat() constructor


相關文章與資料

初探 Intl.DateTimeFormat


2024年3月4日 星期一

初探 Intl.DateTimeFormat

 初探 Intl.DateTimeFormat

前言

  如果時間格式需要在地化時要如何做呢? JavaScript 本身提供在地化的方法,也就是 Intl.DateTimeFormat() ,在此把學習的過程做個紀錄。


內容

  範例如下

const date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
console.log(new Intl.DateTimeFormat('en-US').format(date));//12/20/2020
console.log(new Intl.DateTimeFormat('zh-tw').format(date));//2020/12/20
console.log(new Intl.DateTimeFormat('fr').format(date));//20/12/2020


使用 Intl.DateTimeFormat() 的方式很簡單,用 new 方式來喚起!並輸入要在地化的區域碼,再對創建的物件喚起 format() 並輸入要在地化的時間,就可以得到在地化的結果,範例的美國時間順序是"月/日/年",台灣是"年/月/日",最後法國是 "日/月/年"。


參考資料

[ developer.mozilla.org ] Intl.DateTimeFormat

2024年2月26日 星期一

檢查 DisplayNames 的輸入碼是否有效

 檢查 DisplayNames 的輸入碼是否有效

前言

  在使用 DisplayNames.of() 時,如果輸入無效時不會有任何的錯誤拋擲,只是回傳輸入碼而已,可以用錯誤拋擲攔截嗎?答案是可以的,在此把學習的過程作的紀錄。


內容

  範例如下

console.log(
  new Intl.DisplayNames("zh-tw", { type: "region", fallback: "code" }).of("TW"),
);//台灣
console.log(
  new Intl.DisplayNames("zh-tw", { type: "region", fallback: "code" }).of("DLC"),
);//RangeError: invalid_argument


檢查的方法是在 Intl.DisplayNames() 時加入 fallback 屬性,並輸入 code ,接著在喚起 DisplayNames.of() 時,如果輸入碼無效時就會拋擲錯誤,範例先輸入有效的輸入碼,結果與平常無異,接著輸入無效的輸入碼 DLC ,結果就會拋擲錯誤。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames.prototype.of()

2024年2月19日 星期一

取得 DisplayNames 的成員

 取得 DisplayNames 的成員

前言

  DisplayNames  是透過 Intl.DisplayNames() ,透過回傳的 Object 來使用,不過該物件不提供公開的成員來取得當初初始化的參數,如 type...等 ,查詢後發現是可以的,但不能直接取得,在此把學習的過程做個紀錄。


內容

  要取得 DisplayNames 的成員可以透過 DisplayNames.resolvedOptions() 所回傳的物件來取得,範例如下

const displayNames = new Intl.DisplayNames(
  ["zh-tw"], 
  { type: "region" }
);
//
const usedOptions = displayNames.resolvedOptions();
console.log(usedOptions.locale); // "zh-TW"
console.log(usedOptions.type); // "region"


透過 DisplayNames.resolvedOptions() 的回傳物件就可以取得當初的初始化成員,如 locale 或 type ,感學這設計很不直覺,感覺應該設計成  Intl.DisplayNames() 會傳的物件裡面會提供 getter 才對。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames.prototype.resolvedOptions()

2024年2月12日 星期一

取得貨幣名稱的方法

 取得貨幣名稱的方法

前言

  這次來學習如何取得貨幣名稱,在此把學習的過程做個紀錄。


內容

  範例如下

let currencyNames = new Intl.DisplayNames(
  ["zh-tw"], 
  { type: "currency" }
);
console.log( currencyNames.of("USD") ); // "美元"
console.log( currencyNames.of("TWD") ); // "新台幣"
console.log( currencyNames.of("CNY") ); // "人民幣"


取得字體名稱是透過 [ developer.mozilla.org ] Intl.DisplayNames 來取得,要記得把屬性 type 改成 currency ,接著就可以跟往常一樣透過 of() 來取得 ,貨幣的參數可以透過 [ zh.wikipedia.org ] ISO 4217 來查詢,範例的人民幣是使用 CNY 而非RMB ,字碼用沒有分大小寫,盛至可以大小寫混著用,只要字碼正確即可。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames

[ zh.wikipedia.org ] ISO 4217

2024年2月5日 星期一

取得字體名稱的方法

 取得字體名稱的方法

前言

  這次來學習如何取得字體名稱,在此把學習的過程做個紀錄。


內容

  範例如下

const scriptNames = new Intl.DisplayNames("zh-tw", {
  type: "script",

});
console.log( scriptNames.of("Kana") );//片假名 
console.log( scriptNames.of("Hira") );//平假名 
console.log( scriptNames.of("Hans") );//簡體 
console.log( scriptNames.of("Hant") );//繁體 


取得字體名稱是透過 [ developer.mozilla.org ] Intl.DisplayNames 來取得,要記得把屬性 type 改成 script ,接著就可以跟往常一樣透過 of() 來取得,字體的參數可以透過 [ zh.wikipedia.org ] ISO 15924 來查詢,要注意的是參數的開頭都是大寫!不能用小寫,不然結果會是錯的。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames

[ zh.wikipedia.org ] ISO 15924

2024年1月29日 星期一

取得語言名稱的方法

 取得語言名稱的方法

前言

  在先前的 取得地區名稱的方法 介紹取得地區名稱的方法,這次來學習取得語言名稱的方法,在此做個紀錄。


內容

  範例如下

const languageNames = new Intl.DisplayNames("zh-tw", {
  type: "language",

});
console.log( languageNames.of("fr") );//法文 
console.log( languageNames.of("zh") ); //中文
console.log( languageNames.of("fr-CA") );//法文(加拿大) 
console.log( languageNames.of("zh-hant") );//繁體中文 
console.log( languageNames.of("zh-hans") );//簡體中文


取得語言名稱一樣是透過 Intl.DisplayNames() ,在第二個參數的 type 屬性填上 language ,接著就可以一樣透過 of() 來取得語言名稱,取得語言名稱要用語言碼而非地區碼,像法文是 fr ,剛好跟地區名稱一樣,zh 指的是中文,語言也可以有地區性,如 fr-ca ,如果要指繁體中文使用的語言碼是 zh-hant ,而簡體中文的語言碼是 zh-hants 。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames


相關文章與資料

取得地區名稱的方法

2024年1月22日 星期一

取得地區名稱的方法

 取得地區名稱的方法

前言

  最近需要取得區域碼的中文,在此把學習的過程作的紀錄。


內容

  範例如下

const regionNames = new Intl.DisplayNames(['zh-TW'], {
  type: 'region',
});
console.log( regionNames.of('TW') ); // 台灣
console.log( regionNames.of('US') ); // 美國


要取得區域碼的中文可以透過 Intl.DisplayNames() 來取得,參數1 是區域碼,參數2 是選項,區域碼可以透過 [ www.iana.org ] Root Zone Database 來查詢,前方的 zh 是中文的意思,選項的部分只要照著範例輸入就是取得區域碼,由於 Intl.DisplayNames() 並非單純設計來取得區域碼,如需要了解更多可以到 [ developer.mozilla.org ] Intl.DisplayNames 。使用 Intl.DisplayNames()  要注意是透過 new 來取得 Object ,而非直接喚起!接著就可以透過 of() 來取得區域碼的中文。 Intl.DisplayNames()  的參數1 會決定最後輸出的語言, of() 是查詢的結果,這是兩個地方輸入區域碼的不同之處。


參考資料

[ developer.mozilla.org ] Intl.DisplayNames

[ www.iana.org ] Root Zone Database

[ blog.othree.net ] zh-tw還是zh-TW

2024年1月15日 星期一

初探 WeakSet

 初探 WeakSet

前言

  在先前的 初探 WeakMap 學習 WeakMap ,這次學習 WeakSet ,在此把學習的過程做個紀錄。


內容

  範例如下

let set = new WeakSet();
let obj1 = {};
let obj2 = {};
let obj3 = {};
//
set.add( obj1 );
set.add( obj2 );
//
console.log( set.has( obj1 ) );//true
console.log( set.has( obj3 ) );//false
//
set.delete( obj1 );
console.log( set.has( obj1 ) );//false


WeakSet 與 WeakMap 相似,功能是存放 Object 的 Set 。範例透過 WeakSet.add() 來新增,用WeakSet.has() 確認存在,如果要消除可以透過 WeakSet.delete(),用起來其實跟 Set 沒什麼兩樣,不過 WeakSet 不用像 WeakMap 擔心內容從外部移除,因為 WeakSet 只有 key ,而沒有 value。


參考資料

[ developer.mozilla.org ] WeakSet


相關文章與資料

初探 WeakMap

2024年1月8日 星期一

初探 WeakMap

 初探 WeakMap

前言

  最近抽空學習 WeakMap ,在此把學習的過程做個紀錄。


內容

  WeakMap 用起來跟 Map 很像 ,只是 Key 的部分用的是 Object ,範例如下

let wm = new WeakMap();
let obj1 = {};
let obj2 = {};
let obj3 = {};
//
wm.set( obj1 , 1234 );
console.log( wm.get(obj1) );//1234
//
wm.delete( obj1 );//
console.log( wm.get(obj1) );//undefined
console.log( obj1 !== undefined );//true
//
obj1 = undefined;
console.log( obj1 !== undefined );//false
//
wm.set( obj2 , obj3 );
console.log( wm.get(obj2) !== undefined );//true
obj2 = undefined;
console.log( obj3 !== undefined );//true


範例開始用 obj1 示範了 get() 與 set() 的基本用法,接著用 delete() 來刪除 ,要注意刪除後 obj1 不會被刪除,接著就是 WeakMap 與 Map 最大的不同, Map 不需要擔心 Key 會被從外部刪除,但 WeakMap 就要考慮,範例將 obj1 設為 undefiend 後就會發生 Key 消失,也正因為 obj1 消失,所以也就沒辦法用 get() 方式取得資料了!接著範例示範 Key 與 Value 都是 Object ,實驗如果 Key 消失了,Value 也會被刪除嗎?結果 Value 還是存在,只有 Key 會消失。


參考資料

[ developer.mozilla.org ] WeakMap

2024年1月1日 星期一

變壓器接頭改造

 變壓器接頭改造

前言

  最近 [ www.octopus.com.tw ] 筆型刻模機 8pc 110v/220v 會發生一下有電一下沒電,只要轉一下變壓器的接頭就可以改善沒電的狀況,所以就研判是變壓器接頭接觸不良,在此把解決的過程做個紀錄。


內容

  變壓器接觸不良的問題,只要重買一個原廠的變壓器即可,但是我一直很介意原廠的變壓器接頭是"L"字的彎頭,彎頭在操作刻模機時會很礙事,所以我想直接買一個同規格的變壓器且是直頭來取代,上網搜索了一下,結果發現 18V 是個不常見的規格,所以選擇很少,找到了電壓符合的,但接頭卻是彎頭。

  在搜尋的過程中也發現有變壓器的接頭是可以換接頭的,所以如果要直頭 18V 的話,只能指望透過可換接頭變壓器加上對應接頭,量了一下原廠接頭,發現規格是 3.5*1.35mm ,所以最後就買了18V/1A 可換接頭變壓器與對應的  3.5*1.35mm 接頭。


  買回來使用後發現刻模機遽然不會動!本以為是接頭規格不正確,結果發現是接頭的長度不一樣,如下圖


接頭的長度差異


圖中的綠色是買來的接頭的原始長度,紅色的部分是多切出來的長度,可以使用刻模機搭配磨頭來削切,切完之後再插入刻模機後就可使用。


  雖然最後我得到了直頭的變壓器,但我還是不禁廠商刻意把接頭做的很長是不是故意的?廠商選用不常見的規格就可以逼消費者買原廠變壓器,作為消費者的我很難認同,不過還好這次我可透過改造接頭來達到目的。


參考資料

[ www.octopus.com.tw ] 筆型刻模機 8pc 110v/220v