JS API General Usage Methods
ElenixOS provides rich JavaScript APIs for developing applications and watch faces. This document will introduce the general usage methods of JS APIs, including basic concepts, naming conventions, error handling, and other content.
Basic Concepts
API Namespaces
ElenixOS's JavaScript APIs are exposed to scripts through the following namespaces:
eos- System APIlv- LVGL UI API
Object-Oriented Style
ElenixOS's JavaScript APIs adopt an object-oriented style, using constructors to create objects and instance methods to manipulate objects:
// Create a button
const button = new lv.button(eos.view.active());
// Set button size and position
button.setSize(100, 50);
button.setPos(10, 10);
// Add label
const label = new lv.label(button);
label.setText("Click Me");
Event Handling
ElenixOS's JavaScript APIs use callback functions to handle events:
// Bind click event
button.addEventCb((e) => {
eos.console.log("Button clicked!");
}, lv.EVENT_CLICKED, null);
Naming Conventions
Constant Naming
Constants use all uppercase letters, with underscores between words:
// Event types
lv.EVENT_CLICKED
lv.EVENT_PRESSED
// Object flags
lv.OBJ_FLAG_CLICKABLE
lv.OBJ_FLAG_CHECKABLE
// Alignment methods
lv.ALIGN_CENTER
lv.ALIGN_TOP_LEFT
Method Naming
Methods use camel case naming, with the first letter lowercase:
// Set methods
button.setSize(width, height);
button.setPos(x, y);
// Get methods
const width = button.getWidth();
const height = button.getHeight();
Constructor Naming
Constructors use camel case naming, with the first letter lowercase:
// Create objects
const button = new lv.button(parent);
const label = new lv.label(parent);
Error Handling
Exception Capture
Use try-catch to catch possible errors:
try {
// Code that may throw errors
const button = new lv.button(null);
} catch (e) {
// Handle error
eos.console.error("Error creating button:", e);
}
Error Types
ElenixOS's JavaScript APIs may throw the following types of errors:
- TypeError - Parameter type error
- ReferenceError - Reference to non-existent object
- RangeError - Parameter value out of range
Memory Management
Object Lifecycle
In ElenixOS, the lifecycle of objects is managed by the garbage collector. When an object is no longer referenced, the garbage collector will automatically reclaim its memory.
Manual Release
For some resource-intensive objects, you can manually release them to save memory:
// Create object
const canvas = new lv.canvas(parent);
// Use object
// ...
// Manually release object
canvas.delete();
Asynchronous Operations
Timers
Use setTimeout and setInterval to execute asynchronous operations:
// Delayed execution
setTimeout(() => {
eos.console.log("Delayed execution");
}, 1000);
// Periodic execution
const intervalId = setInterval(() => {
eos.console.log("Periodic execution");
}, 1000);
// Cancel periodic execution
clearInterval(intervalId);
Callback Functions
Use callback functions to handle the results of asynchronous operations:
// Read file
fs.readFile("/path/to/file", (err, data) => {
if (err) {
eos.console.error("Error reading file:", err);
return;
}
eos.console.log("File content:", data);
});
Debugging Techniques
Log Output
Use eos.console.log to output log information:
eos.console.log("Debug information");
eos.console.error("Error information");
eos.console.warn("Warning information");
Breakpoint Debugging
In the development environment, you can use breakpoint debugging to locate problems:
// Add breakpoint where debugging is needed
debugger;
// Execute code
const result = someFunction();
eos.console.log("Result:", result);
Best Practices
- Code organization: Organize code in a modular way for easy maintenance
- Error handling: Use try-catch to catch possible errors
- Memory management: Release objects that are no longer used in a timely manner
- Performance optimization: Avoid complex calculations during animation
- Code style: Follow consistent code style to improve readability
Summary
ElenixOS's JavaScript APIs adopt an object-oriented style, using constructors to create objects and instance methods to manipulate objects. By understanding and using these APIs, developers can create feature-rich, high-performance applications.
This document introduces the general usage methods of JS APIs, including basic concepts, naming conventions, error handling, and other content. Developers should follow these best practices to ensure code quality and maintainability.