Utilities and Class System
This page covers Leaflet's core utility functions, class inheritance system, and foundational patterns used throughout the library. These components provide the basic building blocks that enable Leaflet's modular architecture and consistent programming patterns.
For detailed DOM event handling and interaction utilities, see DOM Utilities and Event Handling. For the main event system used by Leaflet classes, see Events System.
Core Utility Functions
Leaflet provides a comprehensive set of utility functions in the Util namespace that handle common programming tasks like object identification, string formatting, and function manipulation.
Object Identification and Management
The stamp function provides unique identification for objects, which is essential for Leaflet's internal tracking systems:
The setOptions function handles the merging of configuration objects, creating proper prototype chains to support option inheritance:
String and Number Processing
The utility functions provide consistent formatting and string manipulation:
| Function | Purpose | Example Usage |
|---|---|---|
formatNum | Number precision control | Coordinate rounding |
splitWords | String tokenization | CSS class parsing |
template | String interpolation | URL template processing |
wrapNum | Numeric range wrapping | Longitude normalization |
The template function uses a regex-based approach to replace placeholders with data values, supporting both static values and function callbacks.
Function Control Utilities
The throttle function implements rate limiting for performance-critical operations:
Class Inheritance System
Leaflet's Class provides a foundation for object-oriented programming with support for inheritance, mixins, and configuration management.
Base Class Structure
The Class constructor automatically calls initialize() and processes initialization hooks, providing a consistent instantiation pattern across all Leaflet classes.
Inheritance and Extension
The extend method creates new classes with prototype chain inheritance:
The system supports both the legacy extend() method and modern ES6 class syntax, with static methods available on all classes for configuration and extension.
Options Management
Options handling uses prototypal inheritance to enable efficient defaults and overrides:
Mixin System
The include method enables multiple inheritance through mixins:
Browser Detection System
The Browser namespace provides feature detection for cross-platform compatibility:
The browser detection focuses on capabilities rather than specific browser versions, enabling progressive enhancement based on available features.
DOM Utilities Overview
Leaflet includes essential DOM manipulation utilities, with more comprehensive coverage in the interaction system:
Core DOM Operations
| Function | Purpose | Usage Pattern |
|---|---|---|
get | Element retrieval | ID or element normalization |
create | Element creation | Layer DOM construction |
setPosition | Position management | Layer positioning |
setTransform | CSS transforms | Hardware acceleration |
Element Manipulation
The DOM utilities use modern CSS transforms and maintain position caches for performance optimization.
Foundational Patterns
These utilities establish consistent patterns used throughout Leaflet:
Object Lifecycle Pattern
Event Integration Pattern
Classes that need event capabilities inherit from Evented and use utility functions for DOM interaction:
Configuration Management Pattern
The options system enables hierarchical configuration with proper inheritance: