<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
class Singleton {
static instance = null
static getInstance() {
if (Singleton.instance === null) {
Singleton.instance = new Singleton()
}
return Singleton.instance
}
}
let p1 = Singleton.getInstance()
let p2 = Singleton.getInstance()
console.log(p1 === p2)
console.log(p1 instanceof Singleton)
console.log(p2 instanceof Singleton)
console.log(p1)
</script>
</body>
</html>
【设计模式】单例模式
最新推荐文章于 2025-08-10 08:32:47 发布