search
HomeWeb Front-endJS TutorialLoose 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)