current position:Home>Singleton mode - front end design mode
Singleton mode - front end design mode
2022-04-29 04:52:27【You're far from it.】
First, simply understand the word single example
single : It's a single
example : Is the instance , Since it is an example, it must be the same as new About the process of
stay new We all generate a Empty objects , Open up a space , If a constructor is new Many times
Is that to open up multiple spaces , Generate multiple objects .
Singleton mode is the solution to this problem , Only one object is generated to solve this problem
The following code reflects every time new Will generate different instances , Different objects
function Fn() {
}
let a1 = new Fn()
let a2 = new Fn()
console.log(a1 == a2); // fasle
What we want to achieve is How to let a1 == a2 yes true ?
Is there a possibility of a second new When It's the same space as the one used at the same time !
Let's change the code
Analyze why true
a1 for the first time new When _instance = null
this Point to the yes a1
Overall _instance = this
The second time new When because _instance It's not equal to null Straight back this It means pointing. a1 Of this
therefore yes true
let _instance = null
function Fn() {
if(!_instance) {
_instance = this
}
return _instance
}
let a1 = new Fn()
let a2 = new Fn()
console.log(a1 == a2); // true
Finally, optimize the code
Closures are used to prevent contamination of global variables
This Of self executing functions window It can not be passed , If it doesn't pass , Will destroy performance
because I'll find it layer by layer window.Fn
(function(window) {
let _instance = null
function Fn() {
if(!_instance) {
_instance = this
}
return _instance
}
window.Fn = Fn
})(window)
let g1 = new Fn()
let g2 = new Fn()
console.log(g1 == g2); // true
copyright notice
author[You're far from it.],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261129512725.html
The sidebar is recommended
- Units that can be used by the rotate method of transform in CSS
- Why CSS3 sticky doesn't work?
- [bug resolution] webpack error: module not found Did you mean xxx
- Vue insert iframe
- About database data response to HTML page, Chinese appears? Solution to garbled code (multiple question marks)
- Ask a question about CSS selector
- When Vue jumps the route, the timer cannot be cleared in time
- Document is not defined
- Vuepress packaging deployment stepping on the road of the pit
- Vue quill editor stepping on pit record -- the echo style of rich text content is wrong
guess what you like
The multi selection box of table in element is combined with paging to echo the bug step hole
Vue project optimizes the loading speed of the first screen
Use / deep / to report an error CSS (CSS selected expected)
Nginx configuration
Vue cli reverse proxy
Vuex persistedstate plug-in - vuex persistent storage
Automatic import of less
Understanding and summary of CSS
vue2. Implementation of X data response
JavaScript content understanding
Random recommended
- Props in react
- JavaScript stack“
- Bootstrap blazor table component (III) intelligent generation
- Based on centos7 + nginx + Daphne + uwsgi + django3 2 + supervisor + mysql8 single architecture server production environment deployment (I)
- Differences between vue2 and vue3 named slots
- Vue3: Axios cross domain request problem
- The difference between vue2 and vue3: keep alive
- Configure nginx and SSL certificate for Django project under Windows Environment
- Ant Design Vue: a-table custom column
- Using jQuery in Vue
- Vue dynamic loading picture problem
- Icons using Alibaba vector icon library in Vue
- Java Android mobile phone automatic basic learning element positioning
- Rancher configuring HTTPS domain name to access graphic tutorial
- Building a blog with GitHub pages + hexo (7) how to delete a published article successfully solved: delete it at the same time deploy_ Git folder
- Eight blog views / hexubs
- Build a blog with GitHub pages + hexo (9) set the next theme of hexo blog, and only part of the home page is displayed (not the full text)
- Building a blog with GitHub pages + hexo (10) the next theme of hexo blog mathjax mathematical formula rendering problem
- Hexo/Github. IO configure Tencent cloud CDN
- Rich text editor: ckeditor (using ckeditor4 Vue)
- The get request of nginx agent only returns part of the data. The problem is solved
- JavaScript traverses the irregularly nested multi-layer objects, returns the map with "index", and realizes the rapid positioning of sub attributes
- HTTP keep alive details
- [technical update] http / 3 quic Foundation
- Vue to react ----- can the constructor be omitted when using ES6?
- Use of nested HTML
- Vue to react to realize slot function
- When to use react PureComponent
- Details of Vue to react useeffect
- React 16.6 memo()
- Deep understanding of children https://segmentfault.com/a/1190000011527160
- This paper solves the cross domain problem Vue + springboot caused by the separation of front and back ends
- The difference between shallow copy and deep copy is to use native JavaScript to realize deep copy
- Definition of Vue slot
- Sorting algorithm in JavaScript
- JavaScript implements search algorithm, sequential search and binary search
- leetcode20. Valid parentheses, implemented using JavaScript
- 496 next element larger JavaScript implementation leetcode
- Source code analysis, Vue What happens when using (), take initializing vuex as an example
- Vue bidirectional binding principle