JavaScript closures and memory management are issues frequently encountered in day-to-day development. This article draws from real-world projects to share concrete implementation approaches and practical takeaways.
Quick Start
Here is a basic example:
javascript
export default {
directives: {
focus: {
inserted(el) {
el.focus();
},
},
loading: {
bind(el, binding) {
if (binding.value) {
el.classList.add("loading");
}
},
update(el, binding) {
el.classList.toggle("loading", binding.value);
},
},
},
};
This pattern is concise and well-suited for most scenarios.
Advanced Usage
The core implementation is as follows:
javascript
import React, { Component } from "react";
class DataList extends Component {
state = { items: [], loading: true };
async componentDidMount() {
const res = await fetch("/api/items");
const items = await res.json();
this.setState({ items, loading: false });
}
render() {
const { items, loading } = this.state;
if (loading) return <div>Loading...</div>;
return (
<ul>
{items.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
}
In real projects, edge cases and error handling also need careful consideration.
Real-World Scenario
Here is a practical example:
javascript
import React, { Component } from "react";
class Form extends Component {
state = { name: "", email: "", errors: {} };
handleChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
};
handleSubmit = (e) => {
e.preventDefault();
const errors = this.validate();
if (Object.keys(errors).length === 0) {
this.props.onSubmit(this.state);
} else {
this.setState({ errors });
}
};
validate() {
const errors = {};
if (!this.state.name) errors.name = "Required";
if (!this.state.email.includes("@")) errors.email = "Invalid format";
return errors;
}
}
Understanding closures is key to avoiding memory leaks — always clean up event listeners and timers in component lifecycle hooks.