In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the reason why JavaScript should be encapsulated". The editor shows you the operation process through an actual case, the operation method is simple and fast, and it is practical. I hope this article "what is the reason why JavaScript should be encapsulated" can help you solve the problem.
Need to encapsulate the reasons: 1, through encapsulation can reduce the redundancy of the code, so that the code does not look complex, reducing unnecessary code; 2, objects only need to provide the necessary interface to interact with other objects, internal data can be provided with different levels of protection to prevent unrelated parts of the program from accidentally changing or misusing the private part of the object, which can improve security.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
Why should JavaScript be encapsulated?
Encapsulation is one of the three basic characteristics of object-orientation. things in the real world are abstracted into objects in the computer field, and objects have both properties and behaviors (methods). This abstraction is encapsulation.
In the daily development process, code with the same processing logic is usually encapsulated by functions to reduce the redundancy of the code and make the code look more elegant and beautiful.
When multiple entity objects have some of the same properties and behaviors (methods), it is natural to abstract these same attributes and behaviors to reduce unnecessary code.
An important feature of encapsulation: data hiding.
Objects only provide the necessary interfaces to interact with other objects, while hiding some of their own properties and implementation details to the outside. In this way, objects provide different levels of protection to internal data to prevent unrelated parts of the program from accidentally changing or mistakenly using the private part of the object. In this way, on the premise of ensuring normal interaction, security is ensured, and the object can be used without caring about the methods implemented by the object.
Function (function)-the simplest encapsulation
Function is a core concept for any language. Any number of statements can be encapsulated through the function, and can be called and executed anywhere and at any time.
How to encapsulate: write fragmented statements into the curly braces of the function, become the body of the function, and then you can call.
Unencapsulated code:
Var body = document.getElementsByTagName ("body") [0]; var H2 = document.createElement ("H2"); body.style.backgroundColor = "green"; h2.innerText = "green"; body.appendChild (H2)
Disadvantages:
Easily overridden by variables with the same name-because variables declared in the global scope are easily overridden by variables with the same name
Execute immediately-the parser reads it here and executes it immediately
Encapsulation code:
Function makeGreen () {var body = document.getElementsByTagName ("body") [0]; var H2 = document.createElement ("H2"); body.style.backgroundColor = "green"; h2.innerText = "Green"; body.appendChild (H2);}
Advantages:
Global variables are avoided-because there is a function scope (the focus of the function scope will be examined later)
Execute on demand-the parser reads here, the function is not executed, you can only call this function when you need it
Improve code reusability
This is the end of the content about "what is the reason for JavaScript to be encapsulated". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.