Useful APIs that you probably don't notice

Date

Get the number of days in a month

The 0th day of next month is the last day of the current month.

function daysInMonth(year, month) {
    let date = new Date(year, month + 1, 0);
    return date.getDate();
}

/**
 * Note that JS Date month starts with 0
 * The following computes how many days in March 2017
 */
console.log(daysInMonth(2017, 2)); // 31 
// how many days in Feb 2017
console.log(daysInMonth(2017, 1)); // 28
// how many days in Feb 2016
console.log(daysInMonth(2016, 1)); // 29

getTimezoneOffset - get the time zone difference, in minutes, from current locale (host system settings) to UTC.

let now = new Date(); 
console.log(now.toISOString()); //2018-03-12T01:12:29.566Z
// China is UTC+08:00
console.log(now.getTimezoneOffset()); // -480
// convert to UTC 
let UTCDate = new Date(now.getTime() + now.getTimezoneOffset() * 60 * 1000);
console.log(UTCDate.toISOString()); //2018-03-11T17:12:29.566Z
//convert to UTC+03:00
let eastZone3Date = new Date(UTCDate.getTime() + 3 * 60 * 60 * 1000);
console.log(eastZone3Date.toISOString()); //2018-03-11T20:12:29.566Z

JSON

JSON.stringify(value[, replacer[, space]])

When replacer is a function - apply replacer before stringify the value.

JSON.stringify({
    a: 4,
    b: [3, 5, 'hello'],
}, (key, val) => {
    if(typeof val === 'number') {
        return val * 2;
    }
    return val;
}); //{"a":8,"b":[6,10,"hello"]}

when replacer is an array - use replacer as a white list to filter the keys

JSON.stringify({
    a: 4,
    b: {
        a: 5,
        d: 6
    },
    c: 8
}, ['a', 'b']); //{"a":4,"b":{"a":5}}

space can be used to beautify the output

JSON.stringify({
    a: [3,4,5],
    b: 'hello'
}, null, '|--\t');
/**結(jié)果:
{
|-- "a": [
|-- |-- 3,
|-- |-- 4,
|-- |-- 5
|-- ],
|-- "b": "hello"
}
*/

String

String.prototype.split([separator[, limit]])

''.split('') // []

separator can be a regular expression!

'abc1def2ghi'.split(/\d/);  //["abc", "def", "ghi"]

If the seperator is a regular expression that contains capturing groups, the capturing groups will appear in the result as well.

'abc1def2ghi'.split(/(\d)/);  // ["abc", "1", "def", "2", "ghi"]

Tagged string literals

let person = 'Mike';
let age = 28;

function myTag(strings, personExp, ageExp) {
  let str0 = strings[0]; // "that "
  let str1 = strings[1]; // " is a "

  // There is technically a string after
  // the final expression (in our example),
  // but it is empty (""), so disregard.
  // var str2 = strings[2];

  let ageStr;
  if (ageExp > 99){
    ageStr = 'centenarian';
  } else {
    ageStr = 'youngster';
  }

  return str0 + personExp + str1 + ageStr;
}

let output = myTag`that ${ person } is a ${ age }`;
console.log(output);
// that Mike is a youngster

null vs undefined

If we don't want to distinguish null and undefined, we can use ==

undefined == undefined //true
null == undefined // true
0 == undefined // false
'' == undefined // false
false == undefined // false

Don't simply use == to check for the existence of a global variable as it will throw ReferenceError. Use typeof instead.

// a is not defiend under global scope
a == null // ReferenceError
typeof a // 'undefined'

Spread Operator(...)

spread operator works for objects!

const point2D = {x: 1, y: 2};
const point3D = {...point2D, z: 3};

let obj = {a: 'b', c: 'd', e: 'f'};
let {a, ...other} = obj;
console.log(a); //b
console.log(other); //{c: "d", e: "f"}

Reference

Notice

  • If you want to follow the latest news/articles for the series of reading notes, Please 「Watch」to Subscribe.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末盅视,一起剝皮案震驚了整個(gè)濱河市缓艳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌未舟,老刑警劉巖瞬铸,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件溶弟,死亡現(xiàn)場(chǎng)離奇詭異仪壮,居然都是意外死亡绽榛,警方通過(guò)查閱死者的電腦和手機(jī)湿酸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)灭美,“玉大人推溃,你說(shuō)我怎么就攤上這事〗旄” “怎么了铁坎?”我有些...
    開(kāi)封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)犁苏。 經(jīng)常有香客問(wèn)我硬萍,道長(zhǎng),這世上最難降的妖魔是什么围详? 我笑而不...
    開(kāi)封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任朴乖,我火速辦了婚禮祖屏,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘欧聘。我一直安慰自己梯醒,他們只是感情好席怪,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著期丰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪漠嵌。 梳的紋絲不亂的頭發(fā)上咐汞,一...
    開(kāi)封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音儒鹿,去河邊找鬼化撕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛约炎,可吹牛的內(nèi)容都是我干的植阴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼圾浅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼掠手!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起狸捕,我...
    開(kāi)封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤喷鸽,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后灸拍,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體做祝,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年鸡岗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了混槐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡轩性,死狀恐怖声登,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情揣苏,我是刑警寧澤悯嗓,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站舒岸,受9級(jí)特大地震影響绅作,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛾派,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一俄认、第九天 我趴在偏房一處隱蔽的房頂上張望个少。 院中可真熱鬧,春花似錦眯杏、人聲如沸夜焦。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)茫经。三九已至,卻和暖如春萎津,著一層夾襖步出監(jiān)牢的瞬間卸伞,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工锉屈, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留荤傲,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓颈渊,卻偏偏與公主長(zhǎng)得像遂黍,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子俊嗽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容