In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "what are weak references and strong references in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What are weak references and strong references?
Strong reference in JavaScript: a reference to an object is a strong reference in JavaScript, that is, when a reference object is saved as a variable or constant, then the variable or constant is a strong reference and the object is not recycled.
Weak references in JavaScript: WeakMaps and WeakSets are the only ways we use weak references in JavaScript, and adding an object as a key to WeakMap or WeakSet does not prevent these objects from being recycled.
Give examples of weak references and strong references
A strong quote is a child A holding a dog, and they are connected by a dog chain.
A weak quote is that a man B points to the dog led by An and says, "Hey, there's a dog, and B points to that dog, but there's nothing tied together between them."
When A lets go of the leash, the dog will run away (recycled), regardless of whether B is still pointing.
However, when B is no longer pointing at the dog, the dog is still held by A, which will not affect whether it runs away.
Demonstrate weak and strong references in JavaScript let people = {name:' Zhang San', age:25} let people1 = people
When we assign {name:' Zhang San', age:25} to the variable people in the above code, there is a line in memory that connects them:
Then create the people1 variable and assign people to people1, which means that people1 also refers to this object:
Let's take a look at what happens when we use ES6's newly introduced types WeakSet and WeakMap to store reference values.
Let people = {name:' Zhang San', age:25} let people1 = people;let set = new WeakSet (); set.add (people)
We have created a new instance of WeakSet (), and the reference value corresponding to people,people is {name:' Zhang San', age:25} through the add method.
You can see that the value {name:' Zhang San', age:25} reference in the set instance points to {name:' Zhang San', age:25} (in real memory he points to a pointer reference to the stack of the data, which points to the value of the corresponding address in the corresponding heap). And it is important to note that this weakly referenced "line" is transparent. What does this mean? What's the difference between him and strong quotation?
Summary in a word: the strong reference is recognized as a "connection" by {name:' Zhang San', age:25}, while the weak reference is not recognized. That is, the reference does not know that it is referenced by the set instance.
This means that garbage collection does not know that the reference is referenced by the set instance. So if all strong reference connections for the reference are disconnected (variables are assigned to null or otherwise), the reference will be destroyed as garbage, even if the set instance is still referencing the reference.
Let people = {name:' Zhang San', age:25} let people1 = people;let set = new WeakSet (); set.add (people); people = null;people1 = null
What happens if we break all the strong references?
Because all the strong references are broken, garbage collection thinks that the reference {name:' Zhang San', age:25} is not needed, so it will be destroyed. Then the reference used by the corresponding set instance no longer exists, even if the set instance is still using the reference.
That's all for "what are weak references and strong references in JavaScript?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.