Loose coupling of UI layer in web development
This time I will bring you the loose coupling of the UI layer in web development. What are the precautions when using loose coupling of the UI layer in web development? Here are practical cases, let's take a look.
In web development, the UI is defined by three layers that are isolated and interact with each other.
HTML is used to define the data and semantics of the page
CSS is used to add styles to the page and create visual features
JS is used to add behavior to the page to make it more Interactivity
Allow me to say a few words about loose coupling. When you can modify one component without changing other components, you have loose coupling. For large multi-person systems, where many people are involved in maintaining the code, loose coupling is critical to code maintainability. You absolutely want developers to not break other people's code when they modify one part of the code. When the content of each component of a large system is limited, loose coupling is achieved. Essentially, each component needs to be kept thin enough to ensure loose coupling. The less known about the components, the better it is to form the overall system.
One thing to note: components working together cannot achieve "no coupling". In all systems, components share some information to do their jobs. It's easy to understand that our goal is to ensure that changes to one component do not regularly affect other parts.
If a Web UI is loosely coupled, it is easy to debug. Problems related to text or structure can be located by searching HTML. When a style-related problem occurs, you know the problem is in the CSS. Finally, for those behavioral issues, your ability to go directly to JS to find the problem is a core part of the maintainability of web interfaces.
In the WebPage era, we advocate the three-layer separation of HTML/CSS/JS. For example, it is forbidden to use the inline attribute of the DOM to bind the listener,
You can’t help but wonder, are we going backwards?
History sometimes goes round and round. At first glance, I thought I was going back. In fact, the spiral has turned around and stood at a new starting point. ——Yu Bo "Evolution of Web R&D Model"
In the traditional WebPage era, the level of component support is not high, both at the language level and the framework level. Think about it, there is no JS that does not natively support modules (before ES6) era) and jQuery, so in order to avoid increasing maintenance costs, the best practice of three-layer separation is recommended. With the rise of ES6 and the front-end MV* framework, the entire front-end development model has changed. You will find that the front-end is not only writing pages, but also writing more WebApps. The scale and complexity of the applications are different from those in the WebPage era.
React is a very typical representative. It proposes to use JSX to write HTML, directly writing the page structure and page logic together. If this was placed in the WebPage era, I believe it would be directly regarded as a typical anti-pattern teaching material; but in the WebApp era, it is accepted and used by most people. Including CSS in JS proposed by the React team, they also want to write CSS in JS so that front-end development is completely dominated by JS and componentization is more thorough (I have not done in-depth research and understanding of CSS in JS, and there is no actual large-scale Practical experience in the project, so now I still maintain a wait-and-see attitude and continue to use the previous SASS and LESS for CSS development).
Although the development models of the two Web eras have undergone tremendous changes, there are still some general principles you need to follow regarding the three-layer loose coupling design:
Extract JS from CSS. Early IE8 and earlier browsers allowed writing JS in CSS (without writing examples, this is an anti-pattern, it’s better if you can’t remember it). This will cause performance problems, and what’s even more frightening is that it will be difficult to maintain in the future. . But I believe that none of you here will have access to this kind of code, which is fine.
Extract CSS from JS. It's not that you can't modify CSS in JS. It doesn't mean that you are not allowed to change the style directly. Instead, you can modify the style indirectly by modifying the class. See the following example:
// 不好的写法element.style.color = 'red'; element.style.left = '10px'; element.style.top = '100px'; element.style.visibility = 'visible';// 好的写法.reveal { color: red; left: 10px; top: 100px; visibility: visible; } element.classList.add('.reveal');
Because the CSS className can become a communication bridge between CSS and JS. During the lifecycle of the page, JS can add and delete the className of the element at will. The style defined by className is in the CSS code. At any time, styles in CSS can be modified without having to update JS. JS should not manipulate styles directly in order to maintain loose coupling with CSS.
There is one situation where using the style attribute is acceptable: when you need to reposition an element on the page relative to another element or to the entire page. This calculation cannot be done in CSS, so you can use style.top, style.left, style.bottom and style.rght to correctly position the element. Define the default attributes of this element in CSS, and modify these default values in Javascript.
In view of the current situation that HTML and JS have been written together on the front end, I will not talk about the practice of separating the two in the original book. But, having said all this nonsense, remember this: Predictability leads to faster testing and development, and knowing (rather than guessing) where to start debugging bugs will make the problem Solve faster and the overall quality of your code is higher.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use JS to pass by reference and pass by value
How to make a node.js interface
The above is the detailed content of Loose coupling of UI layer in web development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Summary of the five most popular Go language libraries: essential tools for development, requiring specific code examples. Since its birth, the Go language has received widespread attention and application. As an emerging efficient and concise programming language, Go's rapid development is inseparable from the support of rich open source libraries. This article will introduce the five most popular Go language libraries. These libraries play a vital role in Go development and provide developers with powerful functions and a convenient development experience. At the same time, in order to better understand the uses and functions of these libraries, we will explain them with specific code examples.

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Android development is a busy and exciting job, and choosing a suitable Linux distribution for development is particularly important. Among the many Linux distributions, which one is most suitable for Android development? This article will explore this issue from several aspects and give specific code examples. First, let’s take a look at several currently popular Linux distributions: Ubuntu, Fedora, Debian, CentOS, etc. They all have their own advantages and characteristics.

"Understanding VSCode: What is this tool used for?" 》As a programmer, whether you are a beginner or an experienced developer, you cannot do without the use of code editing tools. Among many editing tools, Visual Studio Code (VSCode for short) is very popular among developers as an open source, lightweight, and powerful code editor. So, what exactly is VSCode used for? This article will delve into the functions and uses of VSCode and provide specific code examples to help readers

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:
