每日進度 1 (JS Concept) Flashcards

1
Q

作用域

A
  • var (scope) (hoisting)
      1. function scope
      1. global scope
    • 外 -> 內 (similar to scope chain, lexical outer environment)、i.e.
  • const、let (block)
    • { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

this in different type of functions

A
    1. normal function: 取決於 the fun. 在哪裡 “執行” (implicit binding)
    1. arrow function: 取決於 the fun. 在哪裡 “宣告”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

this的5種綁定priority

A
  1. arrow function
  2. new $fun () // 使用new關鍵字做綁定
  3. explicit binding (bind、apply、call)
  4. implicit binding (重點是怎樣被執行)
  5. default binding (未經修飾, 沒有xxx.xxx or 沒有值like null or undefined) ( i.e. $fun.call(undefined) )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

同步、非同步fun.的圖示

A

——–fun1——fun2——–fun3——–> // 同步

    /-------------fun2--------------\ ------fun1------------------------------------> // 非同步
             \------fun3----------/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

如何知道一個函式為同步或非同步

A
  • 你沒有辦法知道一個函式是同步或者是非同步,除非你看過文件、或者函式的內部實作
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[1. the callback way] 用3種解法來解下列 the function gets data in ajax way

// hint: call some APIs or fs.readFile in the getData function

const start = () => {
  const data = getData();
  console.log(data);
}
start();
A
  • callback: 在未來某一個時間點會完成執行的事情,wait for it callback
    • 傳callback function進去,作為 “未來會完成的事情” 的 “處理” 的 “預備”

function getData (cb = () => {}) {

	function getAjaxData () {
		fetch('https://jsonplaceholder.typicode.com/users')
    	.then((resp = {}) => resp.json())
      .then((respData = {}) => {
				// console.log(respData);
				cb(respData);
			})
			.catch((error = {}) => console.log(error));
	}
getAjaxData(); }
function start () {
  getData((theData = {}) => {
    console.log(theData);
  });
}

start();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Promise declaration (do sth. ajax with 相關處理) and applying

A
## Promise Declaration
const promise1 = new Promise((resolve, reject) => {
  // do sth. ajax
  if (404) reject(new Error('404'));
    //  不需要return
    //  不需要return
    //  不需要return

resolve(someDataFromAjax);
});

Promise Applying
promise1
.then((ajaxData) => console.log(ajaxData))
.catch((error) => console.log(error));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

[2. the promise way] 用3種解法來解下列 the function gets data in ajax way

// hint: call some APIs or fs.readFile in the getData function

const start = () => {
  const data = getData();
  console.log(data);
}
start();
A

!!! fetch也會回傳Promise !!!

function getData () {
	return new Promise((resolve, reject) => {
	    fetch('https://jsonplaceholder.typicode.com/usddders')
    	      .then((resp = {}) => resp.json())
              .then((respData = {}) => {
                 // console.log(respData);
                 resolve(respData);
              })
	      .catch((error = {}) => console.log(error));
	});
}
function start () {
    getData()
      .then((respData = {}) => console.log(respData))
      .catch((error = {}) => console.log(error));
}

start();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

fetch回傳?

A

Promise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

針對function包裝async代表什麼?(3點)

A
1. async function 的 『 底層 、Promise related』
我們可以把一個function用async包裝來轉成 [回傳Promise] 的形式!!! 
  - (async function的底層還是用Promise來實作)
  - async fun. 一律回傳 Promise
  - // 在該async. function中如果沒有errors,return $value會相當於 resolve($value)
2. 『 不是 』
// 不是所有非同步處理的函式都能夠用async await==包裝使用==,
// ==能否==用async await來==包裝使用==端視==該function的實作方式而定==
// 像是fs.readFile "就被設計為只吃callback形式" ,
// 所以無法用async await包裝改寫
3. 『 使用async await的 "限制" 』
// 要能夠使用 async await 去 apply the function
// 則該function一定要 return Promise
=> 因為async await的底層還是用Promise來實作,所以必須確保return Promise (或async/await)的函式才能夠用async await來操作。
// Promise 跟 async await 是 同一件事 !!!
// Promise 跟 async await 是 同一件事 !!!
// Promise 跟 async await 是 同一件事 !!!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Demonstration of timeoutDetectionPromise

A

https://hackmd.io/AlMvjFHOTAunEQQf20rgpA

  1. ==定義==技術難題
    - In my experience, the difficulty will go away if we have ==enough time==, we usually can find solutions and shift the pressure in the process if we have enough time
  • the emergency incident will be the great example of a difficult technical challenge.
  1. 舉例
    lots of 504 Timeout Errors been detected on the logging system (Splunk), that will cost the machine extra resources and the service will then redirected to 404 page, so it needs to be solved ASAP.
  2. 結果 (強調==時間、快速解決、跟定義前後呼應==)
    Finally, I solved the problem within 2 hours (including hotfix build on pipeline).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Demonstration of timeoutDetectionPromise (concept)

A

https://hackmd.io/AlMvjFHOTAunEQQf20rgpA

  1. ==定義==技術難題
    - In my experience, the difficulty will go away if we have ==enough time==, we usually can find solutions and shift the pressure in the process if we have enough time
  • the emergency incident will be the great example of a difficult technical challenge.
  1. 舉例
    lots of 504 Timeout Errors been detected on the logging system (Splunk), that will cost the machine extra resources and the service will then redirected to 404 page, so it needs to be solved ASAP.
  2. 結果 (強調==時間、快速解決、跟定義前後呼應==)
    Finally, I solved the problem within 2 hours (including hotfix build on pipeline).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Demonstration of timeoutDetectionPromise (code) 主要用了哪個 js function 和 Promise方法

A
export function timeoutDetectionPromise (timeout) {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
      reject(new Error('the timeout error msg that you need'));
    }, timeout);
	}); 
}

export function oauthDotGetWithTimeoutFunctionality(
callback,
oauthLib,
oauthUrl,
… // the parameters that you need
timeout,
) {
Promise.race([
new Promise((resolve, reject) => {
oauthLib.get(oauthUrl, null, null, function (oauthError, result) {
// Promise裡面除非需要,不然一般不太需要return

			if (oauthError) {
				reject(oauthError);
			}
				resolve(result);
			})
		}),
		timeoutDetectionPromise(3000),
	])
	.then((result) => {
		// do sth. with result
	})
	.catch((error) => {
		if (error.message === 'the timeout message that you need') {
            // timeout error
			// do sth. that you need
		return callback && callback(null, null);
	}
		// do sth. when it's not a timeout error
		callback && callback(error, null);
	});
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly