在面向对象的程序设计中, 单例模式是最简单的设计模式,这种类型的设计模式属于创建型模型,所谓单例。就是整个程序有且仅有一个实例,该类只负责创建自己的对象,同时确保只有一个实例。1.1 为什么需要单例模式?单例模式是一个对象创建模式, 用于生产一个对象的实例,它可以确保系统中一个类只有一个实例,它用两个好处:对于频繁调用的对象,可以省略创建对象所花费的时间,这对那些重量级的对象来说,是非常可观的一笔系统开销。在内存中一个实例对象, 节省了内存空间, 避免了重复得创建和销毁对象,可以提高系统的性能, 避免对多重复资源的重复占用, 可以全局访问。1.2 起步通常来说我们每次通过class类new 声明的实例都是一个全新的对象,所以我们多次重复new一个类生成的实例对象都是不同的对象, 较大了提高内存的占用。假设我们有如下的代码class Person{ constructor(){ this.name = '张三' } } let one = new Person(), two = new Person(); console.log(on
zain
不会前端的测试不是好开发