本文同步发表于我的微信公众号,微信搜索 程语新视界 即可关注,每个工作日都有文章更新
ArkTS作为鸿蒙应用开发的主要语言,基于TypeScript并扩展了鸿蒙特有的能力。以下是ArkTS中常用的内置对象分类说明:
一、JavaScript标准内置对象
1. 基础对象
- Object:所有对象的基类
const obj = new Object();
obj.name = 'ArkTS';
- Function:函数对象
function greet() { console.log('Hello'); }
- Boolean:布尔对象
const bool = new Boolean(true);
- Symbol:唯一值对象
const sym = Symbol('unique');
2. 数字和日期
- Number:数字对象
const num = new Number(10);
- Math:数学对象
Math.PI; // 3.141592653589793
Math.random();
- Date:日期对象
const now = new Date();
now.getFullYear();
3. 文本处理
- String:字符串对象
const str = new String('ArkTS');
'hello'.toUpperCase();
- RegExp:正则表达式
const regex = new RegExp('ab+c');
4. 集合对象
- Array:数组对象
const arr = new Array(1, 2, 3);
[].map(item => item * 2);
- Map:键值对集合
const map = new Map();
map.set('key', 'value');
- Set:唯一值集合
const set = new Set([1, 2, 3]);
- WeakMap/WeakSet:弱引用集合
二、ArkTS特有对象
1. UI相关对象
- 组件对象:通过
@Component
装饰的组件
@Component
struct MyComponent {
build() {
Text('Hello')
}
}
- 状态管理对象:
@State count: number = 0;
@Link isSelected: boolean;
2. 资源管理
- 资源引用对象:
$r
Image($r('app.media.icon'))
3. 系统能力对象
- router:页面路由
import router from '@ohos.router';
router.pushUrl({ url: 'pages/NextPage' });
- window:窗口管理
import window from '@ohos.window';
window.getLastWindow(this.context);
- app:应用管理
import app from '@system.app';
app.getInfo();
三、鸿蒙扩展对象
1. 设备能力
- geolocation:地理位置
import geolocation from '@ohos.geolocation';
geolocation.getCurrentLocation();
- sensor:传感器
import sensor from '@ohos.sensor';
sensor.on('accelerometer');
2. 媒体能力
- media:多媒体
import media from '@ohos.multimedia.media';
const player = media.createAudioPlayer();
- image:图片处理
import image from '@ohos.multimedia.image';
const pixelMap = image.createPixelMap();
3. 数据管理
- dataStorage:数据存储
import dataStorage from '@ohos.data.storage';
const storage = dataStorage.getStorage(this.context);
- database:数据库
import relationalStore from '@ohos.data.relationalStore';
const store = relationalStore.getRdbStore(this.context);
四、异步处理对象
1. Promise
new Promise((resolve, reject) => {
setTimeout(() => resolve('done'), 1000);
}).then(result => {});
2. Async/Await
async function fetchData() {
const data = await someAsyncOperation();
}
3. Worker
import worker from '@ohos.worker';
const workerInstance = new worker.ThreadWorker('workers/worker.ts');
五、网络相关对象
1. http
import http from '@ohos.net.http';
const httpRequest = http.createHttp();
2. websocket
import webSocket from '@ohos.net.webSocket';
const ws = new webSocket.WebSocket('ws://example.com');
3. socket
import socket from '@ohos.net.socket';
const tcpSocket = socket.constructTCPSocketInstance();
六、安全相关对象
1. crypto
import crypto from '@ohos.security.crypto';
const cipher = crypto.createCipher('AES128');
2. systemParameter
import systemParameter from '@ohos.systemParameter';
systemParameter.getSync('persist.sys.timezone');
七、文件管理对象
1. fileio
import fileio from '@ohos.fileio';
fileio.openSync(path);
2. file
import file from '@ohos.file';
const dir = file.Dir(opendirPath);
八、实用工具对象
1. util
import util from '@ohos.util';
const textEncoder = new util.TextEncoder();
2. buffer
import buffer from '@ohos.buffer';
const buf = buffer.from('hello');
九、国际化对象
1. i18n
import i18n from '@ohos.i18n';
const calendar = i18n.getCalendar('zh-CN');
十、错误处理对象
1. Error
try {
// ...
} catch (err) {
console.error(err.message);
}
2. BusinessError
import { BusinessError } from '@ohos.base';
throw new BusinessError('自定义业务错误');
ArkTS继承了JavaScript/TypeScript的所有标准内置对象,同时扩展了大量鸿蒙特有的系统能力对象。可以结合这些内置对象和鸿蒙的UI组件,构建功能丰富的应用程序。在实际开发中,应根据具体需求选择合适的对象和API。