Live Note

Remain optimistic

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
let worker = {
someMethod() {
return 1
},

slow(x) {
console.log("called with " + x)
return x * this.someMethod()
},
}

function decorator(func) {
const cache = new Map()

return function (x) {
if (cache.has(x)) {
console.log("cache hit")
return cache.get(x)
}

const result = func.call(this, x)
cache.set(x, result)

return result
}
}

worker.slow = decorator(worker.slow)

console.log(worker.slow(2))
console.log(worker.slow(2))

Injection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function injection(func, ...argsBound) {
return function (...args) {
return func.call(this, ...argsBound, ...args)
}
}

// usage
let user = {
firstName: "Edward",
say(time, phrase) {
console.log(`[${time}]: ${this.firstName} ${phrase}`)
},
}

user.say = injection(
user.say,
new Date().getHours() + ":" + new Date().getMinutes()
)

user.say("Hi")

arrow function

1
2
3
4
5
6
7
8
9
10
11
12
function defer(f, ms) {
return function () {
setTimeout(() => f.apply(this, arguments), ms)
}
}

function sayHello(name) {
console.log(`Hi, ${name}`)
}

const deferSay = defer(sayHello, 2000)
deferSay("Edward")

prototype

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let user = {
name: "Edward",
hello(name) {
console.log(`hi ${this.name}, this is ${name}`)
},
}

Function.prototype.defer = function (ms) {
let f = this
return function (...arg) {
setTimeout(() => f.apply(this, arg), ms)
}
}

user.hello = user.hello.defer(1000)
user.hello("Ejklfj")

Delete Service Worker on Safari

To delete a service worker on Safari, you can follow these steps:

  1. Open Safari Developer Console (Press Command + Option + C)
  2. Copy and paste the following JavaScript code into the console:
1
2
3
4
5
6
7
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.unregister().then(function () {
console.log("Service Worker Unregistered:", registration.scope)
})
}
})

This code will:

  1. Get all registered service workers using navigator.serviceWorker.getRegistrations()
  2. Loop through each registration
  3. Unregister each service worker using the unregister() method
  4. Log a confirmation message with the scope of the unregistered service worker

You can verify that the service worker has been successfully removed by:

  1. Opening Safari Developer Tools
  2. Going to the Application tab
  3. Checking the Service Workers section - it should now be empty

Note: After unregistering the service worker, you might need to close all tabs and restart Safari for the changes to take full effect.

A drawer animation HOC component

Requirements:

  • react-native-reanimated: animation
  • react-native-root-siblings: root sibling
  • Functional component: require hooks

Drawer HOC

Read more »

ES2017 新增

padStart(), padEnd()

字符串补全功能,接受两个参数,如果第二个参数为空,则默认使用空格补全。

1
2
3
4
5
6
// fill with the template
"12".padStart(3, "000") // '012'
"12".padEnd(3, "000") // '120'

// fill with space
"12".padStart(4) // ' 12'

ES2019 新增

trimStart(), trimEnd()

String.trim()的行为一致,不会修改原字符串。

1
2
3
4
5
6
7
let str = "    string    "

// str.trimLeft()
str.trimStart() // 'string '

// str.trimRight()
str.trimEnd() // ' string'

什么是 Diamond

Diamond 指的是一种设计模式,称为 EIP-2535 Diamonds。这种模式用于构建可模块化和可升级的以太坊智能合约。

Diamond

  • 模块化架构:Diamond 是一种创建单一、模块化合约(称为 Diamond)的方式,该合约将不同的功能委托给分离的、可互换的合约组件(称为 Facets)。这有助于更好地组织代码和分离关注点。
  • Facets:每个 Facet 包含特定的功能,并可以独立升级。例如,一个 Facet 可能处理用户管理,而另一个处理交易。
  • Diamond Storage:在 Diamond 模式中,合约的存储结构被设计成可以支持不同 Facets 的升级,而不影响整体合约的其他部分。

对外部世界(如用户界面、其他智能合约和软件/脚本)而言,Diamond 看起来是一个单一的智能合约,具有一个单一的以太坊地址。但在内部,它使用一组称为 Facets 的合约来处理其外部功能,这些 Facets 对外部是隐藏的。

Read more »