Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js dojo & fun code #148

Open
zzz6519003 opened this issue Jun 17, 2021 · 3 comments
Open

js dojo & fun code #148

zzz6519003 opened this issue Jun 17, 2021 · 3 comments

Comments

@zzz6519003
Copy link
Owner

输出是什么?

const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2
  },
  perimeter: () => 2 * Math.PI * this.radius
}

shape.diameter()
shape.perimeter()

答案 B

注意 diameter 的值是一個一般的函式,但是 perimeter 的值是一個箭頭函式。

對於箭頭函式,this 關鍵字指向的是它當前周圍作用域,這個行為和一般函式不同。這表示當我們呼叫 perimeter 時,this 不是指向 shape 物件,而是它的周圍作用域(在範例中是 window)。

在 window 中沒有 radius 這個屬性,因此回傳 undefined。

@zzz6519003
Copy link
Owner Author

將會輸出什麽內容?

class Chameleon {
static colorChange(newColor) {
this.newColor = newColor
return this.newColor
}

constructor({ newColor = 'green' } = {}) {
this.newColor = newColor
}
}

const freddie = new Chameleon({ newColor: 'purple' })
freddie.colorChange('orange')
A: orange
B: purple
C: green
D: TypeError
答案
答案:D

colorChange 是一個靜態方法。靜態方法被設計爲只能被創造它們的建構子使用(也就是 Chameleon 中的 constructor ),並且不能傳遞給實例。因為 freddie 是一個實例,而靜態方法不能被實例使用,因此會抛出 TypeError 錯誤。

@zzz6519003
Copy link
Owner Author

  1. 输出什么?

async function* range(start, end) {
for (let i = start; i <= end; i++) {
yield Promise.resolve(i);
}
}

(async () => {
const gen = range(1, 3);
for await (const item of gen) {
console.log(item);
}
})();
A: Promise {1} Promise {2} Promise {3}
B: Promise {} Promise {} Promise {}
C: 1 2 3
D: undefined undefined undefined
答案
答案: C

我们给 函数range 传递: Promise{1}, Promise{2}, Promise{3},Generator 函数 range 返回一个全是 async object promise 数组。我们将 async object 赋值给变量 gen,之后我们使用for await ... of 进行循环遍历。我们将返回的 Promise 实例赋值给 item: 第一个返回 Promise{1}, 第二个返回 Promise{2},之后是 Promise{3}。因为我们正 awaiting item 的值,resolved 状态的 promsie,promise数组的resolved 值 以此为: 1,2,3.

@zzz6519003
Copy link
Owner Author

// function reduce(arr, fn, init) {
// 	var result = init;
// 	var count = 0;
// 	while (count < arr.length) {
// 		fn(result, arr[count], count, arr);
// 		count++;
// 	}
// 	return result;
// }

// Key points:
// 1) function 'step' is declared and alled immediately 
//- parens around function are convention to show it is being called on creation
// 2) 'value' is initially init, then becomes return value of fn 
//- perfect example of passing a func as an arg. Not just passing func, but *calling* it with specified args.

function reduce(arr, fn, init) {
	return (function step(index, value){
		if (index > arr.length - 1) {
			return value;
		}
		else {
			return step(index+1, fn(value, arr[index], index, arr));
		}
	})(0, init);
}

module.exports = reduce;

@zzz6519003 zzz6519003 changed the title js dojo js dojo & fun code May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant