1# @ohos.util (util工具函数) 2 3该模块主要提供常用的工具函数,实现字符串编解码(TextEncoder,TextDecoder)、有理数运算(RationalNumber)、缓冲区管理(LruBuffer)、范围判断(Scope)、Base64编解码(Base64)、内置对象类型检查(Types)等功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```js 13import util from '@ohos.util'; 14``` 15 16## util.format<sup>9+</sup> 17 18format(format: string, ...args: Object[]): string 19 20通过式样化字符串对输入的内容按特定格式输出。 21 22**系统能力:** SystemCapability.Utils.Lang 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| ------- | -------- | ---- | -------------- | 28| format | string | 是 | 式样化字符串。 | 29| ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 | 30 31**返回值:** 32 33| 类型 | 说明 | 34| ------ | ---------------------------- | 35| string | 按特定格式式样化后的字符串。 | 36 37**示例:** 38 39 ```js 40let res = util.format("%s", "hello world!"); 41console.log(res); 42 ``` 43 44## util.errnoToString<sup>9+</sup> 45 46errnoToString(errno: number): string 47 48获取系统错误码对应的详细信息。 49 50**系统能力:** SystemCapability.Utils.Lang 51 52**参数:** 53 54| 参数名 | 类型 | 必填 | 说明 | 55| ------ | ------ | ---- | -------------------------- | 56| errno | number | 是 | 系统发生错误产生的错误码。 | 57 58**返回值:** 59 60| 类型 | 说明 | 61| ------ | ---------------------- | 62| string | 错误码对应的详细信息。 | 63 64**示例:** 65 66```js 67let errnum = -1; // -1 : a system error number 68let result = util.errnoToString(errnum); 69console.log("result = " + result); 70``` 71 72**部分错误码及信息示例:** 73 74| 错误码 | 信息 | 75| ------ | -------------------------------- | 76| -1 | operation not permitted | 77| -2 | no such file or directory | 78| -3 | no such process | 79| -4 | interrupted system call | 80| -5 | i/o error | 81| -11 | resource temporarily unavailable | 82| -12 | not enough memory | 83| -13 | permission denied | 84| -100 | network is down | 85 86## util.callbackWrapper 87 88callbackWrapper(original: Function): (err: Object, value: Object )=>void 89 90对异步函数进行回调化处理,回调中第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 91 92**系统能力:** SystemCapability.Utils.Lang 93 94**参数:** 95 96| 参数名 | 类型 | 必填 | 说明 | 97| -------- | -------- | -------- | -------- | 98| original | Function | 是 | 异步函数。 | 99 100**返回值:** 101 102| 类型 | 说明 | 103| -------- | -------- | 104| Function | 返回一个第一个参数是拒绝原因(如果 Promise 已解决,则为 null),第二个参数是已解决的回调函数。 | 105 106**示例:** 107 108 ```js 109async function fn() { 110 return 'hello world'; 111} 112let cb = util.callbackWrapper(fn); 113cb(1, (err, ret) => { 114 if (err) throw err; 115 console.log(ret); 116}); 117 ``` 118 119## util.promisify<sup>9+</sup> 120 121promisify(original: (err: Object, value: Object) => void): Function 122 123对异步函数处理并返回一个promise的函数。 124 125**系统能力:** SystemCapability.Utils.Lang 126 127**参数:** 128 129| 参数名 | 类型 | 必填 | 说明 | 130| -------- | -------- | -------- | -------- | 131| original | Function | 是 | 异步函数。 | 132 133**返回值:** 134 135| 类型 | 说明 | 136| -------- | -------- | 137| Function | 采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的函数。 | 138 139**示例:** 140 141 ```js 142function fun(num, callback) { 143 if (typeof num === 'number') { 144 callback(null, num + 3); 145 } else { 146 callback("type err"); 147 } 148} 149 150const addCall = util.promisify(fun); 151(async () => { 152 try { 153 let res = await addCall(2); 154 console.log(res); 155 } catch (err) { 156 console.log(err); 157 } 158})(); 159 ``` 160 161## util.generateRandomUUID<sup>9+</sup> 162 163generateRandomUUID(entropyCache?: boolean): string 164 165使用加密安全随机数生成器生成随机的RFC 4122版本4的string类型UUID。 166 167**系统能力:** SystemCapability.Utils.Lang 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| -------- | -------- | -------- | -------- | 173| entropyCache | boolean | 否 | 是否使用已缓存的UUID, 默认true。 | 174 175**返回值:** 176 177| 类型 | 说明 | 178| -------- | -------- | 179| string | 表示此UUID的字符串。 | 180 181**示例:** 182 183 ```js 184 let uuid = util.generateRandomUUID(true); 185 console.log("RFC 4122 Version 4 UUID:" + uuid); 186 // 输出: 187 // RFC 4122 Version 4 UUID:88368f2a-d5db-47d8-a05f-534fab0a0045 188 ``` 189 190## util.generateRandomBinaryUUID<sup>9+</sup> 191 192generateRandomBinaryUUID(entropyCache?: boolean): Uint8Array 193 194使用加密安全随机数生成器生成随机的RFC 4122版本4的Uint8Array类型UUID。 195 196**系统能力:** SystemCapability.Utils.Lang 197 198**参数:** 199 200| 参数名 | 类型 | 必填 | 说明 | 201| -------- | -------- | -------- | -------- | 202| entropyCache | boolean | 否 | 是否使用已缓存的UUID, 默认true。 | 203 204**返回值:** 205 206| 类型 | 说明 | 207| -------- | -------- | 208| Uint8Array | 表示此UUID的Uint8Array值。 | 209 210**示例:** 211 212 ```js 213 let uuid = util.generateRandomBinaryUUID(true); 214 console.log(JSON.stringify(uuid)); 215 // 输出: 216 // 138,188,43,243,62,254,70,119,130,20,235,222,199,164,140,150 217 ``` 218 219## util.parseUUID<sup>9+</sup> 220 221parseUUID(uuid: string): Uint8Array 222 223将generateRandomUUID生成的string类型UUID转换为generateRandomBinaryUUID生成的Uint8Array类型UUID,如RFC 4122版本4中所述。 224 225**系统能力:** SystemCapability.Utils.Lang 226 227**参数:** 228 229| 参数名 | 类型 | 必填 | 说明 | 230| -------- | -------- | -------- | -------- | 231| uuid | string | 是 | UUID字符串。 | 232 233**返回值:** 234 235| 类型 | 说明 | 236| -------- | -------- | 237| Uint8Array | 返回表示此UUID的Uint8Array,如果解析失败,则抛出SyntaxError。 | 238 239**示例:** 240 241 ```js 242 let uuid = util.parseUUID("84bdf796-66cc-4655-9b89-d6218d100f9c"); 243 console.log(JSON.stringify(uuid)); 244 // 输出: 245 // 132,189,247,150,102,204,70,85,155,137,214,33,141,16,15,156 246 ``` 247 248## util.printf<sup>(deprecated)</sup> 249 250printf(format: string, ...args: Object[]): string 251 252通过式样化字符串对输入的内容按特定格式输出。 253 254> **说明:** 255> 256> 从API version 7开始支持,从API version 9开始废弃,建议使用[util.format<sup>9+</sup>](#utilformat9)替代。 257 258**系统能力:** SystemCapability.Utils.Lang 259 260**参数:** 261 262| 参数名 | 类型 | 必填 | 说明 | 263| -------- | -------- | -------- | -------- | 264| format | string | 是 | 式样化字符串。 | 265| ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 | 266 267**返回值:** 268 269| 类型 | 说明 | 270| -------- | -------- | 271| string | 按特定格式式样化后的字符串。 | 272 273**示例:** 274 275 ```js 276 let res = util.printf("%s", "hello world!"); 277 console.log(res); 278 ``` 279 280 281## util.getErrorString<sup>(deprecated)</sup> 282 283getErrorString(errno: number): string 284 285获取系统错误码对应的详细信息。 286 287> **说明:** 288> 289> 从API version 7开始支持,从API version 9开始废弃,建议使用[util.errnoToString<sup>9+</sup>](#utilerrnotostring9)替代。 290 291**系统能力:** SystemCapability.Utils.Lang 292 293**参数:** 294 295| 参数名 | 类型 | 必填 | 说明 | 296| -------- | -------- | -------- | -------- | 297| errno | number | 是 | 系统发生错误产生的错误码。 | 298 299**返回值:** 300 301| 类型 | 说明 | 302| -------- | -------- | 303| string | 错误码对应的详细信息。 | 304 305**示例:** 306 307 ```js 308 let errnum = -1; // -1 : a system error number 309 let result = util.getErrorString(errnum); 310 console.log("result = " + result); 311 ``` 312 313## util.promiseWrapper<sup>(deprecated)</sup> 314 315promiseWrapper(original: (err: Object, value: Object) => void): Object 316 317对异步函数处理并返回一个promise的版本。 318 319> **说明:** 320> 321> 此接口不可用,建议使用[util.promisify<sup>9+</sup>](#utilpromisify9)替代。 322 323**系统能力:** SystemCapability.Utils.Lang 324 325**参数:** 326 327| 参数名 | 类型 | 必填 | 说明 | 328| -------- | -------- | -------- | -------- | 329| original | Function | 是 | 异步函数。 | 330 331**返回值:** 332 333| 类型 | 说明 | 334| -------- | -------- | 335| Function | 采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 | 336 337 338## TextDecoder 339 340### 属性 341 342**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 343 344| 名称 | 类型 | 可读 | 可写 | 说明 | 345| -------- | -------- | -------- | -------- | -------- | 346| encoding | string | 是 | 否 | 编码格式。<br/>- 支持格式:utf-8、ibm866、iso-8859-2、iso-8859-3、iso-8859-4、iso-8859-5、iso-8859-6、iso-8859-7、iso-8859-8、iso-8859-8-i、iso-8859-10、iso-8859-13、iso-8859-14、iso-8859-15、koi8-r、koi8-u、macintosh、windows-874、windows-1250、windows-1251、windows-1252、windows-1253、windows-1254、windows-1255、windows-1256、windows-1257、windows-1258、x-mac-cyrilli、gbk、gb18030、big5、euc-jp、iso-2022-jp、shift_jis、euc-kr、utf-16be、utf-16le。 | 347| fatal | boolean | 是 | 否 | 是否显示致命错误。 | 348| ignoreBOM | boolean | 是 | 否 | 是否忽略BOM(byte order marker)标记,默认值为false ,表示解码结果包含BOM标记。 | 349 350### constructor<sup>9+</sup> 351 352constructor() 353 354TextDecoder的构造函数。 355 356**系统能力:** SystemCapability.Utils.Lang 357 358### create<sup>9+</sup> 359 360create(encoding?: string,options?: { fatal?: boolean; ignoreBOM?: boolean }): TextDecoder; 361 362替代有参构造功能。 363 364**系统能力:** SystemCapability.Utils.Lang 365 366**参数:** 367 368| 参数名 | 类型 | 必填 | 说明 | 369| -------- | ------ | ---- | ------------------------------------------------ | 370| encoding | string | 否 | 编码格式。 | 371| options | Object | 否 | 编码相关选项参数,存在两个属性fatal和ignoreBOM。 | 372 373**表1.1**options 374 375| 名称 | 参数类型 | 必填 | 说明 | 376| --------- | -------- | ---- | ------------------ | 377| fatal | boolean | 否 | 是否显示致命错误。 | 378| ignoreBOM | boolean | 否 | 是否忽略BOM标记。 | 379 380**示例:** 381 382```js 383let result = util.TextDecoder.create('utf-8', { ignoreBOM : true }) 384let retStr = result.encoding 385``` 386 387### decodeWithStream<sup>9+</sup> 388 389decodeWithStream(input: Uint8Array, options?: { stream?: boolean }): string 390 391通过输入参数解码后输出对应文本。 392 393**系统能力:** SystemCapability.Utils.Lang 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | -------- | -------- | -------- | 399| input | Uint8Array | 是 | 符合格式需要解码的数组。 | 400| options | Object | 否 | 解码相关选项参数。 | 401 402**表2** options 403 404| 名称 | 参数类型 | 必填 | 说明 | 405| -------- | -------- | -------- | -------- | 406| stream | boolean | 否 | 在随后的decodeWithStream()调用中是否跟随附加数据块。如果以块的形式处理数据,则设置为true;如果处理最后的数据块或数据未分块,则设置为false。默认为false。 | 407 408**返回值:** 409 410| 类型 | 说明 | 411| -------- | -------- | 412| string | 解码后的数据。 | 413 414**示例:** 415 416 ```js 417 let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 418 let result = new Uint8Array(6); 419 result[0] = 0xEF; 420 result[1] = 0xBB; 421 result[2] = 0xBF; 422 result[3] = 0x61; 423 result[4] = 0x62; 424 result[5] = 0x63; 425 console.log("input num:"); 426 let retStr = textDecoder.decodeWithStream( result , {stream: false}); 427 console.log("retStr = " + retStr); 428 ``` 429 430### constructor<sup>(deprecated)</sup> 431 432constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }) 433 434TextDecoder的构造函数。 435 436> **说明:** 437> 438> 从API version 7开始支持,从API version 9开始废弃,建议使用[create<sup>9+</sup>](#create9)替代。 439 440**系统能力:** SystemCapability.Utils.Lang 441 442**参数:** 443 444| 参数名 | 类型 | 必填 | 说明 | 445| -------- | -------- | -------- | -------- | 446| encoding | string | 否 | 编码格式。 | 447| options | Object | 否 | 编码相关选项参数,存在两个属性fatal和ignoreBOM。 | 448 449 **表1** options 450 451| 名称 | 参数类型 | 必填 | 说明 | 452| -------- | -------- | -------- | -------- | 453| fatal | boolean | 否 | 是否显示致命错误。 | 454| ignoreBOM | boolean | 否 | 是否忽略BOM标记。 | 455 456**示例:** 457 458 ```js 459 let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 460 ``` 461 462### decode<sup>(deprecated)</sup> 463 464decode(input: Uint8Array, options?: { stream?: false }): string 465 466通过输入参数解码后输出对应文本。 467 468> **说明:** 469> 470> 从API version 7开始支持,从API version 9开始废弃,建议使用[decodeWithStream<sup>9+</sup>](#decodewithstream9)替代。 471 472**系统能力:** SystemCapability.Utils.Lang 473 474**参数:** 475 476| 参数名 | 类型 | 必填 | 说明 | 477| -------- | -------- | -------- | -------- | 478| input | Uint8Array | 是 | 符合格式需要解码的数组。 | 479| options | Object | 否 | 解码相关选项参数。 | 480 481**表2** options 482 483| 名称 | 参数类型 | 必填 | 说明 | 484| -------- | -------- | -------- | -------- | 485| stream | boolean | 否 | 在随后的decode()调用中是否跟随附加数据块。如果以块的形式处理数据,则设置为true;如果处理最后的数据块或数据未分块,则设置为false。默认为false。 | 486 487**返回值:** 488 489| 类型 | 说明 | 490| -------- | -------- | 491| string | 解码后的数据。 | 492 493**示例:** 494 495 ```js 496 let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 497 let result = new Uint8Array(6); 498 result[0] = 0xEF; 499 result[1] = 0xBB; 500 result[2] = 0xBF; 501 result[3] = 0x61; 502 result[4] = 0x62; 503 result[5] = 0x63; 504 console.log("input num:"); 505 let retStr = textDecoder.decode( result , {stream: false}); 506 console.log("retStr = " + retStr); 507 ``` 508 509## TextEncoder 510 511### 属性 512 513**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 514 515| 名称 | 类型 | 可读 | 可写 | 说明 | 516| -------- | -------- | -------- | -------- | -------- | 517| encoding | string | 是 | 否 | 编码格式,默认值是utf-8。 | 518 519 520### constructor 521 522constructor() 523 524TextEncoder的构造函数。 525 526**系统能力:** SystemCapability.Utils.Lang 527 528**示例:** 529 530 ```js 531 let textEncoder = new util.TextEncoder(); 532 ``` 533 534### constructor<sup>9+</sup> 535 536constructor(encoding?: string) 537 538TextEncoder的构造函数。 539 540**系统能力:** SystemCapability.Utils.Lang 541 542**参数:** 543 544| 参数名 | 类型 | 必填 | 说明 | 545| ----- | ---- | ---- | ---- | 546| encoding | string | 否 | 编码格式 | 547 548**示例:** 549 550 ```js 551 let textEncoder = new util.TextEncoder("utf-8"); 552 ``` 553 554### encodeInto<sup>9+</sup> 555 556encodeInto(input?: string): Uint8Array 557 558通过输入参数编码后输出对应文本。 559 560**系统能力:** SystemCapability.Utils.Lang 561 562**参数:** 563 564| 参数名 | 类型 | 必填 | 说明 | 565| ------ | ------ | ---- | ------------------ | 566| input | string | 否 | 需要编码的字符串。 | 567 568**返回值:** 569 570| 类型 | 说明 | 571| ---------- | ------------------ | 572| Uint8Array | 返回编码后的文本。 | 573 574**示例:** 575 576 ```js 577let textEncoder = new util.TextEncoder(); 578let buffer = new ArrayBuffer(20); 579let result = new Uint8Array(buffer); 580result = textEncoder.encodeInto("\uD800¥¥"); 581 ``` 582 583### encodeIntoUint8Array<sup>9+</sup> 584 585encodeIntoUint8Array(input: string, dest: Uint8Array, ): { read: number; written: number } 586 587放置生成的UTF-8编码文本。 588 589**系统能力:** SystemCapability.Utils.Lang 590 591**参数:** 592 593| 参数名 | 类型 | 必填 | 说明 | 594| ------ | ---------- | ---- | ------------------------------------------------------- | 595| input | string | 是 | 需要编码的字符串。 | 596| dest | Uint8Array | 是 | Uint8Array对象实例,用于将生成的UTF-8编码文本放入其中。 | 597 598**返回值:** 599 600| 类型 | 说明 | 601| ---------- | ------------------ | 602| Uint8Array | 返回编码后的文本。 | 603 604**示例:** 605 606 ```js 607let that = new util.TextEncoder() 608let buffer = new ArrayBuffer(4) 609let dest = new Uint8Array(buffer) 610let result = new Object() 611result = that.encodeIntoUint8Array('abcd', dest) 612 ``` 613 614### encodeInto<sup>(deprecated)</sup> 615 616encodeInto(input: string, dest: Uint8Array, ): { read: number; written: number } 617 618放置生成的UTF-8编码文本。 619 620> **说明:** 621> 622> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeIntoUint8Array<sup>9+</sup>](#encodeintouint8array9)替代。 623 624**系统能力:** SystemCapability.Utils.Lang 625 626**参数:** 627 628| 参数名 | 类型 | 必填 | 说明 | 629| -------- | -------- | -------- | -------- | 630| input | string | 是 | 需要编码的字符串。 | 631| dest | Uint8Array | 是 | Uint8Array对象实例,用于将生成的UTF-8编码文本放入其中。 | 632 633**返回值:** 634 635| 类型 | 说明 | 636| -------- | -------- | 637| Uint8Array | 返回编码后的文本。 | 638 639**示例:** 640 ```js 641 let that = new util.TextEncoder() 642 let buffer = new ArrayBuffer(4) 643 let dest = new Uint8Array(buffer) 644 let result = new Object() 645 result = that.encodeInto('abcd', dest) 646 ``` 647 648### encode<sup>(deprecated)</sup> 649 650encode(input?: string): Uint8Array 651 652通过输入参数编码后输出对应文本。 653 654> **说明:** 655> 656> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeInto<sup>9+</sup>](#encodeinto9)替代。 657 658**系统能力:** SystemCapability.Utils.Lang 659 660**参数:** 661 662| 参数名 | 类型 | 必填 | 说明 | 663| -------- | -------- | -------- | -------- | 664| input | string | 否 | 需要编码的字符串。 | 665 666**返回值:** 667 668| 类型 | 说明 | 669| -------- | -------- | 670| Uint8Array | 返回编码后的文本。 | 671 672**示例:** 673 ```js 674 let textEncoder = new util.TextEncoder(); 675 let buffer = new ArrayBuffer(20); 676 let result = new Uint8Array(buffer); 677 result = textEncoder.encode("\uD800¥¥"); 678 ``` 679 680## RationalNumber<sup>8+</sup> 681 682### constructor<sup>9+</sup> 683 684constructor() 685 686RationalNumber的构造函数。 687 688**系统能力:** SystemCapability.Utils.Lang 689 690**示例:** 691 692```js 693let rationalNumber = new util.RationalNumber(); 694``` 695 696### parseRationalNumber<sup>9+</sup> 697 698parseRationalNumber(numerator: number,denominator: number): RationalNumber 699 700替代原有参构造的参数处理。 701 702**系统能力:** SystemCapability.Utils.Lang 703 704**参数:** 705 706| 参数名 | 类型 | 必填 | 说明 | 707| ----------- | ------ | ---- | ---------------- | 708| numerator | number | 是 | 分子,整数类型。 | 709| denominator | number | 是 | 分母,整数类型。 | 710 711**示例:** 712 713```js 714let rationalNumber = util.RationalNumber.parseRationalNumber(1,2) 715``` 716 717### createRationalFromString<sup>8+</sup> 718 719static createRationalFromString(rationalString: string): RationalNumber 720 721基于给定的字符串创建一个RationalNumber对象。 722 723**系统能力:** SystemCapability.Utils.Lang 724 725**参数:** 726 727| 参数名 | 类型 | 必填 | 说明 | 728| -------- | -------- | -------- | -------- | 729| rationalString | string | 是 | 字符串格式。 | 730 731**返回值:** 732 733| 类型 | 说明 | 734| -------- | -------- | 735| object | 返回有理数类的对象。 | 736 737**示例:** 738 739```js 740let rationalNumber = new util.RationalNumber(1,2); 741let rational = util.RationalNumber.createRationalFromString("3/4"); 742``` 743 744### compare<sup>9+</sup> 745 746compare(another: RationalNumber): number 747 748将当前的RationalNumber对象与给定的对象进行比较。 749 750**系统能力:** SystemCapability.Utils.Lang 751 752**参数:** 753 754| 参数名 | 类型 | 必填 | 说明 | 755| ------- | -------------- | ---- | ------------------ | 756| another | RationalNumber | 是 | 其他的有理数对象。 | 757 758**返回值:** 759 760| 类型 | 说明 | 761| ------ | ------------------------------------------------------------ | 762| number | 如果两个对象相等,则返回0;如果给定对象小于当前对象,则返回1;如果给定对象大于当前对象,则返回-1。 | 763 764**示例:** 765 766 ```js 767let rationalNumber = new util.RationalNumber(1,2); 768let rational = util.RationalNumber.createRationalFromString("3/4"); 769let result = rationalNumber.compare(rational); 770 ``` 771 772### valueOf<sup>8+</sup> 773 774valueOf(): number 775 776以整数形式或者浮点数的形式获取当前RationalNumber对象的值。 777 778**系统能力:** SystemCapability.Utils.Lang 779 780**返回值:** 781 782| 类型 | 说明 | 783| -------- | -------- | 784| number | 返回整数或者浮点数的值。 | 785 786**示例:** 787 788```js 789let rationalNumber = new util.RationalNumber(1,2); 790let result = rationalNumber.valueOf(); 791``` 792 793### equals<sup>8+</sup> 794 795equals(obj: Object): boolean 796 797将当前的RationalNumber对象与给定的对象进行比较是否相等。 798 799**系统能力:** SystemCapability.Utils.Lang 800 801**参数:** 802 803| 参数名 | 类型 | 必填 | 说明 | 804| -------- | -------- | -------- | -------- | 805| object | Object | 是 | 其他类型对象。 | 806 807**返回值:** 808 809| 类型 | 说明 | 810| -------- | -------- | 811| boolean | 如果给定对象与当前对象相同,则返回true;否则返回false。 | 812 813**示例:** 814 815```js 816let rationalNumber = new util.RationalNumber(1,2); 817let rational = util.RationalNumber.createRationalFromString("3/4"); 818let result = rationalNumber.equals(rational); 819``` 820 821### getCommonFactor<sup>9+</sup> 822 823getCommonFactor(number1: number,number2: number): number 824 825获取两个指定整数的最大公约数。 826 827**系统能力:** SystemCapability.Utils.Lang 828 829**参数:** 830 831| 参数名 | 类型 | 必填 | 说明 | 832| ------- | ------ | ---- | ---------- | 833| number1 | number | 是 | 整数类型。 | 834| number2 | number | 是 | 整数类型。 | 835 836**返回值:** 837 838| 类型 | 说明 | 839| ------ | ------------------------------ | 840| number | 返回两个给定数字的最大公约数。 | 841 842**示例:** 843 844```js 845let rationalNumber = new util.RationalNumber(1,2); 846let result = util.RationalNumber.getCommonFactor(4,6); 847``` 848 849### getNumerator<sup>8+</sup> 850 851getNumerator(): number 852 853获取当前RationalNumber对象的分子。 854 855**系统能力:** SystemCapability.Utils.Lang 856 857**返回值:** 858 859| 类型 | 说明 | 860| -------- | -------- | 861| number | 返回RationalNumber对象的分子的值。 | 862 863**示例:** 864 865```js 866let rationalNumber = new util.RationalNumber(1,2); 867let result = rationalNumber.getNumerator(); 868``` 869 870### getDenominator<sup>8+</sup> 871 872getDenominator(): number 873 874获取当前RationalNumber对象的分母。 875 876**系统能力:** SystemCapability.Utils.Lang 877 878**返回值:** 879 880| 类型 | 说明 | 881| -------- | -------- | 882| number | 返回RationalNumber对象的分母的值。 | 883 884**示例:** 885 886```js 887let rationalNumber = new util.RationalNumber(1,2); 888let result = rationalNumber.getDenominator(); 889``` 890 891### isZero<sup>8+</sup> 892 893isZero():boolean 894 895检查当前RationalNumber对象是否为0。 896 897**系统能力:** SystemCapability.Utils.Lang 898 899**返回值:** 900 901| 类型 | 说明 | 902| -------- | -------- | 903| boolean | 如果当前对象表示的值为0,则返回true;否则返回false。 | 904 905**示例:** 906 907```js 908let rationalNumber = new util.RationalNumber(1,2); 909let result = rationalNumber.isZero(); 910``` 911 912### isNaN<sup>8+</sup> 913 914isNaN(): boolean 915 916检查当前RationalNumber对象是否表示非数字(NaN)值。 917 918**系统能力:** SystemCapability.Utils.Lang 919 920**返回值:** 921 922| 类型 | 说明 | 923| -------- | -------- | 924| boolean | 如果分母和分子都为0,则返回true;否则返回false。 | 925 926**示例:** 927 928```js 929let rationalNumber = new util.RationalNumber(1,2); 930let result = rationalNumber.isNaN(); 931``` 932 933### isFinite<sup>8+</sup> 934 935isFinite():boolean 936 937检查当前RationalNumber对象是否表示一个有限值。 938 939**系统能力:** SystemCapability.Utils.Lang 940 941**返回值:** 942 943| 类型 | 说明 | 944| -------- | -------- | 945| boolean | 如果分母不为0,则返回true;否则返回false。 | 946 947**示例:** 948 949```js 950let rationalNumber = new util.RationalNumber(1,2); 951let result = rationalNumber.isFinite(); 952``` 953 954### toString<sup>8+</sup> 955 956toString(): string 957 958获取当前RationalNumber对象的字符串表示形式。 959 960**系统能力:** SystemCapability.Utils.Lang 961 962**返回值:** 963 964| 类型 | 说明 | 965| -------- | -------- | 966| string | 返回Numerator/Denominator格式的字符串,例如3/5,如果当前对象的分子和分母都为0,则返回NaN。 | 967 968**示例:** 969 970```js 971let rationalNumber = new util.RationalNumber(1,2); 972let result = rationalNumber.toString(); 973``` 974 975### constructor<sup>(deprecated)</sup> 976 977constructor(numerator: number,denominator: number) 978 979RationalNumber的构造函数。 980 981> **说明:** 982> 983> 从API version 8开始支持,从API version 9开始废弃,建议使用[constructor<sup>9+</sup>](#constructor9)替代。 984 985**系统能力:** SystemCapability.Utils.Lang 986 987**参数:** 988 989| 参数名 | 类型 | 必填 | 说明 | 990| -------- | -------- | -------- | -------- | 991| numerator | number | 是 | 分子,整数类型。 | 992| denominator | number | 是 | 分母,整数类型。 | 993 994**示例:** 995 996```js 997let rationalNumber = new util.RationalNumber(1,2); 998``` 999 1000### compareTo<sup>(deprecated)</sup> 1001 1002compareTo(another: RationalNumber): number 1003 1004将当前的RationalNumber对象与给定的对象进行比较。 1005 1006> **说明:** 1007> 1008> 从API version 8开始支持,从API version 9开始废弃,建议使用[compare<sup>9+</sup>](#compare9)替代。 1009 1010**系统能力:** SystemCapability.Utils.Lang 1011 1012**参数:** 1013 1014| 参数名 | 类型 | 必填 | 说明 | 1015| -------- | -------- | -------- | -------- | 1016| another | RationalNumber | 是 | 其他的有理数对象。 | 1017 1018**返回值:** 1019 1020| 类型 | 说明 | 1021| -------- | -------- | 1022| number | 如果两个对象相等,则返回0;如果给定对象小于当前对象,则返回1;如果给定对象大于当前对象,则返回-1。 | 1023 1024**示例:** 1025 1026```js 1027let rationalNumber = new util.RationalNumber(1,2); 1028let rational = util.RationalNumber.createRationalFromString("3/4"); 1029let result = rationalNumber.compareTo(rational); 1030``` 1031 1032### getCommonDivisor<sup>(deprecated)</sup> 1033 1034static getCommonDivisor(number1: number,number2: number): number 1035 1036获取两个指定整数的最大公约数。 1037 1038> **说明:** 1039> 1040> 从API version 8开始支持,从API version 9开始废弃,建议使用[getCommonFactor<sup>9+</sup>](#getcommonfactor9)替代。 1041 1042**系统能力:** SystemCapability.Utils.Lang 1043 1044**参数:** 1045 1046| 参数名 | 类型 | 必填 | 说明 | 1047| -------- | -------- | -------- | -------- | 1048| number1 | number | 是 | 整数类型。 | 1049| number2 | number | 是 | 整数类型。 | 1050 1051**返回值:** 1052 1053| 类型 | 说明 | 1054| -------- | -------- | 1055| number | 返回两个给定数字的最大公约数。 | 1056 1057**示例:** 1058 1059```js 1060let rationalNumber = new util.RationalNumber(1,2); 1061let result = util.RationalNumber.getCommonDivisor(4,6); 1062``` 1063 1064## LRUCache<sup>9+</sup> 1065 1066### 属性 1067 1068**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 1069 1070| 名称 | 类型 | 可读 | 可写 | 说明 | 1071| ------ | ------ | ---- | ---- | ---------------------- | 1072| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 1073 1074**示例:** 1075 1076```js 1077let pro = new util.LRUCache(); 1078pro.put(2,10); 1079pro.put(1,8); 1080let result = pro.length; 1081``` 1082 1083### constructor<sup>9+</sup> 1084 1085constructor(capacity?: number) 1086 1087默认构造函数用于创建一个新的LruBuffer实例,默认容量为64。 1088 1089**系统能力:** SystemCapability.Utils.Lang 1090 1091**参数:** 1092 1093| 参数名 | 类型 | 必填 | 说明 | 1094| -------- | ------ | ---- | ---------------------------- | 1095| capacity | number | 否 | 指示要为缓冲区自定义的容量。 | 1096 1097**示例:** 1098 1099```js 1100let lrubuffer= new util.LRUCache(); 1101``` 1102 1103 1104### updateCapacity<sup>9+</sup> 1105 1106updateCapacity(newCapacity: number): void 1107 1108将缓冲区容量更新为指定容量,如果newCapacity小于或等于0,则抛出异常。 1109 1110**系统能力:** SystemCapability.Utils.Lang 1111 1112**参数:** 1113 1114| 参数名 | 类型 | 必填 | 说明 | 1115| ----------- | ------ | ---- | ---------------------------- | 1116| newCapacity | number | 是 | 指示要为缓冲区自定义的容量。 | 1117 1118**示例:** 1119 1120```js 1121let pro = new util.LRUCache(); 1122let result = pro.updateCapacity(100); 1123``` 1124 1125 1126### toString<sup>9+</sup> 1127 1128toString(): string 1129 1130返回对象的字符串表示形式。 1131 1132**系统能力:** SystemCapability.Utils.Lang 1133 1134**返回值:** 1135 1136| 类型 | 说明 | 1137| ------ | -------------------------- | 1138| string | 返回对象的字符串表示形式。 | 1139 1140**示例:** 1141 1142```js 1143let pro = new util.LRUCache(); 1144pro.put(2,10); 1145pro.get(2); 1146pro.remove(20); 1147let result = pro.toString(); 1148``` 1149 1150 1151### getCapacity<sup>9+</sup> 1152 1153getCapacity(): number 1154 1155获取当前缓冲区的容量。 1156 1157**系统能力:** SystemCapability.Utils.Lang 1158 1159**返回值:** 1160 1161| 类型 | 说明 | 1162| ------ | ---------------------- | 1163| number | 返回当前缓冲区的容量。 | 1164 1165**示例:** 1166 1167 ```js 1168let pro = new util.LRUCache(); 1169let result = pro.getCapacity(); 1170 ``` 1171 1172 1173### clear<sup>9+</sup> 1174 1175clear(): void 1176 1177从当前缓冲区清除键值对。后续会调用afterRemoval()方法执行后续操作。 1178 1179**系统能力:** SystemCapability.Utils.Lang 1180 1181**示例:** 1182 1183 ```js 1184let pro = new util.LRUCache(); 1185pro.put(2,10); 1186let result = pro.length; 1187pro.clear(); 1188 ``` 1189 1190 1191### getCreateCount<sup>9+</sup> 1192 1193getCreateCount(): number 1194 1195获取createDefault()返回值的次数。 1196 1197**系统能力:** SystemCapability.Utils.Lang 1198 1199**返回值:** 1200 1201| 类型 | 说明 | 1202| ------ | --------------------------------- | 1203| number | 返回createDefault()返回值的次数。 | 1204 1205**示例:** 1206 1207 ```js 1208let pro = new util.LRUCache(); 1209pro.put(1,8); 1210let result = pro.getCreateCount(); 1211 ``` 1212 1213 1214### getMissCount<sup>9+</sup> 1215 1216getMissCount(): number 1217 1218获取查询值不匹配的次数。 1219 1220**系统能力:** SystemCapability.Utils.Lang 1221 1222**返回值:** 1223 1224| 类型 | 说明 | 1225| ------ | ------------------------ | 1226| number | 返回查询值不匹配的次数。 | 1227 1228**示例:** 1229 1230 ```js 1231let pro = new util.LRUCache(); 1232pro.put(2,10); 1233pro.get(2); 1234let result = pro.getMissCount(); 1235 ``` 1236 1237 1238### getRemovalCount<sup>9+</sup> 1239 1240getRemovalCount(): number 1241 1242获取从缓冲区中逐出值的次数。 1243 1244**系统能力:** SystemCapability.Utils.Lang 1245 1246**返回值:** 1247 1248| 类型 | 说明 | 1249| ------ | -------------------------- | 1250| number | 返回从缓冲区中驱逐的次数。 | 1251 1252**示例:** 1253 1254 ```js 1255let pro = new util.LRUCache(); 1256pro.put(2,10); 1257pro.updateCapacity(2); 1258pro.put(50,22); 1259let result = pro.getRemovalCount(); 1260 ``` 1261 1262 1263### getMatchCount<sup>9+</sup> 1264 1265getMatchCount(): number 1266 1267获取查询值匹配成功的次数。 1268 1269**系统能力:** SystemCapability.Utils.Lang 1270 1271**返回值:** 1272 1273| 类型 | 说明 | 1274| ------ | -------------------------- | 1275| number | 返回查询值匹配成功的次数。 | 1276 1277**示例:** 1278 1279 ```js 1280let pro = new util.LRUCache(); 1281pro.put(2,10); 1282pro.get(2); 1283let result = pro.getMatchCount(); 1284 ``` 1285 1286 1287### getPutCount<sup>9+</sup> 1288 1289getPutCount(): number 1290 1291获取将值添加到缓冲区的次数。 1292 1293**系统能力:** SystemCapability.Utils.Lang 1294 1295**返回值:** 1296 1297| 类型 | 说明 | 1298| ------ | ---------------------------- | 1299| number | 返回将值添加到缓冲区的次数。 | 1300 1301**示例:** 1302 1303 ```js 1304let pro = new util.LRUCache(); 1305pro.put(2,10); 1306let result = pro.getPutCount(); 1307 ``` 1308 1309 1310### isEmpty<sup>9+</sup> 1311 1312isEmpty(): boolean 1313 1314检查当前缓冲区是否为空。 1315 1316**系统能力:** SystemCapability.Utils.Lang 1317 1318**返回值:** 1319 1320| 类型 | 说明 | 1321| ------- | ---------------------------------------- | 1322| boolean | 如果当前缓冲区不包含任何值,则返回true。 | 1323 1324**示例:** 1325 1326 ```js 1327let pro = new util.LRUCache(); 1328pro.put(2,10); 1329let result = pro.isEmpty(); 1330 ``` 1331 1332 1333### get<sup>9+</sup> 1334 1335get(key: K): V | undefined 1336 1337表示要查询的键。 1338 1339**系统能力:** SystemCapability.Utils.Lang 1340 1341**参数:** 1342 1343| 参数名 | 类型 | 必填 | 说明 | 1344| ------ | ---- | ---- | ------------ | 1345| key | K | 是 | 要查询的键。 | 1346 1347**返回值:** 1348 1349| 类型 | 说明 | 1350| ------------------------ | ------------------------------------------------------------ | 1351| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回undefined。 | 1352 1353**示例:** 1354 1355 ```js 1356let pro = new util.LRUCache(); 1357pro.put(2,10); 1358let result = pro.get(2); 1359 ``` 1360 1361 1362### put<sup>9+</sup> 1363 1364put(key: K,value: V): V 1365 1366将键值对添加到缓冲区。 1367 1368**系统能力:** SystemCapability.Utils.Lang 1369 1370**参数:** 1371 1372| 参数名 | 类型 | 必填 | 说明 | 1373| ------ | ---- | ---- | -------------------------- | 1374| key | K | 是 | 要添加的密钥。 | 1375| value | V | 是 | 指示与要添加的键关联的值。 | 1376 1377**返回值:** 1378 1379| 类型 | 说明 | 1380| ---- | ------------------------------------------------------------ | 1381| V | 返回与添加的键关联的值;如果要添加的键已经存在,则返回原始值,如果键或值为空,则抛出此异常。 | 1382 1383**示例:** 1384 1385 ```js 1386let pro = new util.LRUCache(); 1387let result = pro.put(2,10); 1388 ``` 1389 1390### values<sup>9+</sup> 1391 1392values(): V[] 1393 1394获取当前缓冲区中所有值从最近访问到最近最少访问的顺序列表 。 1395 1396**系统能力:** SystemCapability.Utils.Lang 1397 1398**返回值:** 1399 1400| 类型 | 说明 | 1401| --------- | ------------------------------------------------------------ | 1402| V [] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 1403 1404**示例:** 1405 1406 ```js 1407let pro = new util.LRUCache(); 1408pro.put(2,10); 1409pro.put(2,"anhu"); 1410pro.put("afaf","grfb"); 1411let result = pro.values(); 1412 ``` 1413 1414 1415### keys<sup>9+</sup> 1416 1417keys(): K[] 1418 1419获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 1420 1421**系统能力:** SystemCapability.Utils.Lang 1422 1423**返回值:** 1424 1425| 类型 | 说明 | 1426| --------- | ------------------------------------------------------------ | 1427| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 1428 1429**示例:** 1430 1431 ```js 1432let pro = new util.LRUCache(); 1433pro.put(2,10); 1434let result = pro.keys(); 1435 ``` 1436 1437 1438### remove<sup>9+</sup> 1439 1440remove(key: K): V | undefined 1441 1442从当前缓冲区中删除指定的键及其关联的值。 1443 1444**系统能力:** SystemCapability.Utils.Lang 1445 1446**参数:** 1447 1448| 参数名 | 类型 | 必填 | 说明 | 1449| ------ | ---- | ---- | -------------- | 1450| key | K | 是 | 要删除的密钥。 | 1451 1452**返回值:** 1453 1454| 类型 | 说明 | 1455| ------------------------ | ------------------------------------------------------------ | 1456| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回一个空的Optional对象,如果key为null,则抛出异常。 | 1457 1458**示例:** 1459 1460 ```js 1461let pro = new util.LRUCache(); 1462pro.put(2,10); 1463let result = pro.remove(20); 1464 ``` 1465 1466 1467### afterRemoval<sup>9+</sup> 1468 1469afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 1470 1471删除值后执行后续操作。 1472 1473**系统能力:** SystemCapability.Utils.Lang 1474 1475**参数:** 1476 1477| 参数名 | 类型 | 必填 | 说明 | 1478| -------- | ------- | ---- | ------------------------------------------------------------ | 1479| isEvict | boolean | 是 | 因容量不足而调用该方法时,参数值为true,其他情况为false。 | 1480| key | K | 是 | 表示删除的键。 | 1481| value | V | 是 | 表示删除的值。 | 1482| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 1483 1484**示例:** 1485 1486 ```js 1487let arr = []; 1488class ChildLruBuffer<K, V> extends util.LRUCache<K, V> 1489{ 1490 constructor() 1491 { 1492 super(); 1493 } 1494 afterRemoval(isEvict, key, value, newValue) 1495 { 1496 if (isEvict === false) 1497 { 1498 arr = [key, value, newValue]; 1499 } 1500 } 1501} 1502let lru = new ChildLruBuffer(); 1503lru.afterRemoval(false,10,30,null); 1504 ``` 1505 1506 1507### contains<sup>9+</sup> 1508 1509contains(key: K): boolean 1510 1511检查当前缓冲区是否包含指定的键。 1512 1513**系统能力:** SystemCapability.Utils.Lang 1514 1515**参数:** 1516 1517| 参数名 | 类型 | 必填 | 说明 | 1518| ------ | ------ | ---- | ---------------- | 1519| key | K | 是 | 表示要检查的键。 | 1520 1521**返回值:** 1522 1523| 类型 | 说明 | 1524| ------- | ------------------------------------------ | 1525| boolean | 如果缓冲区包含指定的键,则返回 true。 | 1526 1527**示例:** 1528 1529 ```js 1530let pro = new util.LRUCache(); 1531pro.put(2,10); 1532let obj = {1:"key"}; 1533let result = pro.contains(obj); 1534 ``` 1535 1536 1537### createDefault<sup>9+</sup> 1538 1539createDefault(key: K): V 1540 1541如果未计算特定键的值,则执行后续操作,参数表示丢失的键,返回与键关联的值。 1542 1543**系统能力:** SystemCapability.Utils.Lang 1544 1545**参数:** 1546 1547| 参数名 | 类型 | 必填 | 说明 | 1548| ------ | ---- | ---- | -------------- | 1549| key | K | 是 | 表示丢失的键。 | 1550 1551**返回值:** 1552 1553| 类型 | 说明 | 1554| ---- | ------------------ | 1555| V | 返回与键关联的值。 | 1556 1557**示例:** 1558 1559 ```js 1560let pro = new util.LRUCache(); 1561let result = pro.createDefault(50); 1562 ``` 1563 1564 1565### entries<sup>9+</sup> 1566 1567entries(): IterableIterator<[K,V]> 1568 1569允许迭代包含在这个对象中的所有键值对。 1570 1571**系统能力:** SystemCapability.Utils.Lang 1572 1573**返回值:** 1574 1575| 类型 | 说明 | 1576| ----------- | -------------------- | 1577| [K, V] | 返回一个可迭代数组。 | 1578 1579**示例:** 1580 1581 ```js 1582let pro = new util.LRUCache(); 1583pro.put(2,10); 1584let result = pro.entries(); 1585 ``` 1586 1587### [Symbol.iterator]<sup>9+</sup> 1588 1589[Symbol.iterator]\(): IterableIterator<[K, V]> 1590 1591返回一个键值对形式的二维数组。 1592 1593**系统能力:** SystemCapability.Utils.Lang 1594 1595**返回值:** 1596 1597| 类型 | 说明 | 1598| ----------- | ------------------------------ | 1599| [K, V] | 返回一个键值对形式的二维数组。 | 1600 1601**示例:** 1602 1603 ```js 1604let pro = new util.LRUCache(); 1605pro.put(2,10); 1606let result = pro[Symbol.iterator](); 1607 ``` 1608 1609## ScopeComparable<sup>8+</sup> 1610 1611ScopeComparable类型的值需要实现compareTo方法,确保传入的数据具有可比性。 1612 1613**系统能力:** SystemCapability.Utils.Lang 1614 1615### compareTo<sup>8+</sup> 1616 1617compareTo(other: ScopeComparable): boolean; 1618 1619比较两个值的大小,返回一个布尔值。 1620 1621**系统能力:** SystemCapability.Utils.Lang 1622 1623**参数:** 1624 1625| 参数名 | 类型 | 必填 | 说明 | 1626| ------ | ---- | ---- | -------------- | 1627| other | [ScopeComparable](#scopecomparable8) | 是 | 表示要比较的值。 | 1628 1629**返回值:** 1630 1631| 类型 | 说明 | 1632| ---- | ------------------ | 1633| boolean | 调用compareTo的值大于等于传入的值返回true,否则返回false。| 1634 1635**示例:** 1636 1637构造新类,实现compareTo方法。后续示例代码中,均以此Temperature类为例。 1638 1639```js 1640class Temperature{ 1641 constructor(value){ 1642 // 当使用ts语言开发时,需要补充以下代码: 1643 // private readonly _temp: Temperature; 1644 this._temp = value; 1645 } 1646 compareTo(value){ 1647 return this._temp >= value.getTemp(); 1648 } 1649 getTemp(){ 1650 return this._temp; 1651 } 1652 toString(){ 1653 return this._temp.toString(); 1654 } 1655} 1656``` 1657 1658## ScopeType<sup>8+</sup> 1659 1660用于表示范围中的值的类型。 1661 1662**系统能力:** SystemCapability.Utils.Lang 1663 1664| 类型 | 说明 | 1665| -------- | -------- | 1666| number | 表示值的类型为数字。 | 1667| [ScopeComparable](#scopecomparable8) | 表示值的类型为ScopeComparable。| 1668 1669## ScopeHelper<sup>9+</sup> 1670 1671### constructor<sup>9+</sup> 1672 1673constructor(lowerObj: ScopeType, upperObj: ScopeType) 1674 1675用于创建指定下限和上限的作用域实例的构造函数,返回一个ScopeHelper对象。 1676 1677**系统能力:** SystemCapability.Utils.Lang 1678 1679**参数:** 1680 1681| 参数名 | 类型 | 必填 | 说明 | 1682| -------- | ------------------------ | ---- | ---------------------- | 1683| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 1684| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 1685 1686**示例:** 1687 1688 ```js 1689let tempLower = new Temperature(30); 1690let tempUpper = new Temperature(40); 1691let range = new util.ScopeHelper(tempLower, tempUpper); 1692 ``` 1693 1694 1695### toString<sup>9+</sup> 1696 1697toString(): string 1698 1699该字符串化方法返回一个包含当前范围的字符串表示形式。 1700 1701**系统能力:** SystemCapability.Utils.Lang 1702 1703**返回值:** 1704 1705| 类型 | 说明 | 1706| ------ | -------------------------------------- | 1707| string | 返回包含当前范围对象的字符串表示形式。 | 1708 1709**示例:** 1710 1711 ```js 1712let tempLower = new Temperature(30); 1713let tempUpper = new Temperature(40); 1714let range = new util.ScopeHelper(tempLower, tempUpper); 1715let result = range.toString(); 1716 ``` 1717 1718 1719### intersect<sup>9+</sup> 1720 1721intersect(range: ScopeHelper): ScopeHelper 1722 1723获取给定范围和当前范围的交集。 1724 1725**系统能力:** SystemCapability.Utils.Lang 1726 1727**参数:** 1728 1729| 参数名 | 类型 | 必填 | 说明 | 1730| ------ | ---------------------------- | ---- | ------------------ | 1731| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 1732 1733**返回值:** 1734 1735| 类型 | 说明 | 1736| ------------------------------ | ------------------------------ | 1737| [ScopeHelper9+](#scopehelper9) | 返回给定范围和当前范围的交集。 | 1738 1739**示例:** 1740 1741 ```js 1742let tempLower = new Temperature(30); 1743let tempUpper = new Temperature(40); 1744let range = new util.ScopeHelper(tempLower, tempUpper); 1745let tempMiDF = new Temperature(35); 1746let tempMidS = new Temperature(39); 1747let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 1748range.intersect(rangeFir); 1749 ``` 1750 1751 1752### intersect<sup>9+</sup> 1753 1754intersect(lowerObj:ScopeType,upperObj:ScopeType):ScopeHelper 1755 1756获取当前范围与给定下限和上限范围的交集。 1757 1758**系统能力:** SystemCapability.Utils.Lang 1759 1760**参数:** 1761 1762| 参数名 | 类型 | 必填 | 说明 | 1763| -------- | ------------------------ | ---- | ---------------- | 1764| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 1765| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 1766 1767**返回值:** 1768 1769| 类型 | 说明 | 1770| ---------------------------- | ---------------------------------------- | 1771| [ScopeHelper](#scopehelper9) | 返回当前范围与给定下限和上限范围的交集。 | 1772 1773**示例:** 1774 1775 ```js 1776let tempLower = new Temperature(30); 1777let tempUpper = new Temperature(40); 1778let tempMiDF = new Temperature(35); 1779let tempMidS = new Temperature(39); 1780let range = new util.ScopeHelper(tempLower, tempUpper); 1781let result = range.intersect(tempMiDF, tempMidS); 1782 ``` 1783 1784 1785### getUpper<sup>9+</sup> 1786 1787getUpper(): ScopeType 1788 1789获取当前范围的上限。 1790 1791**系统能力:** SystemCapability.Utils.Lang 1792 1793**返回值:** 1794 1795| 类型 | 说明 | 1796| ------------------------ | ---------------------- | 1797| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 1798 1799**示例:** 1800 1801 ```js 1802let tempLower = new Temperature(30); 1803let tempUpper = new Temperature(40); 1804let range = new util.ScopeHelper(tempLower, tempUpper); 1805let result = range.getUpper(); 1806 ``` 1807 1808 1809### getLower<sup>9+</sup> 1810 1811getLower(): ScopeType 1812 1813获取当前范围的下限。 1814 1815**系统能力:** SystemCapability.Utils.Lang 1816 1817**返回值:** 1818 1819| 类型 | 说明 | 1820| ------------------------ | ---------------------- | 1821| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 1822 1823**示例:** 1824 1825 ```js 1826let tempLower = new Temperature(30); 1827let tempUpper = new Temperature(40); 1828let range = new util.ScopeHelper(tempLower, tempUpper); 1829let result = range.getLower(); 1830 ``` 1831 1832 1833### expand<sup>9+</sup> 1834 1835expand(lowerObj: ScopeType,upperObj: ScopeType): ScopeHelper 1836 1837创建并返回包括当前范围和给定下限和上限的并集。 1838 1839**系统能力:** SystemCapability.Utils.Lang 1840 1841**参数:** 1842 1843| 参数名 | 类型 | 必填 | 说明 | 1844| -------- | ------------------------ | ---- | ---------------- | 1845| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 1846| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 1847 1848**返回值:** 1849 1850| 类型 | 说明 | 1851| ---------------------------- | ------------------------------------ | 1852| [ScopeHelper](#scopehelper9) | 返回当前范围和给定下限和上限的并集。 | 1853 1854**示例:** 1855 1856 ```js 1857let tempLower = new Temperature(30); 1858let tempUpper = new Temperature(40); 1859let tempMiDF = new Temperature(35); 1860let tempMidS = new Temperature(39); 1861let range = new util.ScopeHelper(tempLower, tempUpper); 1862let result = range.expand(tempMiDF, tempMidS); 1863 ``` 1864 1865 1866### expand<sup>9+</sup> 1867 1868expand(range: ScopeHelper): ScopeHelper 1869 1870创建并返回包括当前范围和给定范围的并集。 1871 1872**系统能力:** SystemCapability.Utils.Lang 1873 1874**参数:** 1875 1876| 参数名 | 类型 | 必填 | 说明 | 1877| ------ | ---------------------------- | ---- | ------------------ | 1878| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 1879 1880**返回值:** 1881 1882| 类型 | 说明 | 1883| ---------------------------- | ---------------------------------- | 1884| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定范围的并集。 | 1885 1886**示例:** 1887 1888 ```js 1889let tempLower = new Temperature(30); 1890let tempUpper = new Temperature(40); 1891let tempMiDF = new Temperature(35); 1892let tempMidS = new Temperature(39); 1893let range = new util.ScopeHelper(tempLower, tempUpper); 1894let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 1895let result = range.expand(rangeFir); 1896 ``` 1897 1898 1899### expand<sup>9+</sup> 1900 1901expand(value: ScopeType): ScopeHelper 1902 1903创建并返回包括当前范围和给定值的并集。 1904 1905**系统能力:** SystemCapability.Utils.Lang 1906 1907**参数:** 1908 1909| 参数名 | 类型 | 必填 | 说明 | 1910| ------ | ------------------------ | ---- | ---------------- | 1911| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 1912 1913**返回值:** 1914 1915| 类型 | 说明 | 1916| ---------------------------- | -------------------------------- | 1917| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定值的并集。 | 1918 1919**示例:** 1920 1921 ```js 1922let tempLower = new Temperature(30); 1923let tempUpper = new Temperature(40); 1924let tempMiDF = new Temperature(35); 1925let range = new util.ScopeHelper(tempLower, tempUpper); 1926let result = range.expand(tempMiDF); 1927 ``` 1928 1929 1930### contains<sup>9+</sup> 1931 1932contains(value: ScopeType): boolean 1933 1934检查给定value是否包含在当前范围内。 1935 1936**系统能力:** SystemCapability.Utils.Lang 1937 1938**参数:** 1939 1940| 参数名 | 类型 | 必填 | 说明 | 1941| ------ | ------------------------ | ---- | ---------------- | 1942| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 1943 1944**返回值:** 1945 1946| 类型 | 说明 | 1947| ------- | --------------------------------------------------- | 1948| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 1949 1950**示例:** 1951 1952 ```js 1953let tempLower = new Temperature(30); 1954let tempUpper = new Temperature(40); 1955let tempMiDF = new Temperature(35); 1956let range = new util.ScopeHelper(tempLower, tempUpper); 1957range.contains(tempMiDF); 1958 ``` 1959 1960 1961### contains<sup>9+</sup> 1962 1963contains(range: ScopeHelper): boolean 1964 1965检查给定range是否在当前范围内。 1966 1967**系统能力:** SystemCapability.Utils.Lang 1968 1969**参数:** 1970 1971| 参数名 | 类型 | 必填 | 说明 | 1972| ------ | ---------------------------- | ---- | ------------------ | 1973| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 1974 1975**返回值:** 1976 1977| 类型 | 说明 | 1978| ------- | ----------------------------------------------------- | 1979| boolean | 如果给定范围包含在当前范围内返回true,否则返回false。 | 1980 1981**示例:** 1982 1983 ```js 1984let tempLower = new Temperature(30); 1985let tempUpper = new Temperature(40); 1986let range = new util.ScopeHelper(tempLower, tempUpper); 1987let tempLess = new Temperature(20); 1988let tempMore = new Temperature(45); 1989let rangeSec = new util.ScopeHelper(tempLess, tempMore); 1990let result = range.contains(rangeSec); 1991 ``` 1992 1993 1994### clamp<sup>9+</sup> 1995 1996clamp(value: ScopeType): ScopeType 1997 1998将给定值限定到当前范围内。 1999 2000**系统能力:** SystemCapability.Utils.Lang 2001 2002**参数:** 2003 2004| 参数名 | 类型 | 必填 | 说明 | 2005| ------ | ------------------------ | ---- | -------------- | 2006| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 2007 2008**返回值:** 2009 2010| 类型 | 说明 | 2011| ------------------------ | ------------------------------------------------------------ | 2012| [ScopeType](#scopetype8) | 如果传入的value小于下限,则返回lowerObj;如果大于上限值则返回upperObj;如果在当前范围内,则返回value。 | 2013 2014**示例:** 2015 2016 ```js 2017let tempLower = new Temperature(30); 2018let tempUpper = new Temperature(40); 2019let tempMiDF = new Temperature(35); 2020let range = new util.ScopeHelper(tempLower, tempUpper); 2021let result = range.clamp(tempMiDF); 2022 ``` 2023 2024## Base64Helper<sup>9+</sup> 2025 2026### constructor<sup>9+</sup> 2027 2028constructor() 2029 2030Base64Helper的构造函数。 2031 2032**系统能力:** SystemCapability.Utils.Lang 2033 2034**示例:** 2035 2036 ```js 2037let base64 = new util.Base64Helper(); 2038 ``` 2039 2040### encodeSync<sup>9+</sup> 2041 2042encodeSync(src: Uint8Array): Uint8Array 2043 2044通过输入参数编码后输出对应文本。 2045 2046**系统能力:** SystemCapability.Utils.Lang 2047 2048**参数:** 2049 2050| 参数名 | 类型 | 必填 | 说明 | 2051| ------ | ---------- | ---- | ------------------- | 2052| src | Uint8Array | 是 | 编码输入Uint8数组。 | 2053 2054**返回值:** 2055 2056| 类型 | 说明 | 2057| ---------- | ----------------------------- | 2058| Uint8Array | 返回编码后新分配的Uint8数组。 | 2059 2060**示例:** 2061 2062 ```js 2063let that = new util.Base64Helper(); 2064let array = new Uint8Array([115,49,51]); 2065let result = that.encodeSync(array); 2066 ``` 2067 2068 2069### encodeToStringSync<sup>9+</sup> 2070 2071encodeToStringSync(src: Uint8Array): string 2072 2073通过输入参数编码后输出对应文本。 2074 2075**系统能力:** SystemCapability.Utils.Lang 2076 2077**参数:** 2078 2079| 参数名 | 类型 | 必填 | 说明 | 2080| ------ | ---------- | ---- | ------------------- | 2081| src | Uint8Array | 是 | 编码输入Uint8数组。 | 2082 2083**返回值:** 2084 2085| 类型 | 说明 | 2086| ------ | -------------------- | 2087| string | 返回编码后的字符串。 | 2088 2089**示例:** 2090 2091 ```js 2092let that = new util.Base64Helper(); 2093let array = new Uint8Array([115,49,51]); 2094let result = that.encodeToStringSync(array); 2095 ``` 2096 2097 2098### decodeSync<sup>9+</sup> 2099 2100decodeSync(src: Uint8Array | string): Uint8Array 2101 2102通过输入参数解码后输出对应文本。 2103 2104**系统能力:** SystemCapability.Utils.Lang 2105 2106**参数:** 2107 2108| 参数名 | 类型 | 必填 | 说明 | 2109| ------ | ------------------------------ | ---- | ----------------------------- | 2110| src | Uint8Array \| string | 是 | 解码输入Uint8数组或者字符串。 | 2111 2112**返回值:** 2113 2114| 类型 | 说明 | 2115| ---------- | ----------------------------- | 2116| Uint8Array | 返回解码后新分配的Uint8数组。 | 2117 2118**示例:** 2119 2120 ```js 2121let that = new util.Base64Helper(); 2122let buff = 'czEz'; 2123let result = that.decodeSync(buff); 2124 ``` 2125 2126 2127### encode<sup>9+</sup> 2128 2129encode(src: Uint8Array): Promise<Uint8Array> 2130 2131通过输入参数异步编码后输出对应文本。 2132 2133**系统能力:** SystemCapability.Utils.Lang 2134 2135**参数:** 2136 2137| 参数名 | 类型 | 必填 | 说明 | 2138| ------ | ---------- | ---- | ----------------------- | 2139| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 2140 2141**返回值:** 2142 2143| 类型 | 说明 | 2144| ------------------------- | --------------------------------- | 2145| Promise<Uint8Array> | 返回异步编码后新分配的Uint8数组。 | 2146 2147**示例:** 2148 2149 ```js 2150let that = new util.Base64Helper(); 2151let array = new Uint8Array([115,49,51]); 2152let rarray = new Uint8Array([99,122,69,122]); 2153that.encode(array).then(val=>{ 2154 for (var i = 0; i < rarray.length; i++) { 2155 console.log(val[i].toString()) 2156 } 2157}) 2158 ``` 2159 2160 2161### encodeToString<sup>9+</sup> 2162 2163encodeToString(src: Uint8Array): Promise<string> 2164 2165通过输入参数异步编码后输出对应文本。 2166 2167**系统能力:** SystemCapability.Utils.Lang 2168 2169**参数:** 2170 2171| 参数名 | 类型 | 必填 | 说明 | 2172| ------ | ---------- | ---- | ----------------------- | 2173| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 2174 2175**返回值:** 2176 2177| 类型 | 说明 | 2178| --------------------- | ------------------------ | 2179| Promise<string> | 返回异步编码后的字符串。 | 2180 2181**示例:** 2182 2183 ```js 2184let that = new util.Base64Helper(); 2185let array = new Uint8Array([115,49,51]); 2186that.encodeToString(array).then(val=>{ 2187 console.log(val) 2188}) 2189 ``` 2190 2191 2192### decode<sup>9+</sup> 2193 2194decode(src: Uint8Array | string): Promise<Uint8Array> 2195 2196通过输入参数异步解码后输出对应文本。 2197 2198**系统能力:** SystemCapability.Utils.Lang 2199 2200**参数:** 2201 2202| 参数名 | 类型 | 必填 | 说明 | 2203| ------ | ------------------------------ | ---- | --------------------------------- | 2204| src | Uint8Array \| string | 是 | 异步解码输入Uint8数组或者字符串。 | 2205 2206**返回值:** 2207 2208| 类型 | 说明 | 2209| ------------------------- | --------------------------------- | 2210| Promise<Uint8Array> | 返回异步解码后新分配的Uint8数组。 | 2211 2212**示例:** 2213 2214 ```js 2215let that = new util.Base64Helper(); 2216let array = new Uint8Array([99,122,69,122]); 2217let rarray = new Uint8Array([115,49,51]); 2218that.decode(array).then(val=>{ 2219 for (var i = 0; i < rarray.length; i++) { 2220 console.log(val[i].toString()) 2221 } 2222}) 2223 ``` 2224 2225## types<sup>8+</sup> 2226 2227### constructor<sup>8+</sup> 2228 2229constructor() 2230 2231Types的构造函数。 2232 2233**系统能力:** SystemCapability.Utils.Lang 2234 2235**示例:** 2236 2237 ```js 2238 let type = new util.types(); 2239 ``` 2240 2241 2242### isAnyArrayBuffer<sup>8+</sup> 2243 2244isAnyArrayBuffer(value: Object): boolean 2245 2246检查输入的value是否是ArrayBuffer类型。 2247 2248**系统能力:** SystemCapability.Utils.Lang 2249 2250**参数:** 2251 2252| 参数名 | 类型 | 必填 | 说明 | 2253| -------- | -------- | -------- | -------- | 2254| value | Object | 是 | 待检测对象。 | 2255 2256**返回值:** 2257 2258| 类型 | 说明 | 2259| -------- | -------- | 2260| boolean | 判断的结果,如果是ArrayBuffer类型为true,反之为false。 | 2261 2262**示例:** 2263 2264 ```js 2265 let that = new util.types(); 2266 let result = that.isAnyArrayBuffer(new ArrayBuffer(0)); 2267 ``` 2268 2269 2270### isArrayBufferView<sup>8+</sup> 2271 2272isArrayBufferView(value: Object): boolean 2273 2274检查输入的value是否是内置ArrayBufferView辅助类型。 2275 2276ArrayBufferView辅助类型包括:Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint32Array、Float32Array、Float64Array、DataView。 2277 2278**系统能力:** SystemCapability.Utils.Lang 2279 2280**参数:** 2281 2282| 参数名 | 类型 | 必填 | 说明 | 2283| -------- | -------- | -------- | -------- | 2284| value | Object | 是 | 待检测对象。 | 2285 2286**返回值:** 2287 2288| 类型 | 说明 | 2289| -------- | -------- | 2290| boolean | 判断的结果,如果是内置包含的ArrayBufferView辅助类型为true,反之为false。 | 2291 2292**示例:** 2293 2294 ```js 2295 let that = new util.types(); 2296 let result = that.isArrayBufferView(new Int8Array([])); 2297 ``` 2298 2299 2300### isArgumentsObject<sup>8+</sup> 2301 2302isArgumentsObject(value: Object): boolean 2303 2304检查输入的value是否是一个arguments对象类型。 2305 2306**系统能力:** SystemCapability.Utils.Lang 2307 2308**参数:** 2309 2310| 参数名 | 类型 | 必填 | 说明 | 2311| -------- | -------- | -------- | -------- | 2312| value | Object | 是 | 待检测对象。 | 2313 2314**返回值:** 2315 2316| 类型 | 说明 | 2317| -------- | -------- | 2318| boolean | 判断的结果,如果是内置包含的arguments类型为true,反之为false。 | 2319 2320**示例:** 2321 2322 ```js 2323 let that = new util.types(); 2324 function foo() { 2325 var result = that.isArgumentsObject(arguments); 2326 } 2327 let f = foo(); 2328 ``` 2329 2330 2331### isArrayBuffer<sup>8+</sup> 2332 2333isArrayBuffer(value: Object): boolean 2334 2335检查输入的value是否是ArrayBuffer类型。 2336 2337**系统能力:** SystemCapability.Utils.Lang 2338 2339**参数:** 2340 2341| 参数名 | 类型 | 必填 | 说明 | 2342| -------- | -------- | -------- | -------- | 2343| value | Object | 是 | 待检测对象。 | 2344 2345**返回值:** 2346 2347| 类型 | 说明 | 2348| -------- | -------- | 2349| boolean | 判断的结果,如果是内置包含的ArrayBuffer类型为true,反之为false。 | 2350 2351**示例:** 2352 2353 ```js 2354 let that = new util.types(); 2355 let result = that.isArrayBuffer(new ArrayBuffer(0)); 2356 ``` 2357 2358 2359### isAsyncFunction<sup>8+</sup> 2360 2361isAsyncFunction(value: Object): boolean 2362 2363检查输入的value是否是一个异步函数类型。 2364 2365**系统能力:** SystemCapability.Utils.Lang 2366 2367**参数:** 2368 2369| 参数名 | 类型 | 必填 | 说明 | 2370| -------- | -------- | -------- | -------- | 2371| value | Object | 是 | 待检测对象。 | 2372 2373**返回值:** 2374 2375| 类型 | 说明 | 2376| -------- | -------- | 2377| boolean | 判断的结果,如果是内置包含的异步函数类型为true,反之为false。 | 2378 2379**示例:** 2380 2381 ```js 2382 let that = new util.types(); 2383 let result = that.isAsyncFunction(async function foo() {}); 2384 ``` 2385 2386 2387### isBooleanObject<sup>8+</sup> 2388 2389isBooleanObject(value: Object): boolean 2390 2391检查输入的value是否是一个Boolean对象类型。 2392 2393**系统能力:** SystemCapability.Utils.Lang 2394 2395**参数:** 2396 2397| 参数名 | 类型 | 必填 | 说明 | 2398| -------- | -------- | -------- | -------- | 2399| value | Object | 是 | 待检测对象。 | 2400 2401**返回值:** 2402 2403| 类型 | 说明 | 2404| -------- | -------- | 2405| boolean | 判断的结果,如果是内置包含的Boolean对象类型为true,反之为false。 | 2406 2407**示例:** 2408 2409 ```js 2410 let that = new util.types(); 2411 let result = that.isBooleanObject(new Boolean(true)); 2412 ``` 2413 2414 2415### isBoxedPrimitive<sup>8+</sup> 2416 2417isBoxedPrimitive(value: Object): boolean 2418 2419检查输入的value是否是Boolean或Number或String或Symbol对象类型。 2420 2421**系统能力:** SystemCapability.Utils.Lang 2422 2423**参数:** 2424 2425| 参数名 | 类型 | 必填 | 说明 | 2426| -------- | -------- | -------- | -------- | 2427| value | Object | 是 | 待检测对象。 | 2428 2429**返回值:** 2430 2431| 类型 | 说明 | 2432| -------- | -------- | 2433| boolean | 判断的结果,如果是内置包含的Boolean或Number或String或Symbol对象类型为true,反之为false。 | 2434 2435**示例:** 2436 2437 ```js 2438 let that = new util.types(); 2439 let result = that.isBoxedPrimitive(new Boolean(false)); 2440 ``` 2441 2442 2443### isDataView<sup>8+</sup> 2444 2445isDataView(value: Object): boolean 2446 2447检查输入的value是否是DataView类型。 2448 2449**系统能力:** SystemCapability.Utils.Lang 2450 2451**参数:** 2452 2453| 参数名 | 类型 | 必填 | 说明 | 2454| -------- | -------- | -------- | -------- | 2455| value | Object | 是 | 待检测对象。 | 2456 2457**返回值:** 2458 2459| 类型 | 说明 | 2460| -------- | -------- | 2461| boolean | 判断的结果,如果是内置包含的DataView对象类型为true,反之为false。 | 2462 2463**示例:** 2464 2465 ```js 2466 let that = new util.types(); 2467 const ab = new ArrayBuffer(20); 2468 let result = that.isDataView(new DataView(ab)); 2469 ``` 2470 2471 2472### isDate<sup>8+</sup> 2473 2474isDate(value: Object): boolean 2475 2476检查输入的value是否是Date类型。 2477 2478**系统能力:** SystemCapability.Utils.Lang 2479 2480**参数:** 2481 2482| 参数名 | 类型 | 必填 | 说明 | 2483| -------- | -------- | -------- | -------- | 2484| value | Object | 是 | 待检测对象。 | 2485 2486**返回值:** 2487 2488| 类型 | 说明 | 2489| -------- | -------- | 2490| boolean | 判断的结果,如果是内置包含的Date对象类型为true,反之为false。 | 2491 2492**示例:** 2493 2494 ```js 2495 let that = new util.types(); 2496 let result = that.isDate(new Date()); 2497 ``` 2498 2499 2500### isExternal<sup>8+</sup> 2501 2502isExternal(value: Object): boolean 2503 2504检查输入的value是否是native External类型。 2505 2506**系统能力:** SystemCapability.Utils.Lang 2507 2508**参数:** 2509 2510| 参数名 | 类型 | 必填 | 说明 | 2511| -------- | -------- | -------- | -------- | 2512| value | Object | 是 | 待检测对象。 | 2513 2514**返回值:** 2515 2516| 类型 | 说明 | 2517| -------- | -------- | 2518| boolean | 判断的结果,如果是内置包含native External类型为true,反之为false。 | 2519 2520**示例:** 2521 2522 ```js 2523 let that = new util.types(); 2524 let result = that.isExternal(true); 2525 ``` 2526 2527 2528### isFloat32Array<sup>8+</sup> 2529 2530isFloat32Array(value: Object): boolean 2531 2532检查输入的value是否是Float32Array数组类型。 2533 2534**系统能力:** SystemCapability.Utils.Lang 2535 2536**参数:** 2537 2538| 参数名 | 类型 | 必填 | 说明 | 2539| -------- | -------- | -------- | -------- | 2540| value | Object | 是 | 待检测对象。 | 2541 2542**返回值:** 2543 2544| 类型 | 说明 | 2545| -------- | -------- | 2546| boolean | 判断的结果,如果是内置包含的Float32Array数组类型为true,反之为false。 | 2547 2548**示例:** 2549 2550 ```js 2551 let that = new util.types(); 2552 let result = that.isFloat32Array(new Float32Array()); 2553 ``` 2554 2555 2556### isFloat64Array<sup>8+</sup> 2557 2558isFloat64Array(value: Object): boolean 2559 2560检查输入的value是否是Float64Array数组类型。 2561 2562**系统能力:** SystemCapability.Utils.Lang 2563 2564**参数:** 2565 2566| 参数名 | 类型 | 必填 | 说明 | 2567| -------- | -------- | -------- | -------- | 2568| value | Object | 是 | 待检测对象。 | 2569 2570**返回值:** 2571 2572| 类型 | 说明 | 2573| -------- | -------- | 2574| boolean | 判断的结果,如果是内置包含的Float64Array数组类型为true,反之为false。 | 2575 2576**示例:** 2577 2578 ```js 2579 let that = new util.types(); 2580 let result = that.isFloat64Array(new Float64Array()); 2581 ``` 2582 2583 2584### isGeneratorFunction<sup>8+</sup> 2585 2586isGeneratorFunction(value: Object): boolean 2587 2588检查输入的value是否是generator函数类型。 2589 2590**系统能力:** SystemCapability.Utils.Lang 2591 2592**参数:** 2593 2594| 参数名 | 类型 | 必填 | 说明 | 2595| -------- | -------- | -------- | -------- | 2596| value | Object | 是 | 待检测对象。 | 2597 2598**返回值:** 2599 2600| 类型 | 说明 | 2601| -------- | -------- | 2602| boolean | 判断的结果,如果是内置包含的generator函数类型为true,反之为false。 | 2603 2604**示例:** 2605 2606 ```js 2607 let that = new util.types(); 2608 let result = that.isGeneratorFunction(function* foo() {}); 2609 ``` 2610 2611 2612### isGeneratorObject<sup>8+</sup> 2613 2614isGeneratorObject(value: Object): boolean 2615 2616检查输入的value是否是generator对象类型。 2617 2618**系统能力:** SystemCapability.Utils.Lang 2619 2620**参数:** 2621 2622| 参数名 | 类型 | 必填 | 说明 | 2623| -------- | -------- | -------- | -------- | 2624| value | Object | 是 | 待检测对象。 | 2625 2626**返回值:** 2627 2628| 类型 | 说明 | 2629| -------- | -------- | 2630| boolean | 判断的结果,如果是内置包含的generator对象类型为true,反之为false。 | 2631 2632**示例:** 2633 2634 ```js 2635 let that = new util.types(); 2636 function* foo() {} 2637 const generator = foo(); 2638 let result = that.isGeneratorObject(generator); 2639 ``` 2640 2641 2642### isInt8Array<sup>8+</sup> 2643 2644isInt8Array(value: Object): boolean 2645 2646检查输入的value是否是Int8Array数组类型。 2647 2648**系统能力:** SystemCapability.Utils.Lang 2649 2650**参数:** 2651 2652| 参数名 | 类型 | 必填 | 说明 | 2653| -------- | -------- | -------- | -------- | 2654| value | Object | 是 | 待检测对象。 | 2655 2656**返回值:** 2657 2658| 类型 | 说明 | 2659| -------- | -------- | 2660| boolean | 判断的结果,如果是内置包含的Int8Array数组类型为true,反之为false。 | 2661 2662**示例:** 2663 2664 ```js 2665 let that = new util.types(); 2666 let result = that.isInt8Array(new Int8Array([])); 2667 ``` 2668 2669 2670### isInt16Array<sup>8+</sup> 2671 2672isInt16Array(value: Object): boolean 2673 2674检查输入的value是否是Int16Array数组类型。 2675 2676**系统能力:** SystemCapability.Utils.Lang 2677 2678**参数:** 2679 2680| 参数名 | 类型 | 必填 | 说明 | 2681| -------- | -------- | -------- | -------- | 2682| value | Object | 是 | 待检测对象。 | 2683 2684**返回值:** 2685 2686| 类型 | 说明 | 2687| -------- | -------- | 2688| boolean | 判断的结果,如果是内置包含的Int16Array数组类型为true,反之为false。 | 2689 2690**示例:** 2691 2692 ```js 2693 let that = new util.types(); 2694 let result = that.isInt16Array(new Int16Array([])); 2695 ``` 2696 2697 2698### isInt32Array<sup>8+</sup> 2699 2700isInt32Array(value: Object): boolean 2701 2702检查输入的value是否是Int32Array数组类型。 2703 2704**系统能力:** SystemCapability.Utils.Lang 2705 2706**参数:** 2707 2708| 参数名 | 类型 | 必填 | 说明 | 2709| -------- | -------- | -------- | -------- | 2710| value | Object | 是 | 待检测对象。 | 2711 2712**返回值:** 2713 2714| 类型 | 说明 | 2715| -------- | -------- | 2716| boolean | 判断的结果,如果是内置包含的Int32Array数组类型为true,反之为false。 | 2717 2718**示例:** 2719 2720 ```js 2721 let that = new util.types(); 2722 let result = that.isInt32Array(new Int32Array([])); 2723 ``` 2724 2725 2726### isMap<sup>8+</sup> 2727 2728isMap(value: Object): boolean 2729 2730检查输入的value是否是Map类型。 2731 2732**系统能力:** SystemCapability.Utils.Lang 2733 2734**参数:** 2735 2736| 参数名 | 类型 | 必填 | 说明 | 2737| -------- | -------- | -------- | -------- | 2738| value | Object | 是 | 待检测对象。 | 2739 2740**返回值:** 2741 2742| 类型 | 说明 | 2743| -------- | -------- | 2744| boolean | 判断的结果,如果是内置包含的Map类型为true,反之为false。 | 2745 2746**示例:** 2747 2748 ```js 2749 let that = new util.types(); 2750 let result = that.isMap(new Map()); 2751 ``` 2752 2753 2754### isMapIterator<sup>8+</sup> 2755 2756isMapIterator(value: Object): boolean 2757 2758检查输入的value是否是Map的Iterator类型。 2759 2760**系统能力:** SystemCapability.Utils.Lang 2761 2762**参数:** 2763 2764 2765| 参数名 | 类型 | 必填 | 说明 | 2766| -------- | -------- | -------- | -------- | 2767| value | Object | 是 | 待检测对象。 | 2768 2769**返回值:** 2770 2771| 类型 | 说明 | 2772| -------- | -------- | 2773| boolean | 判断的结果,如果是内置包含的Map的Iterator类型为true,反之为false。 | 2774 2775**示例:** 2776 2777 ```js 2778 let that = new util.types(); 2779 const map = new Map(); 2780 let result = that.isMapIterator(map.keys()); 2781 ``` 2782 2783 2784### isNativeError<sup>8+</sup> 2785 2786isNativeError(value: Object): boolean 2787 2788检查输入的value是否是Error类型。 2789 2790**系统能力:** SystemCapability.Utils.Lang 2791 2792**参数:** 2793 2794| 参数名 | 类型 | 必填 | 说明 | 2795| -------- | -------- | -------- | -------- | 2796| value | Object | 是 | 待检测对象。 | 2797 2798**返回值:** 2799 2800| 类型 | 说明 | 2801| -------- | -------- | 2802| boolean | 判断的结果,如果是内置包含的Error类型为true,反之为false。 | 2803 2804**示例:** 2805 2806 ```js 2807 let that = new util.types(); 2808 let result = that.isNativeError(new TypeError()); 2809 ``` 2810 2811 2812### isNumberObject<sup>8+</sup> 2813 2814isNumberObject(value: Object): boolean 2815 2816检查输入的value是否是Number对象类型。 2817 2818**系统能力:** SystemCapability.Utils.Lang 2819 2820**参数:** 2821 2822| 参数名 | 类型 | 必填 | 说明 | 2823| -------- | -------- | -------- | -------- | 2824| value | Object | 是 | 待检测对象。 | 2825 2826**返回值:** 2827 2828| 类型 | 说明 | 2829| -------- | -------- | 2830| boolean | 判断的结果,如果是内置包含的Number对象类型为true,反之为false。 | 2831 2832**示例:** 2833 2834 ```js 2835 let that = new util.types(); 2836 let result = that.isNumberObject(new Number(0)); 2837 ``` 2838 2839 2840### isPromise<sup>8+</sup> 2841 2842isPromise(value: Object): boolean 2843 2844检查输入的value是否是Promise类型。 2845 2846**系统能力:** SystemCapability.Utils.Lang 2847 2848**参数:** 2849 2850| 参数名 | 类型 | 必填 | 说明 | 2851| -------- | -------- | -------- | -------- | 2852| value | Object | 是 | 待检测对象。 | 2853 2854**返回值:** 2855 2856| 类型 | 说明 | 2857| -------- | -------- | 2858| boolean | 判断的结果,如果是内置包含的Promise类型为true,反之为false。 | 2859 2860**示例:** 2861 2862 ```js 2863 let that = new util.types(); 2864 let result = that.isPromise(Promise.resolve(1)); 2865 ``` 2866 2867 2868### isProxy<sup>8+</sup> 2869 2870isProxy(value: Object): boolean 2871 2872检查输入的value是否是Proxy类型。 2873 2874**系统能力:** SystemCapability.Utils.Lang 2875 2876**参数:** 2877 2878| 参数名 | 类型 | 必填 | 说明 | 2879| -------- | -------- | -------- | -------- | 2880| value | Object | 是 | 待检测对象。 | 2881 2882**返回值:** 2883 2884| 类型 | 说明 | 2885| -------- | -------- | 2886| boolean | 判断的结果,如果是内置包含的Proxy类型为true,反之为false。 | 2887 2888**示例:** 2889 2890 ```js 2891 let that = new util.types(); 2892 const target = {}; 2893 const proxy = new Proxy(target, {}); 2894 let result = that.isProxy(proxy); 2895 ``` 2896 2897 2898### isRegExp<sup>8+</sup> 2899 2900isRegExp(value: Object): boolean 2901 2902检查输入的value是否是RegExp类型。 2903 2904**系统能力:** SystemCapability.Utils.Lang 2905 2906**参数:** 2907 2908| 参数名 | 类型 | 必填 | 说明 | 2909| -------- | -------- | -------- | -------- | 2910| value | Object | 是 | 待检测对象。 | 2911 2912**返回值:** 2913 2914| 类型 | 说明 | 2915| -------- | -------- | 2916| boolean | 判断的结果,如果是内置包含的RegExp类型为true,反之为false。 | 2917 2918**示例:** 2919 2920 ```js 2921 let that = new util.types(); 2922 let result = that.isRegExp(new RegExp('abc')); 2923 ``` 2924 2925 2926### isSet<sup>8+</sup> 2927 2928isSet(value: Object): boolean 2929 2930检查输入的value是否是Set类型。 2931 2932**系统能力:** SystemCapability.Utils.Lang 2933 2934**参数:** 2935 2936| 参数名 | 类型 | 必填 | 说明 | 2937| -------- | -------- | -------- | -------- | 2938| value | Object | 是 | 待检测对象。 | 2939 2940**返回值:** 2941 2942| 类型 | 说明 | 2943| -------- | -------- | 2944| boolean | 判断的结果,如果是内置包含的Set类型为true,反之为false。 | 2945 2946**示例:** 2947 2948 ```js 2949 let that = new util.types(); 2950 let result = that.isSet(new Set()); 2951 ``` 2952 2953 2954### isSetIterator<sup>8+</sup> 2955 2956isSetIterator(value: Object): boolean 2957 2958检查输入的value是否是Set的Iterator类型。 2959 2960**系统能力:** SystemCapability.Utils.Lang 2961 2962**参数:** 2963 2964| 参数名 | 类型 | 必填 | 说明 | 2965| -------- | -------- | -------- | -------- | 2966| value | Object | 是 | 待检测对象。 | 2967 2968**返回值:** 2969 2970| 类型 | 说明 | 2971| -------- | -------- | 2972| boolean | 判断的结果,如果是内置包含的Set的Iterator类型为true,反之为false。 | 2973 2974**示例:** 2975 2976 ```js 2977 let that = new util.types(); 2978 const set = new Set(); 2979 let result = that.isSetIterator(set.keys()); 2980 ``` 2981 2982 2983### isStringObject<sup>8+</sup> 2984 2985isStringObject(value: Object): boolean 2986 2987检查输入的value是否是String对象类型。 2988 2989**系统能力:** SystemCapability.Utils.Lang 2990 2991**参数:** 2992 2993| 参数名 | 类型 | 必填 | 说明 | 2994| -------- | -------- | -------- | -------- | 2995| value | Object | 是 | 待检测对象。 | 2996 2997**返回值:** 2998 2999| 类型 | 说明 | 3000| -------- | -------- | 3001| boolean | 判断的结果,如果是内置包含的String对象类型为true,反之为false。 | 3002 3003**示例:** 3004 3005 ```js 3006 let that = new util.types(); 3007 let result = that.isStringObject(new String('foo')); 3008 ``` 3009 3010 3011### isSymbolObjec<sup>8+</sup> 3012 3013isSymbolObject(value: Object): boolean 3014 3015检查输入的value是否是Symbol对象类型。 3016 3017**系统能力:** SystemCapability.Utils.Lang 3018 3019**参数:** 3020 3021| 参数名 | 类型 | 必填 | 说明 | 3022| -------- | -------- | -------- | -------- | 3023| value | Object | 是 | 待检测对象。 | 3024 3025**返回值:** 3026 3027| 类型 | 说明 | 3028| -------- | -------- | 3029| boolean | 判断的结果,如果是内置包含的Symbol对象类型为true,反之为false。 | 3030 3031**示例:** 3032 3033 ```js 3034 let that = new util.types(); 3035 const symbols = Symbol('foo'); 3036 let result = that.isSymbolObject(Object(symbols)); 3037 ``` 3038 3039 3040### isTypedArray<sup>8+</sup> 3041 3042isTypedArray(value: Object): boolean 3043 3044检查输入的value是否是TypedArray类型的辅助类型。 3045 3046TypedArray类型的辅助类型,包括Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint16Array、Uint32Array、Float32Array、Float64Array、DataView。 3047 3048**系统能力:** SystemCapability.Utils.Lang 3049 3050**参数:** 3051 3052| 参数名 | 类型 | 必填 | 说明 | 3053| -------- | -------- | -------- | -------- | 3054| value | Object | 是 | 待检测对象。 | 3055 3056**返回值:** 3057 3058| 类型 | 说明 | 3059| -------- | -------- | 3060| boolean | 判断的结果,如果是内置包含的TypedArray包含的类型为true,反之为false。 | 3061 3062**示例:** 3063 3064 ```js 3065 let that = new util.types(); 3066 let result = that.isTypedArray(new Float64Array([])); 3067 ``` 3068 3069 3070### isUint8Array<sup>8+</sup> 3071 3072isUint8Array(value: Object): boolean 3073 3074检查输入的value是否是Uint8Array数组类型。 3075 3076**系统能力:** SystemCapability.Utils.Lang 3077 3078**参数:** 3079 3080| 参数名 | 类型 | 必填 | 说明 | 3081| -------- | -------- | -------- | -------- | 3082| value | Object | 是 | 待检测对象。 | 3083 3084**返回值:** 3085 3086| 类型 | 说明 | 3087| -------- | -------- | 3088| boolean | 判断的结果,如果是内置包含的Uint8Array数组类型为true,反之为false。 | 3089 3090**示例:** 3091 3092 ```js 3093 let that = new util.types(); 3094 let result = that.isUint8Array(new Uint8Array([])); 3095 ``` 3096 3097 3098### isUint8ClampedArray<sup>8+</sup> 3099 3100isUint8ClampedArray(value: Object): boolean 3101 3102检查输入的value是否是Uint8ClampedArray数组类型。 3103 3104**系统能力:** SystemCapability.Utils.Lang 3105 3106**参数:** 3107 3108| 参数名 | 类型 | 必填 | 说明 | 3109| -------- | -------- | -------- | -------- | 3110| value | Object | 是 | 待检测对象。 | 3111 3112**返回值:** 3113 3114| 类型 | 说明 | 3115| -------- | -------- | 3116| boolean | 判断的结果,如果是内置包含的Uint8ClampedArray数组类型为true,反之为false。 | 3117 3118**示例:** 3119 3120 ```js 3121 let that = new util.types(); 3122 let result = that.isUint8ClampedArray(new Uint8ClampedArray([])); 3123 ``` 3124 3125 3126### isUint16Array<sup>8+</sup> 3127 3128isUint16Array(value: Object): boolean 3129 3130检查输入的value是否是Uint16Array数组类型。 3131 3132**系统能力:** SystemCapability.Utils.Lang 3133 3134**参数:** 3135 3136| 参数名 | 类型 | 必填 | 说明 | 3137| -------- | -------- | -------- | -------- | 3138| value | Object | 是 | 待检测对象。 | 3139 3140**返回值:** 3141 3142| 类型 | 说明 | 3143| -------- | -------- | 3144| boolean | 判断的结果,如果是内置包含的Uint16Array数组类型为true,反之为false。 | 3145 3146**示例:** 3147 3148 ```js 3149 let that = new util.types(); 3150 let result = that.isUint16Array(new Uint16Array([])); 3151 ``` 3152 3153 3154### isUint32Array<sup>8+</sup> 3155 3156isUint32Array(value: Object): boolean 3157 3158检查输入的value是否是Uint32Array数组类型。 3159 3160**系统能力:** SystemCapability.Utils.Lang 3161 3162**参数:** 3163 3164| 参数名 | 类型 | 必填 | 说明 | 3165| -------- | -------- | -------- | -------- | 3166| value | Object | 是 | 待检测对象。 | 3167 3168**返回值:** 3169 3170| 类型 | 说明 | 3171| -------- | -------- | 3172| boolean | 判断的结果,如果是内置包含的Uint32Array数组类型为true,反之为false。 | 3173 3174**示例:** 3175 3176 ```js 3177 let that = new util.types(); 3178 let result = that.isUint32Array(new Uint32Array([])); 3179 ``` 3180 3181 3182### isWeakMap<sup>8+</sup> 3183 3184isWeakMap(value: Object): boolean 3185 3186检查输入的value是否是WeakMap类型。 3187 3188**系统能力:** SystemCapability.Utils.Lang 3189 3190**参数:** 3191 3192| 参数名 | 类型 | 必填 | 说明 | 3193| -------- | -------- | -------- | -------- | 3194| value | Object | 是 | 待检测对象。 | 3195 3196**返回值:** 3197 3198| 类型 | 说明 | 3199| -------- | -------- | 3200| boolean | 判断的结果,如果是内置包含的WeakMap类型为true,反之为false。 | 3201 3202**示例:** 3203 3204 ```js 3205 let that = new util.types(); 3206 let result = that.isWeakMap(new WeakMap()); 3207 ``` 3208 3209 3210### isWeakSet<sup>8+</sup> 3211 3212isWeakSet(value: Object): boolean 3213 3214检查输入的value是否是WeakSet类型。 3215 3216**系统能力:** SystemCapability.Utils.Lang 3217 3218**参数:** 3219 3220| 参数名 | 类型 | 必填 | 说明 | 3221| -------- | -------- | -------- | -------- | 3222| value | Object | 是 | 待检测对象。 | 3223 3224**返回值:** 3225 3226| 类型 | 说明 | 3227| -------- | -------- | 3228| boolean | 判断的结果,如果是内置包含的WeakSet类型为true,反之为false。 | 3229 3230**示例:** 3231 3232 ```js 3233 let that = new util.types(); 3234 let result = that.isWeakSet(new WeakSet()); 3235 ``` 3236 3237 3238### isBigInt64Array<sup>8+</sup> 3239 3240isBigInt64Array(value: Object): boolean 3241 3242检查输入的value是否是BigInt64Array类型。 3243 3244**系统能力:** SystemCapability.Utils.Lang 3245 3246**参数:** 3247 3248| 参数名 | 类型 | 必填 | 说明 | 3249| -------- | -------- | -------- | -------- | 3250| value | Object | 是 | 待检测对象。 | 3251 3252**返回值:** 3253 3254| 类型 | 说明 | 3255| -------- | -------- | 3256| boolean | 判断的结果,如果是内置包含的BigInt64Array类型为true,反之为false。 | 3257 3258**示例:** 3259 3260 ```js 3261 let that = new util.types(); 3262 let result = that.isBigInt64Array(new BigInt64Array([])); 3263 ``` 3264 3265 3266### isBigUint64Array<sup>8+</sup> 3267 3268isBigUint64Array(value: Object): boolean 3269 3270检查输入的value是否是BigUint64Array类型。 3271 3272**系统能力:** SystemCapability.Utils.Lang 3273 3274**参数:** 3275 3276| 参数名 | 类型 | 必填 | 说明 | 3277| -------- | -------- | -------- | -------- | 3278| value | Object | 是 | 待检测对象。 | 3279 3280**返回值:** 3281 3282| 类型 | 说明 | 3283| -------- | -------- | 3284| boolean | 判断的结果,如果是内置包含的BigUint64Array类型为true,反之为false。 | 3285 3286**示例:** 3287 3288 ```js 3289 let that = new util.types(); 3290 let result = that.isBigUint64Array(new BigUint64Array([])); 3291 ``` 3292 3293 3294### isModuleNamespaceObject<sup>8+</sup> 3295 3296isModuleNamespaceObject(value: Object): boolean 3297 3298检查输入的value是否是Module Namespace Object类型。 3299 3300**系统能力:** SystemCapability.Utils.Lang 3301 3302**参数:** 3303 3304| 参数名 | 类型 | 必填 | 说明 | 3305| -------- | -------- | -------- | -------- | 3306| value | Object | 是 | 待检测对象。 | 3307 3308**返回值:** 3309 3310| 类型 | 说明 | 3311| -------- | -------- | 3312| boolean | 判断的结果,如果是内置包含的Module Namespace Object类型为true,反之为false。 | 3313 3314**示例:** 3315 3316 ```js 3317 import url from '@ohos.url' 3318 let that = new util.types(); 3319 let result = that.isModuleNamespaceObject(url); 3320 ``` 3321 3322 3323### isSharedArrayBuffer<sup>8+</sup> 3324 3325isSharedArrayBuffer(value: Object): boolean 3326 3327检查输入的value是否是SharedArrayBuffer类型。 3328 3329**系统能力:** SystemCapability.Utils.Lang 3330 3331**参数:** 3332 3333| 参数名 | 类型 | 必填 | 说明 | 3334| -------- | -------- | -------- | -------- | 3335| value | Object | 是 | 待检测对象。 | 3336 3337**返回值:** 3338 3339| 类型 | 说明 | 3340| -------- | -------- | 3341| boolean | 判断的结果,如果是内置包含的SharedArrayBuffer类型为true,反之为false。 | 3342 3343**示例:** 3344 3345 ```js 3346 let that = new util.types(); 3347 let result = that.isSharedArrayBuffer(new SharedArrayBuffer(0)); 3348 ``` 3349 3350## LruBuffer<sup>(deprecated)</sup> 3351 3352> **说明:** 3353> 3354> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache<sup>9+</sup>](#lrucache9)替代。 3355 3356### 属性 3357 3358**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 3359 3360| 名称 | 类型 | 可读 | 可写 | 说明 | 3361| -------- | -------- | -------- | -------- | -------- | 3362| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 3363 3364**示例:** 3365 3366 ```js 3367 let pro = new util.LruBuffer(); 3368 pro.put(2,10); 3369 pro.put(1,8); 3370 let result = pro.length; 3371 ``` 3372 3373### constructor<sup>(deprecated)</sup> 3374 3375constructor(capacity?: number) 3376 3377默认构造函数用于创建一个新的LruBuffer实例,默认容量为64。 3378 3379> **说明:** 3380> 3381> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.constructor<sup>9+</sup>](#constructor9-3)替代。 3382 3383**系统能力:** SystemCapability.Utils.Lang 3384 3385**参数:** 3386 3387| 参数名 | 类型 | 必填 | 说明 | 3388| -------- | -------- | -------- | -------- | 3389| capacity | number | 否 | 指示要为缓冲区自定义的容量。 | 3390 3391**示例:** 3392 3393 ```js 3394 let lrubuffer= new util.LruBuffer(); 3395 ``` 3396 3397### updateCapacity<sup>(deprecated)</sup> 3398 3399updateCapacity(newCapacity: number): void 3400 3401将缓冲区容量更新为指定容量,如果newCapacity小于或等于0,则抛出异常。 3402 3403> **说明:** 3404> 3405> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.updateCapacity<sup>9+</sup>](#updatecapacity9)替代。 3406 3407**系统能力:** SystemCapability.Utils.Lang 3408 3409**参数:** 3410 3411| 参数名 | 类型 | 必填 | 说明 | 3412| -------- | -------- | -------- | -------- | 3413| newCapacity | number | 是 | 指示要为缓冲区自定义的容量。 | 3414 3415**示例:** 3416 3417 ```js 3418 let pro = new util.LruBuffer(); 3419 let result = pro.updateCapacity(100); 3420 ``` 3421 3422### toString<sup>(deprecated)</sup> 3423 3424toString(): string 3425 3426返回对象的字符串表示形式。 3427 3428> **说明:** 3429> 3430> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.toString<sup>9+</sup>](#tostring9)替代。 3431 3432**系统能力:** SystemCapability.Utils.Lang 3433 3434**返回值:** 3435 3436| 类型 | 说明 | 3437| -------- | -------- | 3438| string | 返回对象的字符串表示形式。 | 3439 3440**示例:** 3441 3442 ```js 3443 let pro = new util.LruBuffer(); 3444 pro.put(2,10); 3445 pro.get(2); 3446 pro.remove(20); 3447 let result = pro.toString(); 3448 ``` 3449 3450### getCapacity<sup>(deprecated)</sup> 3451 3452getCapacity(): number 3453 3454获取当前缓冲区的容量。 3455 3456> **说明:** 3457> 3458> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCapacity<sup>9+</sup>](#getcapacity9)替代。 3459 3460**系统能力:** SystemCapability.Utils.Lang 3461 3462**返回值:** 3463 3464| 类型 | 说明 | 3465| -------- | -------- | 3466| number | 返回当前缓冲区的容量。 | 3467 3468**示例:** 3469 ```js 3470 let pro = new util.LruBuffer(); 3471 let result = pro.getCapacity(); 3472 ``` 3473 3474### clear<sup>(deprecated)</sup> 3475 3476clear(): void 3477 3478从当前缓冲区清除键值对。后续会调用afterRemoval()方法执行后续操作。 3479 3480> **说明:** 3481> 3482> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.clear<sup>9+</sup>](#clear9)替代。 3483 3484**系统能力:** SystemCapability.Utils.Lang 3485 3486**示例:** 3487 3488 ```js 3489 let pro = new util.LruBuffer(); 3490 pro.put(2,10); 3491 let result = pro.length; 3492 pro.clear(); 3493 ``` 3494 3495### getCreateCount<sup>(deprecated)</sup> 3496 3497getCreateCount(): number 3498 3499获取createDefault()返回值的次数。 3500 3501> **说明:** 3502> 3503> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCreateCount<sup>9+</sup>](#getcreatecount9)替代。 3504 3505**系统能力:** SystemCapability.Utils.Lang 3506 3507**返回值:** 3508 3509| 类型 | 说明 | 3510| -------- | -------- | 3511| number | 返回createDefault()返回值的次数。 | 3512 3513**示例:** 3514 3515 ```js 3516 let pro = new util.LruBuffer(); 3517 pro.put(1,8); 3518 let result = pro.getCreateCount(); 3519 ``` 3520 3521### getMissCount<sup>(deprecated)</sup> 3522 3523getMissCount(): number 3524 3525获取查询值不匹配的次数。 3526 3527> **说明:** 3528> 3529> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMissCount<sup>9+</sup>](#getmisscount9)替代。 3530 3531**系统能力:** SystemCapability.Utils.Lang 3532 3533**返回值:** 3534 3535| 类型 | 说明 | 3536| -------- | -------- | 3537| number | 返回查询值不匹配的次数。 | 3538 3539**示例:** 3540 3541 ```js 3542 let pro = new util.LruBuffer(); 3543 pro.put(2,10); 3544 pro.get(2); 3545 let result = pro.getMissCount(); 3546 ``` 3547 3548### getRemovalCount<sup>(deprecated)</sup> 3549 3550getRemovalCount(): number 3551 3552获取从缓冲区中逐出值的次数。 3553 3554> **说明:** 3555> 3556> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getRemovalCount<sup>9+</sup>](#getremovalcount9)替代。 3557 3558**系统能力:** SystemCapability.Utils.Lang 3559 3560**返回值:** 3561 3562| 类型 | 说明 | 3563| -------- | -------- | 3564| number | 返回从缓冲区中驱逐的次数。 | 3565 3566**示例:** 3567 3568 ```js 3569 let pro = new util.LruBuffer(); 3570 pro.put(2,10); 3571 pro.updateCapacity(2); 3572 pro.put(50,22); 3573 let result = pro.getRemovalCount(); 3574 ``` 3575 3576### getMatchCount<sup>(deprecated)</sup> 3577 3578getMatchCount(): number 3579 3580获取查询值匹配成功的次数。 3581 3582> **说明:** 3583> 3584> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMatchCount<sup>9+</sup>](#getmatchcount9)替代。 3585 3586**系统能力:** SystemCapability.Utils.Lang 3587 3588**返回值:** 3589 3590| 类型 | 说明 | 3591| -------- | -------- | 3592| number | 返回查询值匹配成功的次数。 | 3593 3594**示例:** 3595 3596 ```js 3597 let pro = new util.LruBuffer(); 3598 pro.put(2,10); 3599 pro.get(2); 3600 let result = pro.getMatchCount(); 3601 ``` 3602 3603### getPutCount<sup>(deprecated)</sup> 3604 3605getPutCount(): number 3606 3607获取将值添加到缓冲区的次数。 3608 3609> **说明:** 3610> 3611> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getPutCount<sup>9+</sup>](#getputcount9)替代。 3612 3613**系统能力:** SystemCapability.Utils.Lang 3614 3615**返回值:** 3616 3617| 类型 | 说明 | 3618| -------- | -------- | 3619| number | 返回将值添加到缓冲区的次数。 | 3620 3621**示例:** 3622 3623 ```js 3624 let pro = new util.LruBuffer(); 3625 pro.put(2,10); 3626 let result = pro.getPutCount(); 3627 ``` 3628 3629### isEmpty<sup>(deprecated)</sup> 3630 3631isEmpty(): boolean 3632 3633检查当前缓冲区是否为空。 3634 3635> **说明:** 3636> 3637> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.isEmpty<sup>9+</sup>](#isempty9)替代。 3638 3639**系统能力:** SystemCapability.Utils.Lang 3640 3641**返回值:** 3642 3643| 类型 | 说明 | 3644| -------- | -------- | 3645| boolean | 如果当前缓冲区不包含任何值,则返回true。 | 3646 3647**示例:** 3648 3649 ```js 3650 let pro = new util.LruBuffer(); 3651 pro.put(2,10); 3652 let result = pro.isEmpty(); 3653 ``` 3654 3655### get<sup>(deprecated)</sup> 3656 3657get(key: K): V | undefined 3658 3659表示要查询的键。 3660 3661> **说明:** 3662> 3663> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.get<sup>9+</sup>](#get9)替代。 3664 3665**系统能力:** SystemCapability.Utils.Lang 3666 3667**参数:** 3668 3669| 参数名 | 类型 | 必填 | 说明 | 3670| -------- | -------- | -------- | -------- | 3671| key | K | 是 | 要查询的键。 | 3672 3673**返回值:** 3674 3675| 类型 | 说明 | 3676| -------- | -------- | 3677| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回undefined。 | 3678 3679**示例:** 3680 3681 ```js 3682 let pro = new util.LruBuffer(); 3683 pro.put(2,10); 3684 let result = pro.get(2); 3685 ``` 3686 3687### put<sup>(deprecated)</sup> 3688 3689put(key: K,value: V): V 3690 3691将键值对添加到缓冲区。 3692 3693> **说明:** 3694> 3695> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.put<sup>9+</sup>](#put9)替代。 3696 3697**系统能力:** SystemCapability.Utils.Lang 3698 3699**参数:** 3700 3701| 参数名 | 类型 | 必填 | 说明 | 3702| -------- | -------- | -------- | -------- | 3703| key | K | 是 | 要添加的密钥。 | 3704| value | V | 是 | 指示与要添加的键关联的值。 | 3705 3706**返回值:** 3707 3708| 类型 | 说明 | 3709| -------- | -------- | 3710| V | 返回与添加的键关联的值;如果要添加的键已经存在,则返回原始值,如果键或值为空,则抛出此异常。 | 3711 3712**示例:** 3713 3714 ```js 3715 let pro = new util.LruBuffer(); 3716 let result = pro.put(2,10); 3717 ``` 3718 3719### values<sup>(deprecated)</sup> 3720 3721values(): V[] 3722 3723获取当前缓冲区中所有值从最近访问到最近最少访问的顺序列表。 3724 3725> **说明:** 3726> 3727> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.values<sup>9+</sup>](#values9)替代。 3728 3729**系统能力:** SystemCapability.Utils.Lang 3730 3731**返回值:** 3732 3733| 类型 | 说明 | 3734| -------- | -------- | 3735| V [] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 3736 3737**示例:** 3738 3739 ```js 3740 let pro = new util.LruBuffer(); 3741 pro.put(2,10); 3742 pro.put(2,"anhu"); 3743 pro.put("afaf","grfb"); 3744 let result = pro.values(); 3745 ``` 3746 3747### keys<sup>(deprecated)</sup> 3748 3749keys(): K[] 3750 3751获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 3752 3753> **说明:** 3754> 3755> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.keys<sup>9+</sup>](#keys9)替代。 3756 3757**系统能力:** SystemCapability.Utils.Lang 3758 3759**返回值:** 3760 3761| 类型 | 说明 | 3762| -------- | -------- | 3763| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 3764 3765**示例:** 3766 3767 ```js 3768 let pro = new util.LruBuffer(); 3769 pro.put(2,10); 3770 let result = pro.keys(); 3771 ``` 3772 3773### remove<sup>(deprecated)</sup> 3774 3775remove(key: K): V | undefined 3776 3777从当前缓冲区中删除指定的键及其关联的值。 3778 3779> **说明:** 3780> 3781> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.remove<sup>9+</sup>](#remove9)替代。 3782 3783**系统能力:** SystemCapability.Utils.Lang 3784 3785**参数:** 3786 3787| 参数名 | 类型 | 必填 | 说明 | 3788| -------- | -------- | -------- | -------- | 3789| key | K | 是 | 要删除的密钥。 | 3790 3791**返回值:** 3792 3793| 类型 | 说明 | 3794| -------- | -------- | 3795| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回一个空的Optional对象,如果key为null,则抛出异常。 | 3796 3797**示例:** 3798 ```js 3799 let pro = new util.LruBuffer(); 3800 pro.put(2,10); 3801 let result = pro.remove(20); 3802 ``` 3803 3804### afterRemoval<sup>(deprecated)</sup> 3805 3806afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 3807 3808删除值后执行后续操作。 3809 3810> **说明:** 3811> 3812> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.afterRemoval<sup>9+</sup>](#afterremoval9)替代。 3813 3814**系统能力:** SystemCapability.Utils.Lang 3815 3816**参数:** 3817 3818| 参数名 | 类型 | 必填 | 说明 | 3819| -------- | -------- | -------- | -------- | 3820| isEvict | boolean | 是 | 因容量不足而调用该方法时,参数值为true,其他情况为false。 | 3821| key | K | 是 | 表示删除的键。 | 3822| value | V | 是 | 表示删除的值。 | 3823| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 3824 3825**示例:** 3826 3827 ```js 3828 let arr = []; 3829 class ChildLruBuffer<K, V> extends util.LruBuffer<K, V> 3830 { 3831 constructor() 3832 { 3833 super(); 3834 } 3835 afterRemoval(isEvict, key, value, newValue) 3836 { 3837 if (isEvict === false) 3838 { 3839 arr = [key, value, newValue]; 3840 } 3841 } 3842 } 3843 let lru = new ChildLruBuffer(); 3844 lru.afterRemoval(false,10,30,null); 3845 ``` 3846 3847### contains<sup>(deprecated)</sup> 3848 3849contains(key: K): boolean 3850 3851检查当前缓冲区是否包含指定的键。 3852 3853 3854> **说明:** 3855> 3856> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.contains<sup>9+</sup>](#contains9)替代。 3857 3858**系统能力:** SystemCapability.Utils.Lang 3859 3860**参数:** 3861 3862| 参数名 | 类型 | 必填 | 说明 | 3863| -------- | -------- | -------- | -------- | 3864| key | K | 是 | 表示要检查的键。 | 3865 3866**返回值:** 3867 3868| 类型 | 说明 | 3869| -------- | -------- | 3870| boolean | 如果缓冲区包含指定的键,则返回 true。 | 3871 3872**示例:** 3873 3874 ```js 3875 let pro = new util.LruBuffer(); 3876 pro.put(2,10); 3877 let result = pro.contains(20); 3878 ``` 3879 3880### createDefault<sup>(deprecated)</sup> 3881 3882createDefault(key: K): V 3883 3884如果未计算特定键的值,则执行后续操作,参数表示丢失的键,返回与键关联的值。 3885 3886> **说明:** 3887> 3888> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.createDefault<sup>9+</sup>](#createdefault9)替代。 3889 3890**系统能力:** SystemCapability.Utils.Lang 3891 3892**参数:** 3893 3894| 参数名 | 类型 | 必填 | 说明 | 3895| -------- | -------- | -------- | -------- | 3896| key | K | 是 | 表示丢失的键。 | 3897 3898**返回值:** 3899 3900| 类型 | 说明 | 3901| -------- | -------- | 3902| V | 返回与键关联的值。 | 3903 3904**示例:** 3905 3906 ```js 3907 let pro = new util.LruBuffer(); 3908 let result = pro.createDefault(50); 3909 ``` 3910 3911### entries<sup>(deprecated)</sup> 3912 3913entries(): IterableIterator<[K,V]> 3914 3915允许迭代包含在这个对象中的所有键值对。 3916 3917> **说明:** 3918> 3919> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.entries<sup>9+</sup>](#entries9)替代。 3920 3921**系统能力:** SystemCapability.Utils.Lang 3922 3923**返回值:** 3924 3925| 类型 | 说明 | 3926| -------- | -------- | 3927| [K, V] | 返回一个可迭代数组。 | 3928 3929**示例:** 3930 3931 ```js 3932 let pro = new util.LruBuffer(); 3933 pro.put(2,10); 3934 let result = pro.entries(); 3935 ``` 3936 3937### [Symbol.iterator]<sup>(deprecated)</sup> 3938 3939[Symbol.iterator]\(): IterableIterator<[K, V]> 3940 3941返回一个键值对形式的二维数组。 3942 3943> **说明:** 3944> 3945> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.Symbol.iterator<sup>9+</sup>](#symboliterator9)替代。 3946 3947**系统能力:** SystemCapability.Utils.Lang 3948 3949**返回值:** 3950 3951| 类型 | 说明 | 3952| -------- | -------- | 3953| [K, V] | 返回一个键值对形式的二维数组。 | 3954 3955**示例:** 3956 3957 ```js 3958 let pro = new util.LruBuffer(); 3959 pro.put(2,10); 3960 let result = pro[Symbol.iterator](); 3961 ``` 3962 3963## Scope<sup>(deprecated)</sup> 3964 3965> **说明:** 3966> 3967> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper<sup>9+</sup>](#scopehelper9)替代。 3968 3969### constructor<sup>(deprecated)</sup> 3970 3971constructor(lowerObj: ScopeType, upperObj: ScopeType) 3972 3973用于创建指定下限和上限的作用域实例的构造函数,返回一个Scope对象。 3974 3975> **说明:** 3976> 3977> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.constructor<sup>9+</sup>](#constructor9-4)替代。 3978 3979 3980**系统能力:** SystemCapability.Utils.Lang 3981 3982**参数:** 3983 3984| 参数名 | 类型 | 必填 | 说明 | 3985| -------- | -------- | -------- | -------- | 3986| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 3987| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 3988 3989**示例:** 3990 ```js 3991 let tempLower = new Temperature(30); 3992 let tempUpper = new Temperature(40); 3993 let range = new util.Scope(tempLower, tempUpper); 3994 ``` 3995 3996### toString<sup>(deprecated)</sup> 3997 3998toString(): string 3999 4000该字符串化方法返回一个包含当前范围的字符串表示形式。 4001 4002> **说明:** 4003> 4004> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.toString<sup>9+</sup>](#tostring9-1)替代。 4005 4006**系统能力:** SystemCapability.Utils.Lang 4007 4008**返回值:** 4009 4010| 类型 | 说明 | 4011| -------- | -------- | 4012| string | 返回包含当前范围对象的字符串表示形式。 | 4013 4014**示例:** 4015 4016 ```js 4017 let tempLower = new Temperature(30); 4018 let tempUpper = new Temperature(40); 4019 let range = new util.Scope(tempLower, tempUpper); 4020 let result = range.toString(); 4021 ``` 4022 4023### intersect<sup>(deprecated)</sup> 4024 4025intersect(range: Scope): Scope 4026 4027获取给定范围和当前范围的交集。 4028 4029> **说明:** 4030> 4031> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9)替代。 4032 4033**系统能力:** SystemCapability.Utils.Lang 4034 4035**参数:** 4036 4037| 参数名 | 类型 | 必填 | 说明 | 4038| -------- | -------- | -------- | -------- | 4039| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 4040 4041**返回值:** 4042 4043| 类型 | 说明 | 4044| -------- | -------- | 4045| [Scope](#scopedeprecated) | 返回给定范围和当前范围的交集。 | 4046 4047**示例:** 4048 4049 ```js 4050 let tempLower = new Temperature(30); 4051 let tempUpper = new Temperature(40); 4052 let range = new util.Scope(tempLower, tempUpper); 4053 let tempMiDF = new Temperature(35); 4054 let tempMidS = new Temperature(39); 4055 let rangeFir = new util.Scope(tempMiDF, tempMidS); 4056 range.intersect(rangeFir ); 4057 ``` 4058 4059### intersect<sup>(deprecated)</sup> 4060 4061intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope 4062 4063获取当前范围与给定下限和上限范围的交集。 4064 4065> **说明:** 4066> 4067> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9-1)替代。 4068 4069**系统能力:** SystemCapability.Utils.Lang 4070 4071**参数:** 4072 4073| 参数名 | 类型 | 必填 | 说明 | 4074| -------- | -------- | -------- | -------- | 4075| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 4076| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 4077 4078**返回值:** 4079 4080| 类型 | 说明 | 4081| -------- | -------- | 4082| [Scope](#scopedeprecated) | 返回当前范围与给定下限和上限范围的交集。 | 4083 4084**示例:** 4085 4086 ```js 4087 let tempLower = new Temperature(30); 4088 let tempUpper = new Temperature(40); 4089 let tempMiDF = new Temperature(35); 4090 let tempMidS = new Temperature(39); 4091 let range = new util.Scope(tempLower, tempUpper); 4092 let result = range.intersect(tempMiDF, tempMidS); 4093 ``` 4094 4095### getUpper<sup>(deprecated)</sup> 4096 4097getUpper(): ScopeType 4098 4099获取当前范围的上限。 4100 4101> **说明:** 4102> 4103> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getUpper<sup>9+</sup>](#getupper9)替代。 4104 4105**系统能力:** SystemCapability.Utils.Lang 4106 4107**返回值:** 4108 4109| 类型 | 说明 | 4110| -------- | -------- | 4111| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 4112 4113**示例:** 4114 4115 ```js 4116 let tempLower = new Temperature(30); 4117 let tempUpper = new Temperature(40); 4118 let range = new util.Scope(tempLower, tempUpper); 4119 let result = range.getUpper(); 4120 ``` 4121 4122### getLower<sup>(deprecated)</sup> 4123 4124getLower(): ScopeType 4125 4126获取当前范围的下限。 4127 4128> **说明:** 4129> 4130> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getLower<sup>9+</sup>](#getlower9)替代。 4131 4132**系统能力:** SystemCapability.Utils.Lang 4133 4134**返回值:** 4135 4136| 类型 | 说明 | 4137| -------- | -------- | 4138| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 4139 4140**示例:** 4141 4142 ```js 4143 let tempLower = new Temperature(30); 4144 let tempUpper = new Temperature(40); 4145 let range = new util.Scope(tempLower, tempUpper); 4146 let result = range.getLower(); 4147 ``` 4148 4149### expand<sup>(deprecated)</sup> 4150 4151expand(lowerObj: ScopeType,upperObj: ScopeType): Scope 4152 4153创建并返回包括当前范围和给定下限和上限的并集。 4154 4155> **说明:** 4156> 4157> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9)替代。 4158 4159**系统能力:** SystemCapability.Utils.Lang 4160 4161**参数:** 4162 4163| 参数名 | 类型 | 必填 | 说明 | 4164| -------- | -------- | -------- | -------- | 4165| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 4166| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 4167 4168**返回值:** 4169 4170| 类型 | 说明 | 4171| -------- | -------- | 4172| [Scope](#scopedeprecated) | 返回当前范围和给定下限和上限的并集。 | 4173 4174**示例:** 4175 4176 ```js 4177 let tempLower = new Temperature(30); 4178 let tempUpper = new Temperature(40); 4179 let tempMiDF = new Temperature(35); 4180 let tempMidS = new Temperature(39); 4181 let range = new util.Scope(tempLower, tempUpper); 4182 let result = range.expand(tempMiDF, tempMidS); 4183 ``` 4184 4185### expand<sup>(deprecated)</sup> 4186 4187expand(range: Scope): Scope 4188 4189创建并返回包括当前范围和给定范围的并集。 4190 4191> **说明:** 4192> 4193> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-1)替代。 4194 4195**系统能力:** SystemCapability.Utils.Lang 4196 4197**参数:** 4198 4199| 参数名 | 类型 | 必填 | 说明 | 4200| -------- | -------- | -------- | -------- | 4201| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 4202 4203**返回值:** 4204 4205| 类型 | 说明 | 4206| -------- | -------- | 4207| [Scope](#scopedeprecated) | 返回包括当前范围和给定范围的并集。 | 4208 4209**示例:** 4210 4211 ```js 4212 let tempLower = new Temperature(30); 4213 let tempUpper = new Temperature(40); 4214 let tempMiDF = new Temperature(35); 4215 let tempMidS = new Temperature(39); 4216 let range = new util.Scope(tempLower, tempUpper); 4217 let rangeFir = new util.Scope(tempMiDF, tempMidS); 4218 let result = range.expand(rangeFir); 4219 ``` 4220 4221### expand<sup>(deprecated)</sup> 4222 4223expand(value: ScopeType): Scope 4224 4225创建并返回包括当前范围和给定值的并集。 4226 4227> **说明:** 4228> 4229> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-2)替代。 4230 4231**系统能力:** SystemCapability.Utils.Lang 4232 4233**参数:** 4234 4235| 参数名 | 类型 | 必填 | 说明 | 4236| -------- | -------- | -------- | -------- | 4237| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 4238 4239**返回值:** 4240 4241| 类型 | 说明 | 4242| -------- | -------- | 4243| [Scope](#scopedeprecated) | 返回包括当前范围和给定值的并集。 | 4244 4245**示例:** 4246 4247 ```js 4248 let tempLower = new Temperature(30); 4249 let tempUpper = new Temperature(40); 4250 let tempMiDF = new Temperature(35); 4251 let range = new util.Scope(tempLower, tempUpper); 4252 let result = range.expand(tempMiDF); 4253 ``` 4254 4255### contains<sup>(deprecated)</sup> 4256 4257contains(value: ScopeType): boolean 4258 4259检查给定value是否包含在当前范围内。 4260 4261> **说明:** 4262> 4263> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-1)替代。 4264 4265**系统能力:** SystemCapability.Utils.Lang 4266 4267**参数:** 4268 4269| 参数名 | 类型 | 必填 | 说明 | 4270| -------- | -------- | -------- | -------- | 4271| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 4272 4273**返回值:** 4274 4275| 类型 | 说明 | 4276| -------- | -------- | 4277| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 4278 4279**示例:** 4280 4281 ```js 4282 let tempLower = new Temperature(30); 4283 let tempUpper = new Temperature(40); 4284 let tempMiDF = new Temperature(35); 4285 let range = new util.Scope(tempLower, tempUpper); 4286 range.contains(tempMiDF); 4287 ``` 4288 4289### contains<sup>(deprecated)</sup> 4290 4291contains(range: Scope): boolean 4292 4293检查给定range是否在当前范围内。 4294 4295> **说明:** 4296> 4297> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-2)替代。 4298 4299**系统能力:** SystemCapability.Utils.Lang 4300 4301**参数:** 4302 4303| 参数名 | 类型 | 必填 | 说明 | 4304| -------- | -------- | -------- | -------- | 4305| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 4306 4307**返回值:** 4308 4309| 类型 | 说明 | 4310| -------- | -------- | 4311| boolean | 如果给定范围包含在当前范围内返回true,否则返回false。 | 4312 4313**示例:** 4314 4315 ```js 4316 let tempLower = new Temperature(30); 4317 let tempUpper = new Temperature(40); 4318 let range = new util.Scope(tempLower, tempUpper); 4319 let tempLess = new Temperature(20); 4320 let tempMore = new Temperature(45); 4321 let rangeSec = new util.Scope(tempLess, tempMore); 4322 let result = range.contains(rangeSec); 4323 ``` 4324 4325### clamp<sup>(deprecated)</sup> 4326 4327 4328clamp(value: ScopeType): ScopeType 4329 4330将给定值限定到当前范围内。 4331 4332> **说明:** 4333> 4334> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.clamp<sup>9+</sup>](#clamp9)替代。 4335 4336**系统能力:** SystemCapability.Utils.Lang 4337 4338**参数:** 4339 4340| 参数名 | 类型 | 必填 | 说明 | 4341| -------- | -------- | -------- | -------- | 4342| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 4343 4344**返回值:** 4345 4346| 类型 | 说明 | 4347| -------- | -------- | 4348| [ScopeType](#scopetype8) | 如果传入的value小于下限,则返回lowerObj;如果大于上限值则返回upperObj;如果在当前范围内,则返回value。 | 4349 4350**示例:** 4351 4352 ```js 4353 let tempLower = new Temperature(30); 4354 let tempUpper = new Temperature(40); 4355 let tempMiDF = new Temperature(35); 4356 let range = new util.Scope(tempLower, tempUpper); 4357 let result = range.clamp(tempMiDF); 4358 ``` 4359 4360 4361## Base64<sup>(deprecated)</sup> 4362 4363> **说明:** 4364> 4365> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper<sup>9+</sup>](#base64helper9)替代。 4366 4367### constructor<sup>(deprecated)</sup> 4368 4369constructor() 4370 4371Base64的构造函数。 4372 4373> **说明:** 4374> 4375> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.constructor<sup>9+</sup>](#constructor9-5)替代。 4376 4377**系统能力:** SystemCapability.Utils.Lang 4378 4379**示例:** 4380 4381 ```js 4382 let base64 = new util.Base64(); 4383 ``` 4384 4385### encodeSync<sup>(deprecated)</sup> 4386 4387encodeSync(src: Uint8Array): Uint8Array 4388 4389通过输入参数编码后输出对应文本。 4390 4391> **说明:** 4392> 4393> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeSync<sup>9+</sup>](#encodesync9)替代。 4394 4395**系统能力:** SystemCapability.Utils.Lang 4396 4397**参数:** 4398 4399| 参数名 | 类型 | 必填 | 说明 | 4400| -------- | -------- | -------- | -------- | 4401| src | Uint8Array | 是 | 编码输入Uint8数组。 | 4402 4403**返回值:** 4404 4405| 类型 | 说明 | 4406| -------- | -------- | 4407| Uint8Array | 返回编码后新分配的Uint8数组。 | 4408 4409**示例:** 4410 4411 ```js 4412 let that = new util.Base64(); 4413 let array = new Uint8Array([115,49,51]); 4414 let result = that.encodeSync(array); 4415 ``` 4416 4417### encodeToStringSync<sup>(deprecated)</sup> 4418 4419encodeToStringSync(src: Uint8Array): string 4420 4421通过输入参数编码后输出对应文本。 4422 4423> **说明:** 4424> 4425> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToStringSync<sup>9+</sup>](#encodetostringsync9)替代。 4426 4427**系统能力:** SystemCapability.Utils.Lang 4428 4429**参数:** 4430 4431| 参数名 | 类型 | 必填 | 说明 | 4432| -------- | -------- | -------- | -------- | 4433| src | Uint8Array | 是 | 编码输入Uint8数组。 | 4434 4435**返回值:** 4436 4437| 类型 | 说明 | 4438| -------- | -------- | 4439| string | 返回编码后的字符串。 | 4440 4441**示例:** 4442 4443 ```js 4444 let that = new util.Base64(); 4445 let array = new Uint8Array([115,49,51]); 4446 let result = that.encodeToStringSync(array); 4447 ``` 4448 4449### decodeSync<sup>(deprecated)</sup> 4450 4451decodeSync(src: Uint8Array | string): Uint8Array 4452 4453通过输入参数解码后输出对应文本。 4454 4455> **说明:** 4456> 4457> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decodeSync<sup>9+</sup>](#decodesync9)替代。 4458 4459**系统能力:** SystemCapability.Utils.Lang 4460 4461**参数:** 4462 4463| 参数名 | 类型 | 必填 | 说明 | 4464| -------- | -------- | -------- | -------- | 4465| src | Uint8Array \| string | 是 | 解码输入Uint8数组或者字符串。 | 4466 4467**返回值:** 4468 4469| 类型 | 说明 | 4470| -------- | -------- | 4471| Uint8Array | 返回解码后新分配的Uint8数组。 | 4472 4473**示例:** 4474 4475 ```js 4476 let that = new util.Base64(); 4477 let buff = 'czEz'; 4478 let result = that.decodeSync(buff); 4479 ``` 4480 4481### encode<sup>(deprecated)</sup> 4482 4483encode(src: Uint8Array): Promise<Uint8Array> 4484 4485通过输入参数异步编码后输出对应文本。 4486 4487> **说明:** 4488> 4489> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encode<sup>9+</sup>](#encode9)替代。 4490 4491**系统能力:** SystemCapability.Utils.Lang 4492 4493**参数:** 4494 4495| 参数名 | 类型 | 必填 | 说明 | 4496| -------- | -------- | -------- | -------- | 4497| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 4498 4499**返回值:** 4500 4501| 类型 | 说明 | 4502| -------- | -------- | 4503| Promise<Uint8Array> | 返回异步编码后新分配的Uint8数组。 | 4504 4505**示例:** 4506 4507 ```js 4508 let that = new util.Base64(); 4509 let array = new Uint8Array([115,49,51]); 4510 let rarray = new Uint8Array([99,122,69,122]); 4511 that.encode(array).then(val=>{ 4512 for (var i = 0; i < rarray.length; i++) { 4513 console.log(val[i].toString()) 4514 } 4515 }) 4516 ``` 4517 4518### encodeToString<sup>(deprecated)</sup> 4519 4520encodeToString(src: Uint8Array): Promise<string> 4521 4522通过输入参数异步编码后输出对应文本。 4523 4524> **说明:** 4525> 4526> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToString<sup>9+</sup>](#encodetostring9)替代。 4527 4528**系统能力:** SystemCapability.Utils.Lang 4529 4530**参数:** 4531 4532| 参数名 | 类型 | 必填 | 说明 | 4533| -------- | -------- | -------- | -------- | 4534| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 4535 4536**返回值:** 4537 4538| 类型 | 说明 | 4539| -------- | -------- | 4540| Promise<string> | 返回异步编码后的字符串。 | 4541 4542**示例:** 4543 4544 ```js 4545 let that = new util.Base64(); 4546 let array = new Uint8Array([115,49,51]); 4547 that.encodeToString(array).then(val=>{ 4548 console.log(val) 4549 }) 4550 ``` 4551 4552### decode<sup>(deprecated)</sup> 4553 4554 4555decode(src: Uint8Array | string): Promise<Uint8Array> 4556 4557通过输入参数异步解码后输出对应文本。 4558 4559> **说明:** 4560> 4561> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decode<sup>9+</sup>](#decode9)替代。 4562 4563**系统能力:** SystemCapability.Utils.Lang 4564 4565**参数:** 4566 4567| 参数名 | 类型 | 必填 | 说明 | 4568| -------- | -------- | -------- | -------- | 4569| src | Uint8Array \| string | 是 | 异步解码输入Uint8数组或者字符串。 | 4570 4571**返回值:** 4572 4573| 类型 | 说明 | 4574| -------- | -------- | 4575| Promise<Uint8Array> | 返回异步解码后新分配的Uint8数组。 | 4576 4577**示例:** 4578 4579 ```js 4580 let that = new util.Base64(); 4581 let array = new Uint8Array([99,122,69,122]); 4582 let rarray = new Uint8Array([115,49,51]); 4583 that.decode(array).then(val=>{ 4584 for (var i = 0; i < rarray.length; i++) { 4585 console.log(val[i].toString()) 4586 } 4587 }) 4588 ``` 4589