const userTable = { '1': { money: 10000 } }
function withdraw (id, money) {
userTable[id].money -= money
}
function deposit (id, money) {
const currentMoney = userTable[id].money
currentMoney += money
userTable[id].money = currentMoney
}
withdraw('1', 10000) // 0
deposit('1', 1000) // 1000
const money1 = userTable[id].money
const money2 = userTable[id].money // *
money1 -= 10000
userTable[id].money = money1
money2 += 1000 // 認為餘額還是 10000
userTable[id].money = money2
// 11000, 現賺 10000 (也可能變 0)
https://codesandbox.io/s/ui-blocking-r354l
function bar () { console.log('baz') }
function foo () { bar() }
setTimeout(() => { console.log('setTimeout') }, 1000)
setInterval(() => { console.log('setInterval') }, 5000)
foo()
為什麼要用 setTimeout, 只用 Promise.resolve 不可以嗎?
setTimeout(() => {
console.log('setTimeout')
})
new Promise(resolve => {
console.log('promise executor')
resolve()
})
.then(() => {
console.log('promise then')
})
console.log('main')
請寫出以下印出 0~9 的順序
async function async1() {
new Promise(async resolve => {
Promise.resolve()
.then(() => {
console.log(0)
resolve()
})
})
.then(() => {
console.log(1)
})
await async2()
console.log(2)
}
async function async2() {
async3()
console.log(3)
}
function async3() {
new Promise((resolve) => resolve())
.then(() => {
console.log(4)
})
Promise.resolve()
.then(() => {
console.log(5)
})
}
async1()
setTimeout(function() {
console.log(6)
}, 0)
new Promise(resolve => {
console.log(7)
resolve()
})
.then(function() {
console.log(8)
})
.then(function() {
console.log(9)
})
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing