资讯专栏INFORMATION COLUMN

Useful APIs that you probably don't notice

崔晓明 / 3077人阅读

摘要:结果

Date Get the number of days in a month

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

</>复制代码

  1. function daysInMonth(year, month) {
  2. let date = new Date(year, month + 1, 0);
  3. return date.getDate();
  4. }
  5. /**
  6. * Note that JS Date month starts with 0
  7. * The following computes how many days in March 2017
  8. */
  9. console.log(daysInMonth(2017, 2)); // 31
  10. // how many days in Feb 2017
  11. console.log(daysInMonth(2017, 1)); // 28
  12. // how many days in Feb 2016
  13. console.log(daysInMonth(2016, 1)); // 29
getTimezoneOffset - get the time zone difference, in minutes, from current locale (host system settings) to UTC.

</>复制代码

  1. let now = new Date();
  2. console.log(now.toISOString()); //2018-03-12T01:12:29.566Z
  3. // China is UTC+08:00
  4. console.log(now.getTimezoneOffset()); // -480
  5. // convert to UTC
  6. let UTCDate = new Date(now.getTime() + now.getTimezoneOffset() * 60 * 1000);
  7. console.log(UTCDate.toISOString()); //2018-03-11T17:12:29.566Z
  8. //convert to UTC+03:00
  9. let eastZone3Date = new Date(UTCDate.getTime() + 3 * 60 * 60 * 1000);
  10. console.log(eastZone3Date.toISOString()); //2018-03-11T20:12:29.566Z
JSON [JSON.stringify(value[, replacer[, space]])](https://developer.mozilla.org... When replacer is a function - apply replacer before stringify the value.

</>复制代码

  1. JSON.stringify({
  2. a: 4,
  3. b: [3, 5, "hello"],
  4. }, (key, val) => {
  5. if(typeof val === "number") {
  6. return val * 2;
  7. }
  8. return val;
  9. }); //{"a":8,"b":[6,10,"hello"]}
when replacer is an array - use replacer as a white list to filter the keys

</>复制代码

  1. JSON.stringify({
  2. a: 4,
  3. b: {
  4. a: 5,
  5. d: 6
  6. },
  7. c: 8
  8. }, ["a", "b"]); //{"a":4,"b":{"a":5}}
space can be used to beautify the output

</>复制代码

  1. JSON.stringify({
  2. a: [3,4,5],
  3. b: "hello"
  4. }, null, "|--
  5. ");
  6. /**结果:
  7. {
  8. |-- "a": [
  9. |-- |-- 3,
  10. |-- |-- 4,
  11. |-- |-- 5
  12. |-- ],
  13. |-- "b": "hello"
  14. }
  15. */
String [String.prototype.split([separator[, limit]])](https://developer.mozilla.org...

</>复制代码

  1. "".split("") // []
separator can be a regular expression!

</>复制代码

  1. "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.

</>复制代码

  1. "abc1def2ghi".split(/(d)/); // ["abc", "1", "def", "2", "ghi"]
Tagged string literals

</>复制代码

  1. let person = "Mike";
  2. let age = 28;
  3. function myTag(strings, personExp, ageExp) {
  4. let str0 = strings[0]; // "that "
  5. let str1 = strings[1]; // " is a "
  6. // There is technically a string after
  7. // the final expression (in our example),
  8. // but it is empty (""), so disregard.
  9. // var str2 = strings[2];
  10. let ageStr;
  11. if (ageExp > 99){
  12. ageStr = "centenarian";
  13. } else {
  14. ageStr = "youngster";
  15. }
  16. return str0 + personExp + str1 + ageStr;
  17. }
  18. let output = myTag`that ${ person } is a ${ age }`;
  19. console.log(output);
  20. // that Mike is a youngster
null vs undefined

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

</>复制代码

  1. undefined == undefined //true
  2. null == undefined // true
  3. 0 == undefined // false
  4. "" == undefined // false
  5. false == undefined // false

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

</>复制代码

  1. // a is not defiend under global scope
  2. a == null // ReferenceError
  3. typeof a // "undefined"
Spread Operator(...)

spread operator works for objects!

</>复制代码

  1. const point2D = {x: 1, y: 2};
  2. const point3D = {...point2D, z: 3};
  3. let obj = {a: "b", c: "d", e: "f"};
  4. let {a, ...other} = obj;
  5. console.log(a); //b
  6. console.log(other); //{c: "d", e: "f"}
Reference

Speaking Javascript

MDN

Notice

If you want to follow the latest news/articles for the series of reading notes, Please 「Watch」to Subscribe.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/93463.html

相关文章

  • PHP|标准配置之php.ini (2)

    摘要: ;;;;;;;;;;;;;;;;;;; ; Module Settings ; ;;;;;;;;;;;;;;;;;;; [CLI Server] ; Whether the CLI web server uses ANSI color coding in its terminal output. cli_server.color = On [Date] ; Defines th...

    tianren124 评论0 收藏0
  • Make a simple custom EventEmitter

    Thoughts Recently I have been reading the book Async Javascript about JS asynchronicity and JS event is one of the useful solutions to the problem. To get a deeper understanding of how events work, I ...

    fizz 评论0 收藏0
  • You Don&#039;t Know JS》阅读理解——作用域

    摘要:在我们的程序中有很多变量标识符,我们现在或者将来将使用它。当我们使用时,如果并没有找到这个变量,在非严格模式下,程序会默认帮我们在全局创建一个变量。词法作用域也就是说,变量的作用域就是他声明的时候的作用域。 作用域 定义 首先我们来想想作用域是用来干什么的。在我们的程序中有很多变量(标识符identifier),我们现在或者将来将使用它。那么多变量,我咋知道我有没有声明或者定义过他呢,...

    codeKK 评论0 收藏0
  • How did I install Youcompleteme

    I always want imporve my skills on VIM. Thus recently I spent two days on setting up a VIM IDE for C/C++ proprogramming. Below are some useful steps for YCM. Specifications Mac InfoOS X Yosemite Versi...

    TalkingData 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<