14d6c458bSopenharmony_ci# ets_utils 24d6c458bSopenharmony_ciThe ets_utils component provides four modules: js_api_module, js_util_module, js_sys_module, and js_concurrent_module. The following provides an introduction to these modules and describes their directory structures, APIs, and involved repositories. 34d6c458bSopenharmony_ci 44d6c458bSopenharmony_ci# Modules 54d6c458bSopenharmony_ci## js_api_module 64d6c458bSopenharmony_ci### Introduction 74d6c458bSopenharmony_ci 84d6c458bSopenharmony_ciThe js_api_module provides the following classes:<br>**URL**: provides APIs to parse, construct, standardize, and encode URLs. The constructor of this class creates a **URL** object, where you can call APIs to parse and change the URL. **URLSearchParams**: provides APIs to handle URL query strings. 94d6c458bSopenharmony_ci 104d6c458bSopenharmony_ci**URI**: provides APIs to parse URI strings. 114d6c458bSopenharmony_ci 124d6c458bSopenharmony_ci**ConvertXml**: provides APIs to convert XML text into JavaScript objects. 134d6c458bSopenharmony_ci 144d6c458bSopenharmony_ci**XmlSerializer**: provides APIs to generate an XML file. The constructor of this class creates an **XmlSerializer** object, where you can call the APIs to generate an XML file. **XmlPullParser**: provides APIs to parse an XML file. The constructor of this class creates an **XmlPullParser** object, where you can call the APIs to parse an XML file. 154d6c458bSopenharmony_ci 164d6c458bSopenharmony_ci### Directory Structure 174d6c458bSopenharmony_ci 184d6c458bSopenharmony_ci``` 194d6c458bSopenharmony_cicommonlibrary/ets_utils/js_api_module/ 204d6c458bSopenharmony_ci├── Class:URL # URL class 214d6c458bSopenharmony_ci│ ├── new URL(input[, base]) # Used to create a URL object 224d6c458bSopenharmony_ci│ ├── hash # hash attribute 234d6c458bSopenharmony_ci│ ├── host # host attribute 244d6c458bSopenharmony_ci│ ├── hostname # hostname attribute 254d6c458bSopenharmony_ci│ ├── href # href attribute 264d6c458bSopenharmony_ci│ ├── origin # origin attribute 274d6c458bSopenharmony_ci│ ├── password # password attribute 284d6c458bSopenharmony_ci│ ├── pathname # pathname attribute 294d6c458bSopenharmony_ci│ ├── port # port attribute 304d6c458bSopenharmony_ci│ ├── protocol # protocol attribute 314d6c458bSopenharmony_ci│ ├── search # search attribute 324d6c458bSopenharmony_ci│ ├── searchParams # searchParams attribute 334d6c458bSopenharmony_ci│ ├── username # username attribute 344d6c458bSopenharmony_ci│ ├── toString() # toString method 354d6c458bSopenharmony_ci│ └── toJSON() # toJSON method 364d6c458bSopenharmony_ci├── Class: URLSearchParams # URLSearchParams class 374d6c458bSopenharmony_ci│ ├── new URLSearchParams() # Used to create a URLSearchParams object 384d6c458bSopenharmony_ci│ ├── new URLSearchParams(string) # Used to create a URLSearchParams object 394d6c458bSopenharmony_ci│ ├── new URLSearchParams(obj) # Used to create a URLSearchParams object 404d6c458bSopenharmony_ci│ ├── new URLSearchParams(iterable) # Used to create a URLSearchParams object 414d6c458bSopenharmony_ci│ ├── append(name, value) # append method 424d6c458bSopenharmony_ci│ ├── delete(name) # delete method 434d6c458bSopenharmony_ci│ ├── entries() # entries method 444d6c458bSopenharmony_ci│ ├── forEach(fn[, thisArg]) # forEach method 454d6c458bSopenharmony_ci│ ├── get(name) # get method 464d6c458bSopenharmony_ci│ ├── getAll(name) # getAll method 474d6c458bSopenharmony_ci│ ├── has(name) # has method 484d6c458bSopenharmony_ci│ ├── keys() # keys method 494d6c458bSopenharmony_ci│ ├── set(name, value) # set method 504d6c458bSopenharmony_ci│ ├── sort() # sort method 514d6c458bSopenharmony_ci│ ├── toString() # toString method 524d6c458bSopenharmony_ci│ ├── values() # values method 534d6c458bSopenharmony_ci│ └── urlSearchParams[Symbol.iterator]() # Used to create a URLSearchParams object 544d6c458bSopenharmony_ci├── Class:URI # URI class 554d6c458bSopenharmony_ci│ ├── URI(str: string) # Used to create a URI object 564d6c458bSopenharmony_ci│ ├── scheme # scheme attribute 574d6c458bSopenharmony_ci│ ├── authority # authority attribute 584d6c458bSopenharmony_ci│ ├── ssp # ssp attribute 594d6c458bSopenharmony_ci│ ├── userinfo # userinfo attribute 604d6c458bSopenharmony_ci│ ├── host # host attribute 614d6c458bSopenharmony_ci│ ├── port # port attribute 624d6c458bSopenharmony_ci│ ├── query # query attribute 634d6c458bSopenharmony_ci│ ├── fragment # fragment attribute 644d6c458bSopenharmony_ci│ ├── path # path attribute 654d6c458bSopenharmony_ci│ ├── equals(ob: Object) # equals attribute 664d6c458bSopenharmony_ci│ ├── normalize() # normalize attribute 674d6c458bSopenharmony_ci│ ├── checkIsAbsolute() # checkIsAbsolute attribute 684d6c458bSopenharmony_ci│ ├── normalize() # normalize method 694d6c458bSopenharmony_ci│ └── toString() # toString method 704d6c458bSopenharmony_ci├── Class:ConvertXml # ConvertXml class 714d6c458bSopenharmony_ci│ ├── ConvertXml() # Used to create a ConvertXml object 724d6c458bSopenharmony_ci│ └── convert(xml: string, options: Object) # convert method 734d6c458bSopenharmony_ci├── Class:XmlSerializer # XmlSerializer class 744d6c458bSopenharmony_ci│ ├── new XmlSerializer(buffer: ArrayBuffer | DataView, encoding?: string) # Used to create an XmlSerializer object 754d6c458bSopenharmony_ci│ ├── setAttributes(name: string, value: string) # setAttributes method 764d6c458bSopenharmony_ci│ ├── addEmptyElement(name: string) # addEmptyElement method 774d6c458bSopenharmony_ci│ ├── setDeclaration() # setDeclaration method 784d6c458bSopenharmony_ci│ ├── startElement(name: string) # startElement method 794d6c458bSopenharmony_ci│ ├── endElement() # endElement method 804d6c458bSopenharmony_ci│ ├── setNamespace(prefix: string, namespace: string) # setNamespace method 814d6c458bSopenharmony_ci│ ├── setComment(text: string) # setComment method 824d6c458bSopenharmony_ci│ ├── setCData(text: string) # setCData method 834d6c458bSopenharmony_ci│ ├── setText(text: string) # setText method 844d6c458bSopenharmony_ci│ └── setDocType(text: string) # setDocType method 854d6c458bSopenharmony_ci└── Class: XmlPullParser # XmlPullParser class 864d6c458bSopenharmony_ci ├── new (buffer: ArrayBuffer | DataView, encoding?: string) # Used to create an XmlPullParser object 874d6c458bSopenharmony_ci └── parse(option: ParseOptions) # parse method 884d6c458bSopenharmony_ci``` 894d6c458bSopenharmony_ci 904d6c458bSopenharmony_ci### Usage 914d6c458bSopenharmony_ci 924d6c458bSopenharmony_ci#### Available APIs 934d6c458bSopenharmony_ci 944d6c458bSopenharmony_ci 954d6c458bSopenharmony_ci| API| Description| 964d6c458bSopenharmony_ci| -------- | -------- | 974d6c458bSopenharmony_ci| URL(url: string,base?:string \| URL) | Creates a **URL** object that references a URL specified using an absolute URL string, a relative URL string, and a base URL string.| 984d6c458bSopenharmony_ci| tostring():string | Converts the parsed URL into a string. Its function is the same as that of read-only **URL.href**.| 994d6c458bSopenharmony_ci| toJSON():string | Converts the parsed URL into a JSON string.| 1004d6c458bSopenharmony_ci| new URLSearchParams() | No-argument constructor that creates a **URLSearchParams** object. The question mark (?) at the beginning of the query string will be ignored.| 1014d6c458bSopenharmony_ci| new URLSearchParams(string) | Constructor that creates a **URLSearchParams** object with the string type specified. The question mark (?) at the beginning of the query string will be ignored.| 1024d6c458bSopenharmony_ci| new URLSearchParams(obj) | Constructor that creates a **URLSearchParams** object with the object type specified. The question mark (?) at the beginning of the query string will be ignored.| 1034d6c458bSopenharmony_ci| new URLSearchParams(iterable) | Constructor that creates a **URLSearchParams** object with the iterable type specified. The question mark (?) at the beginning of the query string will be ignored.| 1044d6c458bSopenharmony_ci| has(name: string): boolean | Checks whether a key in this **URLSearchParams** object has a value. It returns **true** if the key has a value and returns **false** otherwise.| 1054d6c458bSopenharmony_ci| set(name: string, value string): void | Sets the value for a key in this **URLSearchParams** object. If key-value pairs matching the specified key exist, the value of the first occurrence of the key-value pair will be changed to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string.| 1064d6c458bSopenharmony_ci| sort(): void | Sorts all key-value pairs in this **URLSearchParams** object based on the Unicode code points of the keys and returns **undefined**. | 1074d6c458bSopenharmony_ci| toString(): string | Obtains strings that can be used for URL query based on this **URLSearchParams** object.| 1084d6c458bSopenharmony_ci| keys(): iterableIterator\<string> | Obtains an iterator that contains the keys of all the key-value pairs in this **URLSearchParams** object.| 1094d6c458bSopenharmony_ci| values(): iterableIterator\<string> | Obtains an iterator that contains the values of all the key-value pairs in this **URLSearchParams** object.| 1104d6c458bSopenharmony_ci| append(name: string, value: string): void | Appends a key-value pair into this **URLSearchParams** object.| 1114d6c458bSopenharmony_ci| delete(name: string): void | Deletes key-value pairs of the specified key from this **URLSearchParams** object.| 1124d6c458bSopenharmony_ci| get(name: string): string | Obtains the value of the first key-value pair with the specified key in this **URLSearchParams** object.| 1134d6c458bSopenharmony_ci| getAll(name: string): string[] | Obtains all the key-value pairs with the specified key in this **URLSearchParams** object.| 1144d6c458bSopenharmony_ci| entries(): iterableIterator<[string, string]> | Obtains an iterator that contains all the key-value pairs of this **URLSearchParams** object.| 1154d6c458bSopenharmony_ci| forEach(): void | Traverses the key-value pairs in this **URLSearchParams** object by using a callback.| 1164d6c458bSopenharmony_ci| urlSearchParams\[Symbol.iterator]() | Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and each array contains a key and value.| 1174d6c458bSopenharmony_ci| URI(str: string) | Constructs a URI by parsing the given input parameter according to the syntax specified in Appendix A of RFC 2396.| 1184d6c458bSopenharmony_ci| scheme | Obtains the scheme component of this URI; returns **null** if **scheme** is not defined.| 1194d6c458bSopenharmony_ci| authority | Obtains the decoded authority component of this URI; returns **null** if **authority** is not defined.| 1204d6c458bSopenharmony_ci| ssp | Obtains the decoded scheme-specific component of this URI.| 1214d6c458bSopenharmony_ci| userinfo | Obtains the decoded userinfo component of this URI. The return value contains password and username.| 1224d6c458bSopenharmony_ci| host | Obtains the host component of this URI; returns **null** if **host** is not defined.| 1234d6c458bSopenharmony_ci| port | Obtains the port component of this URI; returns **-1** if **port** is not defined. The return value is a non-negative integer.| 1244d6c458bSopenharmony_ci| query | Obtains the query component of this URI; returns **null** if **query** is not defined.| 1254d6c458bSopenharmony_ci| fragment | Obtains the decoded fragment component of this URI; returns **null** if **fragment** is not defined.| 1264d6c458bSopenharmony_ci| path | Obtains the decoded path component of this URI; returns **null** if **path** is not defined.| 1274d6c458bSopenharmony_ci| equals(ob: Object) | Checks whether this URI is the same as another URI object. It immediately returns **false** if the given object is not a URI.| 1284d6c458bSopenharmony_ci| normalize() | Normalizes the path of this URI. It constructs a new URI object if the path of the URI is not standard.| 1294d6c458bSopenharmony_ci| checkIsAbsolute() | Checks whether this URI is an absolute URI (whether the scheme component is defined). It returns **true** if the URI is an absolute URI; otherwise, it returns **false**.| 1304d6c458bSopenharmony_ci| ConvertXml() | A no-argument constructor that creates a **ConvertXml** object. | 1314d6c458bSopenharmony_ci| convert(xml: string, options: Object) | Converts an XML text into a JavaScript object.| 1324d6c458bSopenharmony_ci| XmlSerializer(buffer: ArrayBuffer \| DataView, encoding?: string) | Creates and returns an **XmlSerializer** object. The **XmlSerializer** object passes two parameters. The first parameter is the memory of the **ArrayBuffer** or **DataView** type, and the second parameter is the file format (UTF-8 by default).| 1334d6c458bSopenharmony_ci| setAttributes(name: string, value: string): void | Sets attributes for the XML file.| 1344d6c458bSopenharmony_ci| addEmptyElement(name: string): void | Adds an empty element.| 1354d6c458bSopenharmony_ci| setDeclaration(): void | Sets a declaration for the XML file. Example: <? xml version="1.0"encoding="utf-8">| 1364d6c458bSopenharmony_ci| startElement(name: string): void | Writes the start tag based on the given element name.| 1374d6c458bSopenharmony_ci| endElement(): void | Writes the end tag of the element.| 1384d6c458bSopenharmony_ci| setNamespace(prefix: string, namespace: string): void | Sets the namespace for an element tag.| 1394d6c458bSopenharmony_ci| setComment(text: string): void | Sets the comment.| 1404d6c458bSopenharmony_ci| setCData(text: string): void | Sets the **CData** attribute.| 1414d6c458bSopenharmony_ci| setText(text: string): void | Sets the **Text** attribute.| 1424d6c458bSopenharmony_ci| setDocType(text: string): void | Sets the **DocType** attribute.| 1434d6c458bSopenharmony_ci| XmlPullParser(buffer: ArrayBuffer \| DataView, encoding?: string) | Creates and returns an **XmlPullParser** object. The **XmlPullParser** object passes two parameters. The first parameter is the memory of the **ArrayBuffer** or **DataView** type, and the second parameter is the file format (UTF-8 by default).| 1444d6c458bSopenharmony_ci| parse(option: ParseOptions): void | Parses XML information. **ParseOptions** contains five optional parameters: {supportDoctype?: boolean ignoreNameSpace?: boolean tagValueCallbackFunction?: (name: string, value: string) => boolean attributeValueCallbackFunction?: (name: string, value: string) => boolean) tokenValueCallbackFunction?: (eventType: EventType, value: ParseInfo) => boolean }. For **tokenValueCallbackFunction**, the first input parameter indicates the event type, and the second input parameter indicates the **info** interface that contains getters such as **getColumnNumber** and **getDepth**. You can use methods such as **info.getDepth()** to obtain information during the parsing.| 1454d6c458bSopenharmony_ci#### How to Use 1464d6c458bSopenharmony_ci 1474d6c458bSopenharmony_ciUse the APIs as follows: 1484d6c458bSopenharmony_ci 1494d6c458bSopenharmony_ci 1504d6c458bSopenharmony_ci1. new URL(url: string,base?:string|URL) 1514d6c458bSopenharmony_ci``` 1524d6c458bSopenharmony_cilet b = new URL('https://developer.mozilla.org'); // => 'https://developer.mozilla.org/' 1534d6c458bSopenharmony_ci 1544d6c458bSopenharmony_cilet a = new URL( 'sca/./path/path/../scasa/text', 'http://www.example.com'); 1554d6c458bSopenharmony_ci// => 'http://www.example.com/sca/path/scasa/text' 1564d6c458bSopenharmony_ci``` 1574d6c458bSopenharmony_ci2. tostring():string 1584d6c458bSopenharmony_ci``` 1594d6c458bSopenharmony_ciconst url = new URL('http://10.0xFF.O400.235:8080/directory/file?query#fragment'); 1604d6c458bSopenharmony_ciurl.toString() // => 'http://10.0xff.o400.235:8080/directory/file?query#fragment' 1614d6c458bSopenharmony_ci 1624d6c458bSopenharmony_ciconst url = new URL("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"); 1634d6c458bSopenharmony_ciurl.toString() // => 'http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]/index.html' 1644d6c458bSopenharmony_ci 1654d6c458bSopenharmony_ciconst url = new URL("http://username:password@host:8080/directory/file?query#fragment"); 1664d6c458bSopenharmony_ciurl.toString() // => 'http://username:password@host:8080/directory/file?query#fragment' 1674d6c458bSopenharmony_ci``` 1684d6c458bSopenharmony_ci3. toJSON():string 1694d6c458bSopenharmony_ci``` 1704d6c458bSopenharmony_ciconst url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toString"); 1714d6c458bSopenharmony_ciurl.toJSON(); // => 'https://developer.mozilla.org/en-US/docs/Web/API/URL/toString' 1724d6c458bSopenharmony_ci``` 1734d6c458bSopenharmony_ci4. new URLSearchParams() 1744d6c458bSopenharmony_ci``` 1754d6c458bSopenharmony_cilet params = new URLSearchParams('foo=1&bar=2'); 1764d6c458bSopenharmony_ci``` 1774d6c458bSopenharmony_ci5. new URLSearchParams(string) 1784d6c458bSopenharmony_ci``` 1794d6c458bSopenharmony_ciparams = new URLSearchParams('user=abc&query=xyz'); 1804d6c458bSopenharmony_ciconsole.log(params.get('user')); 1814d6c458bSopenharmony_ci// Prints 'abc' 1824d6c458bSopenharmony_ci``` 1834d6c458bSopenharmony_ci6. new URLSearchParams(obj) 1844d6c458bSopenharmony_ci``` 1854d6c458bSopenharmony_ciconst params = new URLSearchParams({ 1864d6c458bSopenharmony_ci user: 'abc', 1874d6c458bSopenharmony_ci query: ['first', 'second'] 1884d6c458bSopenharmony_ci}); 1894d6c458bSopenharmony_ciconsole.log(params.getAll('query')); 1904d6c458bSopenharmony_ci// Prints [ 'first,second' ] 1914d6c458bSopenharmony_ci``` 1924d6c458bSopenharmony_ci7. new URLSearchParams(iterable) 1934d6c458bSopenharmony_ci``` 1944d6c458bSopenharmony_cilet params; 1954d6c458bSopenharmony_ci 1964d6c458bSopenharmony_ci// Using an array 1974d6c458bSopenharmony_ciparams = new URLSearchParams([ 1984d6c458bSopenharmony_ci ['user', 'abc'], 1994d6c458bSopenharmony_ci ['query', 'first'], 2004d6c458bSopenharmony_ci ['query', 'second'], 2014d6c458bSopenharmony_ci]); 2024d6c458bSopenharmony_ciconsole.log(params.toString()); 2034d6c458bSopenharmony_ci// Prints 'user = abc & query = first&query = second' 2044d6c458bSopenharmony_ci``` 2054d6c458bSopenharmony_ci8. has(name: string): boolean 2064d6c458bSopenharmony_ci``` 2074d6c458bSopenharmony_ciconsole.log(params.has('bar')); // =>true 2084d6c458bSopenharmony_ci``` 2094d6c458bSopenharmony_ci9. set(name: string, value string): void 2104d6c458bSopenharmony_ci``` 2114d6c458bSopenharmony_ciparams.set('baz', 3); 2124d6c458bSopenharmony_ci``` 2134d6c458bSopenharmony_ci10. sort(): void 2144d6c458bSopenharmony_ci``` 2154d6c458bSopenharmony_ciparams .sort(); 2164d6c458bSopenharmony_ci``` 2174d6c458bSopenharmony_ci11. toString(): string 2184d6c458bSopenharmony_ci``` 2194d6c458bSopenharmony_ciconsole.log(params .toString()); // =>bar=2&baz=3&foo=1' 2204d6c458bSopenharmony_ci``` 2214d6c458bSopenharmony_ci12. keys(): iterableIterator\<string> 2224d6c458bSopenharmony_ci``` 2234d6c458bSopenharmony_cifor(var key of params.keys()) { 2244d6c458bSopenharmony_ci console.log(key); 2254d6c458bSopenharmony_ci} // =>bar baz foo 2264d6c458bSopenharmony_ci``` 2274d6c458bSopenharmony_ci13. values(): iterableIterator\<string> 2284d6c458bSopenharmony_ci``` 2294d6c458bSopenharmony_cifor(var value of params.values()) { 2304d6c458bSopenharmony_ci console.log(value); 2314d6c458bSopenharmony_ci} // =>2 3 1 2324d6c458bSopenharmony_ci``` 2334d6c458bSopenharmony_ci14. append(name: string, value: string): void 2344d6c458bSopenharmony_ci``` 2354d6c458bSopenharmony_ciparams.append('foo', 3); // =>bar=2&baz=3&foo=1&foo=3 2364d6c458bSopenharmony_ci``` 2374d6c458bSopenharmony_ci15. delete(name: string): void 2384d6c458bSopenharmony_ci``` 2394d6c458bSopenharmony_ciparams.delete('baz'); // => bar=2&foo=1&foo=3 2404d6c458bSopenharmony_ci``` 2414d6c458bSopenharmony_ci16. get(name: string): string 2424d6c458bSopenharmony_ci``` 2434d6c458bSopenharmony_ciparams.get('foo'); // => 1 2444d6c458bSopenharmony_ci``` 2454d6c458bSopenharmony_ci17. getAll(name: string): string[] 2464d6c458bSopenharmony_ci``` 2474d6c458bSopenharmony_ciparams.getAll('foo'); // =>[ '1', '3' ] 2484d6c458bSopenharmony_ci``` 2494d6c458bSopenharmony_ci18. entries(): iterableIterator<[string, string]> 2504d6c458bSopenharmony_ci``` 2514d6c458bSopenharmony_cifor(var pair of searchParams.entries()) { 2524d6c458bSopenharmony_ci console.log(pair[0]+ ', '+ pair[1]); 2534d6c458bSopenharmony_ci} // => bar, 2 foo, 1 foo, 3 2544d6c458bSopenharmony_ci``` 2554d6c458bSopenharmony_ci19. forEach(): void 2564d6c458bSopenharmony_ci``` 2574d6c458bSopenharmony_ciurl.searchParams.forEach((value, name, searchParams) => { 2584d6c458bSopenharmony_ci console.log(name, value, url.searchParams === searchParams); 2594d6c458bSopenharmony_ci}); 2604d6c458bSopenharmony_ci// => foo 1 true 2614d6c458bSopenharmony_ci// => bar 2 true 2624d6c458bSopenharmony_ci``` 2634d6c458bSopenharmony_ci20. urlSearchParams[Symbol.iterator]() 2644d6c458bSopenharmony_ci``` 2654d6c458bSopenharmony_ciconst params = new URLSearchParams('foo=bar&xyz=baz'); 2664d6c458bSopenharmony_cifor (const [name, value] of params) { 2674d6c458bSopenharmony_ci console.log(name, value); 2684d6c458bSopenharmony_ci} 2694d6c458bSopenharmony_ci// Prints: 2704d6c458bSopenharmony_ci// foo bar 2714d6c458bSopenharmony_ci// xyz bar 2724d6c458bSopenharmony_ci``` 2734d6c458bSopenharmony_ci 2744d6c458bSopenharmony_ci 2754d6c458bSopenharmony_ci21. URI(str: string) 2764d6c458bSopenharmony_ci``` 2774d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 2784d6c458bSopenharmony_ci``` 2794d6c458bSopenharmony_ci22. scheme 2804d6c458bSopenharmony_ci``` 2814d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 2824d6c458bSopenharmony_cigaogao.scheme // => "http"; 2834d6c458bSopenharmony_ci``` 2844d6c458bSopenharmony_ci23. authority 2854d6c458bSopenharmony_ci``` 2864d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 2874d6c458bSopenharmony_cigaogao.authority // => "gg:gaogao@www.baidu.com:99"; 2884d6c458bSopenharmony_ci``` 2894d6c458bSopenharmony_ci24. ssp 2904d6c458bSopenharmony_ci``` 2914d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 2924d6c458bSopenharmony_cigaogao.ssp " // => gg:gaogao@www.baidu.com:99/path/path?query"; 2934d6c458bSopenharmony_ci``` 2944d6c458bSopenharmony_ci25. userinfo 2954d6c458bSopenharmony_ci``` 2964d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 2974d6c458bSopenharmony_cigaogao.userinfo // => "gg:gaogao"; 2984d6c458bSopenharmony_ci``` 2994d6c458bSopenharmony_ci26. host 3004d6c458bSopenharmony_ci``` 3014d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 3024d6c458bSopenharmony_cigaogao.host // => "www.baidu.com"; 3034d6c458bSopenharmony_ci``` 3044d6c458bSopenharmony_ci27. port 3054d6c458bSopenharmony_ci``` 3064d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 3074d6c458bSopenharmony_cigaogao.port // => "99"; 3084d6c458bSopenharmony_ci``` 3094d6c458bSopenharmony_ci28. query 3104d6c458bSopenharmony_ci``` 3114d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 3124d6c458bSopenharmony_cigaogao.query // => "query"; 3134d6c458bSopenharmony_ci``` 3144d6c458bSopenharmony_ci29. fragment 3154d6c458bSopenharmony_ci``` 3164d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 3174d6c458bSopenharmony_cigaogao.fragment // => "fagment"; 3184d6c458bSopenharmony_ci``` 3194d6c458bSopenharmony_ci30. path 3204d6c458bSopenharmony_ci``` 3214d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); 3224d6c458bSopenharmony_cigaogao.path // => "/path/path"; 3234d6c458bSopenharmony_ci``` 3244d6c458bSopenharmony_ci31. equals(ob: Object) 3254d6c458bSopenharmony_ci``` 3264d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment'); 3274d6c458bSopenharmony_cilet gaogao1 = gaogao; 3284d6c458bSopenharmony_cilet res = gaogao.equals(gaogao1); 3294d6c458bSopenharmony_ciconsole.log(res); // => true; 3304d6c458bSopenharmony_ci``` 3314d6c458bSopenharmony_ci32. normalize() 3324d6c458bSopenharmony_ci``` 3334d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/66./../././mm/.././path1?query#fagment'); 3344d6c458bSopenharmony_cilet res = gaogao.normalize(); 3354d6c458bSopenharmony_ciconsole.log(res.path); // => "/path/path1" 3364d6c458bSopenharmony_ciconsole.log(res.toString()); // => "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/path1?query#fagment" 3374d6c458bSopenharmony_ci``` 3384d6c458bSopenharmony_ci33. checkIsAbsolute() 3394d6c458bSopenharmony_ci``` 3404d6c458bSopenharmony_cilet gaogao = new Uri.URI('f/tp://username:password@www.baidu.com:88/path?query#fagment'); 3414d6c458bSopenharmony_cilet res = gaogao.checkIsAbsolute(); 3424d6c458bSopenharmony_ciconsole.log(res); //=> false; 3434d6c458bSopenharmony_ci``` 3444d6c458bSopenharmony_ci34. toString() 3454d6c458bSopenharmony_ci``` 3464d6c458bSopenharmony_cilet gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'); 3474d6c458bSopenharmony_cilet res = gaogao.toString(); 3484d6c458bSopenharmony_ciconsole.log(res.toString()); // => 'http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'; 3494d6c458bSopenharmony_ci``` 3504d6c458bSopenharmony_ci 3514d6c458bSopenharmony_ci 3524d6c458bSopenharmony_ci35. ConvertXml() 3534d6c458bSopenharmony_ci``` 3544d6c458bSopenharmony_civar convertml = new convertXml.ConvertXml(); 3554d6c458bSopenharmony_ci``` 3564d6c458bSopenharmony_ci36. convert(xml: string, options: Object) 3574d6c458bSopenharmony_ci``` 3584d6c458bSopenharmony_civar result = convertml.convert(xml, {compact: false, spaces: 4}); 3594d6c458bSopenharmony_ci``` 3604d6c458bSopenharmony_ci37. new XmlSerializer(buffer: ArrayBuffer | DataView, encoding?: string) 3614d6c458bSopenharmony_ci``` 3624d6c458bSopenharmony_ci 3634d6c458bSopenharmony_civar arrayBuffer = new ArrayBuffer(1024); 3644d6c458bSopenharmony_civar bufView = new DataView(arrayBuffer); 3654d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3664d6c458bSopenharmony_ci``` 3674d6c458bSopenharmony_ci38. setDeclaration(): void 3684d6c458bSopenharmony_ci``` 3694d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3704d6c458bSopenharmony_cithatSer.setDeclaration() // => <?xml version="1.0" encoding="utf-8"?>; 3714d6c458bSopenharmony_ci``` 3724d6c458bSopenharmony_ci39. setComment(text: string):void 3734d6c458bSopenharmony_ci``` 3744d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3754d6c458bSopenharmony_cithatSer.setCommnet("Hello, World!"); // => <!--Hello, World!-->; 3764d6c458bSopenharmony_ci``` 3774d6c458bSopenharmony_ci40. setCData(text: string) :void 3784d6c458bSopenharmony_ci``` 3794d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3804d6c458bSopenharmony_cithatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); // => <![CDATA[root SYSTEM \"http://www.test.org/test.dtd\"]]> 3814d6c458bSopenharmony_ci``` 3824d6c458bSopenharmony_ci41. setDocType(text: string):void 3834d6c458bSopenharmony_ci``` 3844d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3854d6c458bSopenharmony_cithatSer.setDocType("foo"); // => <!DOCTYPE foo> 3864d6c458bSopenharmony_ci``` 3874d6c458bSopenharmony_ci42. setNamespace(prefix: string, namespace: string): void 3884d6c458bSopenharmony_ci43. startElement(name: string): void 3894d6c458bSopenharmony_ci44. setAttributes(name: string, value: string): void 3904d6c458bSopenharmony_ci45. endElement(): void 3914d6c458bSopenharmony_ci46. setText(text: string): void 3924d6c458bSopenharmony_ci``` 3934d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 3944d6c458bSopenharmony_cithatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 3954d6c458bSopenharmony_cithatSer.startElement("table"); 3964d6c458bSopenharmony_cithatSer.setAttributes("importance", "high"); 3974d6c458bSopenharmony_cithatSer.setText("Happy"); 3984d6c458bSopenharmony_ciendElement(); // => <h:table importance="high" xmlns:h="http://www.w3.org/TR/html4/">Happy</h:table> 3994d6c458bSopenharmony_ci``` 4004d6c458bSopenharmony_ci47. addEmptyElement(name: string): void 4014d6c458bSopenharmony_ci``` 4024d6c458bSopenharmony_civar thatSer = new xml.XmlSerializer(bufView); 4034d6c458bSopenharmony_cithatSer.addEmptyElement("b"); // => <b/> 4044d6c458bSopenharmony_ci``` 4054d6c458bSopenharmony_ci48. new (buffer: ArrayBuffer | DataView, encoding?: string) 4064d6c458bSopenharmony_ci``` 4074d6c458bSopenharmony_civar strXml = 4084d6c458bSopenharmony_ci '<?xml version="1.0" encoding="utf-8"?>' + 4094d6c458bSopenharmony_ci '<note importance="high" logged="true">' + 4104d6c458bSopenharmony_ci ' <title>Happy</title>' + 4114d6c458bSopenharmony_ci '</note>'; 4124d6c458bSopenharmony_civar arrayBuffer = new ArrayBuffer(strXml.length*2); 4134d6c458bSopenharmony_civar bufView = new Uint8Array(arrayBuffer); 4144d6c458bSopenharmony_civar strLen = strXml.length; 4154d6c458bSopenharmony_cifor (var i = 0; i < strLen; ++i) { 4164d6c458bSopenharmony_ci bufView[i] = strXml.charCodeAt(i); // Set the ArrayBuffer mode. 4174d6c458bSopenharmony_ci} 4184d6c458bSopenharmony_civar that = new xml.XmlPullParser(arrayBuffer); 4194d6c458bSopenharmony_ci 4204d6c458bSopenharmony_ci``` 4214d6c458bSopenharmony_ci49. parse(option: ParseOptions): void 4224d6c458bSopenharmony_ci``` 4234d6c458bSopenharmony_civar strXml = 4244d6c458bSopenharmony_ci '<?xml version="1.0" encoding="utf-8"?>' + 4254d6c458bSopenharmony_ci '<note importance="high" logged="true">' + 4264d6c458bSopenharmony_ci ' <title>Happy</title>' + 4274d6c458bSopenharmony_ci '</note>'; 4284d6c458bSopenharmony_civar arrayBuffer = new ArrayBuffer(strXml.length*2); 4294d6c458bSopenharmony_civar bufView = new Uint8Array(arrayBuffer); 4304d6c458bSopenharmony_civar strLen = strXml.length; 4314d6c458bSopenharmony_cifor (var i = 0; i < strLen; ++i) { 4324d6c458bSopenharmony_ci bufView[i] = strXml.charCodeAt(i); 4334d6c458bSopenharmony_ci} 4344d6c458bSopenharmony_civar that = new xml.XmlPullParser(arrayBuffer); 4354d6c458bSopenharmony_civar arrTag = {}; 4364d6c458bSopenharmony_ciarrTag[0] = '132'; 4374d6c458bSopenharmony_civar i = 1; 4384d6c458bSopenharmony_cifunction func(key, value){ 4394d6c458bSopenharmony_ci arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 4404d6c458bSopenharmony_ci i++; 4414d6c458bSopenharmony_ci return true; 4424d6c458bSopenharmony_ci} 4434d6c458bSopenharmony_civar options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 4444d6c458bSopenharmony_cithat.parse(options); 4454d6c458bSopenharmony_ci``` 4464d6c458bSopenharmony_ci 4474d6c458bSopenharmony_ci## js_util_module 4484d6c458bSopenharmony_ci### Introduction 4494d6c458bSopenharmony_ci 4504d6c458bSopenharmony_ciThe js_util_module provides the following classes:<br> **TextEncoder**: provides APIs to encode a string into a UTF-8 byte stream. **TextDecoder**: provides APIs to decode a byte stream into a string. **HelpFunction**: provides APIs to perform the callback and promise processing on functions, compile and output error codes, and format class strings. **encode**: provides APIs to use the Base64 encoding scheme to encode all bytes in a Unit8Array typed array into a new Unit8Array typed array or encode a byte array into a string. **decode**: provides APIs to use the Base64 encoding scheme to decode a Base64-encoded string or a Unit8Array typed array into a new Unit8Array typed array. **RationalNumber**: provides APIs to compare rational numbers and obtain numerators and denominators. **LruBuffer**: provides APIs to discard the least recently used data to make rooms for new elements when the buffer is full. The Least Recently Used (LRU) algorithm believes that the recently used data may be accessed again in the near future and the least accessed data is the least valuable data and should be removed from the buffer. **Scope**: provides APIs to define the valid range of a field. The constructor of this class creates comparable objects with lower and upper limits. 4514d6c458bSopenharmony_ci### Directory Structure 4524d6c458bSopenharmony_ci 4534d6c458bSopenharmony_ci``` 4544d6c458bSopenharmony_cicommonlibrary/ets_utils/js_util_module/ 4554d6c458bSopenharmony_ci├── Class:TextEncoder # TextEncoder class 4564d6c458bSopenharmony_ci│ ├── new TextEncoder() # Used to create a TextEncoder object 4574d6c458bSopenharmony_ci│ ├── encode() # encode method 4584d6c458bSopenharmony_ci│ ├── encoding # encoding attribute 4594d6c458bSopenharmony_ci│ └── encodeInto() # encodeInto method 4604d6c458bSopenharmony_ci├── Class:TextDecoder # TextDecoder class 4614d6c458bSopenharmony_ci│ ├── new TextDecoder() # Used to create a TextDecoder object 4624d6c458bSopenharmony_ci│ ├── decode() # decode method 4634d6c458bSopenharmony_ci| ├── decodeWithStream() # decodeWithStream method 4644d6c458bSopenharmony_ci│ ├── encoding # encoding attribute 4654d6c458bSopenharmony_ci│ ├── fatal # fatal attribute 4664d6c458bSopenharmony_ci│ └── ignoreBOM # ignoreBOM attribute 4674d6c458bSopenharmony_ci├── printf() # printf method 4684d6c458bSopenharmony_ci├── getErrorString() # getErrorString method 4694d6c458bSopenharmony_ci├── callbackWrapper() # callbackWrapper method 4704d6c458bSopenharmony_ci├── promiseWrapper() # promiseWrapper method 4714d6c458bSopenharmony_ci├── Class:Base64 # Base64 class 4724d6c458bSopenharmony_ci│ ├── new Base64() # Used to create a Base64 object 4734d6c458bSopenharmony_ci│ ├── encodeSync() # encodeSync method 4744d6c458bSopenharmony_ci│ ├── encodeToStringSync() # encodeToStringSync method 4754d6c458bSopenharmony_ci│ ├── decodeSync() # decodeSync method 4764d6c458bSopenharmony_ci│ ├── encode() # encode method 4774d6c458bSopenharmony_ci│ ├── encodeToString() # encodeToString method 4784d6c458bSopenharmony_ci│ └── decode() # decode method 4794d6c458bSopenharmony_ci├── Class:RationalNumber # RationalNumber class 4804d6c458bSopenharmony_ci│ ├── new RationalNumber() # Used to create a RationalNumber object 4814d6c458bSopenharmony_ci│ ├── createRationalFromString() # createRationalFromString method 4824d6c458bSopenharmony_ci│ ├── compareTo() # compareTo method 4834d6c458bSopenharmony_ci│ ├── equals() # equals method 4844d6c458bSopenharmony_ci│ ├── valueOf() # valueOf method 4854d6c458bSopenharmony_ci│ ├── getCommonDivisor() # getCommonDivisor method 4864d6c458bSopenharmony_ci│ ├── getDenominator() # getDenominator method 4874d6c458bSopenharmony_ci│ ├── getNumerator() # getNumerator method 4884d6c458bSopenharmony_ci│ ├── isFinite() # isFinite method 4894d6c458bSopenharmony_ci│ ├── isNaN() # isNaN method 4904d6c458bSopenharmony_ci│ ├── isZero() # isZero method 4914d6c458bSopenharmony_ci│ └── toString() # toString method 4924d6c458bSopenharmony_ci├── Class:LruBuffer # LruBuffer class 4934d6c458bSopenharmony_ci│ ├── new LruBuffer() # Used to create a LruBuffer object 4944d6c458bSopenharmony_ci│ ├── updateCapacity() # updateCapacity method 4954d6c458bSopenharmony_ci│ ├── toString() # toString method 4964d6c458bSopenharmony_ci│ ├── values() # values method 4974d6c458bSopenharmony_ci│ ├── length # length attribute 4984d6c458bSopenharmony_ci│ ├── getCapacity() # getCapacity method 4994d6c458bSopenharmony_ci│ ├── clear() # clear method 5004d6c458bSopenharmony_ci│ ├── getCreateCount() # getCreateCount method 5014d6c458bSopenharmony_ci│ ├── getMissCount() # getMissCount method 5024d6c458bSopenharmony_ci│ ├── getRemovalCount() # getRemovalCount method 5034d6c458bSopenharmony_ci│ ├── getMatchCount() # getMatchCount method 5044d6c458bSopenharmony_ci│ ├── getPutCount() # getPutCount method 5054d6c458bSopenharmony_ci│ ├── isEmpty() # isEmpty method 5064d6c458bSopenharmony_ci│ ├── get() # get method 5074d6c458bSopenharmony_ci│ ├── put() # put method 5084d6c458bSopenharmony_ci│ ├── keys() # keys method 5094d6c458bSopenharmony_ci│ ├── remove() # remove method 5104d6c458bSopenharmony_ci│ ├── afterRemoval() # afterRemoval method 5114d6c458bSopenharmony_ci│ ├── contains() # contains method 5124d6c458bSopenharmony_ci│ ├── createDefault() # createDefault method 5134d6c458bSopenharmony_ci│ ├── entries() # entries method 5144d6c458bSopenharmony_ci│ └── [Symbol.iterator]() # Symboliterator method 5154d6c458bSopenharmony_ci|—— Class:Scope # Scope class 5164d6c458bSopenharmony_ci| ├── constructor() # Used to create a Scope object 5174d6c458bSopenharmony_ci| ├── toString() # toString method 5184d6c458bSopenharmony_ci| ├── intersect() # intersect method 5194d6c458bSopenharmony_ci| ├── intersect() # intersect method 5204d6c458bSopenharmony_ci| ├── getUpper() # getUpper method 5214d6c458bSopenharmony_ci| ├── getLower() # getLower method 5224d6c458bSopenharmony_ci| ├── expand() # expand method 5234d6c458bSopenharmony_ci| ├── expand() # expand method 5244d6c458bSopenharmony_ci| ├── expand() # expand method 5254d6c458bSopenharmony_ci| ├── contains() # contains method 5264d6c458bSopenharmony_ci| ├── contains() # contains method 5274d6c458bSopenharmony_ci| └── clamp() # clamp method 5284d6c458bSopenharmony_ci└── Class:Types # Types class 5294d6c458bSopenharmony_ci ├── isAnyArrayBuffer() # isAnyArrayBuffer method 5304d6c458bSopenharmony_ci ├── isArrayBufferView() # isArrayBufferView method 5314d6c458bSopenharmony_ci ├── isArgumentsObject() # isArgumentsObject method 5324d6c458bSopenharmony_ci ├── isArrayBuffer() # isArrayBuffer method 5334d6c458bSopenharmony_ci ├── isAsyncFunction() # isAsyncFunction method 5344d6c458bSopenharmony_ci ├── isBigInt64Array() # isBigInt64Array method 5354d6c458bSopenharmony_ci ├── isBigUint64Array() # isBigUint64Array method 5364d6c458bSopenharmony_ci ├── isBooleanObject() # isBooleanObject method 5374d6c458bSopenharmony_ci ├── isBoxedPrimitive() # isBoxedPrimitive method 5384d6c458bSopenharmony_ci ├── isDataView() # isDataView method 5394d6c458bSopenharmony_ci ├── isDate() # isDate method 5404d6c458bSopenharmony_ci ├── isExternal() # isExternal method 5414d6c458bSopenharmony_ci ├── isFloat32Array() # isFloat32Array method 5424d6c458bSopenharmony_ci ├── isFloat64Array() # isFloat64Array method 5434d6c458bSopenharmony_ci ├── isGeneratorFunction() # isGeneratorFunction method 5444d6c458bSopenharmony_ci ├── isGeneratorObject() # isGeneratorObject method 5454d6c458bSopenharmony_ci ├── isInt8Array() # isInt8Array method 5464d6c458bSopenharmony_ci ├── isInt16Array() # isInt16Array method 5474d6c458bSopenharmony_ci ├── isInt32Array() # isInt32Array method 5484d6c458bSopenharmony_ci ├── isMap() # isMap method 5494d6c458bSopenharmony_ci ├── isMapIterator() # isMapIterator method 5504d6c458bSopenharmony_ci ├── isModuleNamespaceObject() # isModuleNamespaceObject method 5514d6c458bSopenharmony_ci ├── isNativeError() # isNativeError method 5524d6c458bSopenharmony_ci ├── isNumberObject() # isNumberObject method 5534d6c458bSopenharmony_ci ├── isPromise() # isPromise method 5544d6c458bSopenharmony_ci ├── isProxy() # isProxy method 5554d6c458bSopenharmony_ci ├── isRegExp() # isRegExp method 5564d6c458bSopenharmony_ci ├── isSet() # isSet method 5574d6c458bSopenharmony_ci ├── isSetIterator() # isSetIterator method 5584d6c458bSopenharmony_ci ├── isSharedArrayBuffer() # isSharedArrayBuffer method 5594d6c458bSopenharmony_ci ├── isStringObject() # isStringObject method 5604d6c458bSopenharmony_ci ├── isSymbolObject() # isSymbolObject method 5614d6c458bSopenharmony_ci ├── isTypedArray() # isTypedArray method 5624d6c458bSopenharmony_ci ├── isUint8Array() # isUint8Array method 5634d6c458bSopenharmony_ci ├── isUint8ClampedArray() # isUint8ClampedArray method 5644d6c458bSopenharmony_ci ├── isUint16Array() # isUint16Array method 5654d6c458bSopenharmony_ci ├── isUint32Array() # isUint32Array method 5664d6c458bSopenharmony_ci ├── isWeakMap() # isWeakMap method 5674d6c458bSopenharmony_ci └── isWeakSet() # isWeakSet method 5684d6c458bSopenharmony_ci``` 5694d6c458bSopenharmony_ci### Usage 5704d6c458bSopenharmony_ci 5714d6c458bSopenharmony_ci#### Available APIs 5724d6c458bSopenharmony_ci 5734d6c458bSopenharmony_ci 5744d6c458bSopenharmony_ci| API| Description| 5754d6c458bSopenharmony_ci| -------- | -------- | 5764d6c458bSopenharmony_ci| constructor(encoding? : string) | A constructor used to create a **TextDecoder** object. **encoding** indicates the encoding format. The default value is **UTF-8**, and the values **gb18030**, **gbk**, and **gb2312** are supported.| 5774d6c458bSopenharmony_ci| readonly encoding : string | Obtains the encoding format. Only **UTF-8** is supported.| 5784d6c458bSopenharmony_ci| encode(input : string) : Uint8Array | Encodes a string into a Uint8Array typed array.| 5794d6c458bSopenharmony_ci| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Encodes a string into a Unit8Array typed array, and stores the array. **dest** indicates the storage location, **read** indicates the number of encoded characters, and **written** indicates the number of bytes occupied by the encoded characters.| 5804d6c458bSopenharmony_ci| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | A constructor used to create a **TextDecoder** object. **encoding** indicates the decoding format. **options** indicates certain attributes, where **fatal** specifies whether to throw an exception, and **ignoreBOM** specifies whether to ignore the BOM flag.| 5814d6c458bSopenharmony_ci| readonly encoding : string | Obtains the decoding format.| 5824d6c458bSopenharmony_ci| readonly fatal : boolean | Obtains the setting of whether to throw an exception.| 5834d6c458bSopenharmony_ci| readonly ignoreBOM : boolean | Obtains the setting of whether to ignore the BOM flag.| 5844d6c458bSopenharmony_ci| decode(input : Uint8Array, options?: { stream?: false }) : string | Decodes a Unit8Array typed array into a string. **input** indicates the data to decode, and **options** specifies whether additional data will be followed, with the default value **false**.| 5854d6c458bSopenharmony_ci| decodeWithStream(input : Uint8Array, options?: { stream?: false }) : string | Decodes a Unit8Array typed array into a string. **input** indicates the data to decode, and **options** specifies whether additional data will be followed, with the default value **false**.| 5864d6c458bSopenharmony_ci| encodeSync(src: Uint8Array): Uint8Array; | Uses the Base64 encoding scheme to synchronously encode all bytes in a Unit8Array typed array into a new Unit8Array typed array.| 5874d6c458bSopenharmony_ci| encodeToStringSync(src: Uint8Array): string; | Uses the Base64 encoding scheme to synchronously encode a Unit8Array typed array into a string.| 5884d6c458bSopenharmony_ci| decodeSync(src: Uint8Array \| string): Uint8Array; | Uses the Base64 encoding scheme to synchronously decode a Base64-encoded string or a Unit8Array typed array into a new Unit8Array typed array.| 5894d6c458bSopenharmony_ci| encode(src: Uint8Array): Promise\<Uint8Array\>; | Uses the Base64 encoding scheme to asynchronously encode all bytes in a Unit8Array typed array into a new Unit8Array typed array.| 5904d6c458bSopenharmony_ci| encodeToString(src: Uint8Array): Promise\<string\>; | Uses the Base64 encoding scheme to asynchronously encode a Unit8Array typed array into a string. It uses a promise to return the result.| 5914d6c458bSopenharmony_ci| decode(src: Uint8Array \| string): Promise\<Uint8Array\>; | Uses the Base64 encoding scheme to asynchronously decode a Base64-encoded string or a Unit8Array typed array into a new Unit8Array typed array.| 5924d6c458bSopenharmony_ci| static createRationalFromString(rationalString: string): RationalNumber | Creates a **RationalNumber** object based on the given string.| 5934d6c458bSopenharmony_ci| compareTo(another: RationalNumber): number | Compares this **RationalNumber** object with the given object.| 5944d6c458bSopenharmony_ci| equals(obj: object): number | Checks whether this **RationalNumber** object equals the given object.| 5954d6c458bSopenharmony_ci| valueOf(): number | Rounds the value of this **RationalNumber** object as an integer or a floating-point number.| 5964d6c458bSopenharmony_ci| static getCommonDivisor(number1: number, number2: number,): number | Obtains the greatest common divisor of two specified integers.| 5974d6c458bSopenharmony_ci| getDenominator(): number | Obtains the denominator of this **RationalNumber** object.| 5984d6c458bSopenharmony_ci| getNumerator(): number | Obtains the numerator of this **RationalNumber** object.| 5994d6c458bSopenharmony_ci| isFinite(): boolean | Checks whether this **RationalNumber** object represents a finite value.| 6004d6c458bSopenharmony_ci| isNaN(): boolean | Checks whether this **RationalNumber** object is a Not a Number (NaN).| 6014d6c458bSopenharmony_ci| isZero(): boolean | Checks whether this **RationalNumber** object is **0**.| 6024d6c458bSopenharmony_ci| toString(): string | Obtains the string representation of this **RationalNumber** object.| 6034d6c458bSopenharmony_ci| constructor(capacity?: number) | Default constructor used to create an **LruBuffer** instance. The default capacity of the buffer is 64.| 6044d6c458bSopenharmony_ci| updateCapacity(newCapacity: number): void | Changes the **LruBuffer** capacity. If the new capacity is less than or equal to **0**, an exception will be thrown.| 6054d6c458bSopenharmony_ci| toString(): string | Obtains the string representation of this **LruBuffer** object. | 6064d6c458bSopenharmony_ci| values(): V[] | Obtains all values in this buffer, listed from the most to the least recently used.| 6074d6c458bSopenharmony_ci| length: number | Obtains the total number of values in this buffer.| 6084d6c458bSopenharmony_ci| getCapacity(): number | Obtains the capacity of this buffer.| 6094d6c458bSopenharmony_ci| clear(): void | Clears key-value pairs from this buffer. **afterRemoval()** will be called to perform subsequent operations.| 6104d6c458bSopenharmony_ci| getCreateCount(): number | Obtains the number of return values for **createDefault()**.| 6114d6c458bSopenharmony_ci| getMissCount(): number | Obtains the number of times that the queried values are mismatched.| 6124d6c458bSopenharmony_ci| getRemovalCount(): number | Obtains the number of removals from this buffer.| 6134d6c458bSopenharmony_ci| getMatchCount(): number | Obtains the number of times that the queried values are matched.| 6144d6c458bSopenharmony_ci| getPutCount(): number | Obtains the number of additions to this buffer.| 6154d6c458bSopenharmony_ci| isEmpty(): boolean | Checks whether this buffer is empty. This API returns **true** if the buffer does not contain any value.| 6164d6c458bSopenharmony_ci| get(key: K) : V \| undefined | Obtains the value of the specified key. This API returns the value of the key if a match is found in the buffer; otherwise, it returns **undefined**.| 6174d6c458bSopenharmony_ci| put(key: K , value: V): V | Adds a key-value pair to the buffer and outputs the value associated with the key. This API returns the existing value if the key already exists; otherwise, it returns the value added. If the key or value is null, an exception will be thrown.| 6184d6c458bSopenharmony_ci| keys(): K[ ] | Obtains all keys in this buffer, listed from the most to the least recently used.| 6194d6c458bSopenharmony_ci| remove(key: K): V \| undefined | Removes the specified key and its value from this buffer.| 6204d6c458bSopenharmony_ci| afterRemoval(isEvict: boolean, key: K, value : V, newValue : V):void | Performs subsequent operations after a value is removed.| 6214d6c458bSopenharmony_ci| contains(key: K): boolean | Checks whether this buffer contains the specified key. This API returns **true** if the buffer contains the specified key.| 6224d6c458bSopenharmony_ci| createDefault(key: K): V | Creates a value if the value of the specified key is not available.| 6234d6c458bSopenharmony_ci| entries(): [K,V] | Obtains a new iterator object that contains all key-value pairs in this buffer. The key and value of each pair are objects.| 6244d6c458bSopenharmony_ci| \[Symbol.iterator\](): [K,V] | Obtains a two-dimensional array in key-value pairs.| 6254d6c458bSopenharmony_ci| constructor(lowerObj: ScopeType, upperObj : ScopeType) | A constructor used to create a **Scope** object with the specified upper and lower limits.| 6264d6c458bSopenharmony_ci| toString(): string | Obtains a string representation that contains this **Scope**.| 6274d6c458bSopenharmony_ci| intersect(range: Scope): Scope | Obtains the intersection of this **Scope** and the given **Scope**.| 6284d6c458bSopenharmony_ci| intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope | Obtains the intersection of this **Scope** and the given lower and upper limits.| 6294d6c458bSopenharmony_ci| getUpper(): ScopeType | Obtains the upper limit of this **Scope**.| 6304d6c458bSopenharmony_ci| getLower(): ScopeType | Obtains the lower limit of this **Scope**.| 6314d6c458bSopenharmony_ci| expand(lowerObj: ScopeType, upperObj: ScopeType): Scope | Obtains the union set of this **Scope** and the given lower and upper limits.| 6324d6c458bSopenharmony_ci| expand(range: Scope): Scope | Obtains the union set of this **Scope** and the given **Scope**.| 6334d6c458bSopenharmony_ci| expand(value: ScopeType): Scope | Obtains the union set of this **Scope** and the given value.| 6344d6c458bSopenharmony_ci| contains(value: ScopeType): boolean | Checks whether a value is within this **Scope**.| 6354d6c458bSopenharmony_ci| contains(range: Scope): boolean | Checks whether a range is within this **Scope**.| 6364d6c458bSopenharmony_ci| clamp(value: ScopeType): ScopeType | Limits a value to this **Scope**.| 6374d6c458bSopenharmony_ci| function printf(format: string, ...args: Object[]): string | Prints the input content in a formatted string. **format** can contain zero or more format specifiers.| 6384d6c458bSopenharmony_ci| function getErrorString(errno: number): string | Obtains detailed information about a system error code.| 6394d6c458bSopenharmony_ci| function callbackWrapper(original: Function): (err: Object, value: Object) => void | Wraps an asynchronous function (or a function that returns a promise) into an error-first callback, which means that **(err, value) => ...** is used as the last parameter of the callback. In the callback, the first parameter indicates the cause of the rejection (the value is **null** if the promise has been resolved), and the second parameter indicates the resolved value.| 6404d6c458bSopenharmony_ci| function promiseWrapper(original: (err: Object, value: Object) => void): Object | Wraps a function that follows the error-first callback paradigm into a promise.| 6414d6c458bSopenharmony_ci| isAnyArrayBuffer(value: Object): boolean | Checks whether the input value is of the **ArrayBuffer** or **SharedArrayBuffer** type.| 6424d6c458bSopenharmony_ci| isArrayBufferView(value: Object): boolean | Checks whether the input value is of the **ArrayBufferView** type, which is a helper type representing any of the following: napi_int8_array, napi_uint8_array, napi_uint8_clamped_array, napi_int16_array, napi_uint16_array, napi_int32_array, napi_uint32_array, napi_float32_array, napi_float64_array, and DataView.| 6434d6c458bSopenharmony_ci| isArgumentsObject(value: Object): boolean | Checks whether the input value is of the **arguments** type.| 6444d6c458bSopenharmony_ci| isArrayBuffer(value: Object): boolean | Checks whether the input value is of the **ArrayBuffer** type.| 6454d6c458bSopenharmony_ci| isAsyncFunction(value: Object): boolean | Checks whether the input value is an asynchronous function.| 6464d6c458bSopenharmony_ci| isBigInt64Array(value: Object): boolean | Checks whether the input value is of the **BigInt64Array** type.| 6474d6c458bSopenharmony_ci| isBigUint64Array(value: Object): boolean | Checks whether the input value is of the **BigUint64Array** type.| 6484d6c458bSopenharmony_ci| isBooleanObject(value: Object): boolean | Checks whether the input value is of the **Boolean** type.| 6494d6c458bSopenharmony_ci| isBoxedPrimitive(value: Object): boolean | Checks whether the input value is of the **Boolean**, **Number**, **String**, or **Symbol** type.| 6504d6c458bSopenharmony_ci| isDataView(value: Object): boolean | Checks whether the input value is of the **DataView** type.| 6514d6c458bSopenharmony_ci| isDate(value: Object): boolean | Checks whether the input value is of the **Date** type.| 6524d6c458bSopenharmony_ci| isExternal(value: Object): boolean | Checks whether the input value is of the **native external** type.| 6534d6c458bSopenharmony_ci| isFloat32Array(value: Object): boolean | Checks whether the input value is of the **Float32Array** type.| 6544d6c458bSopenharmony_ci| isFloat64Array(value: Object): boolean | Checks whether the input value is of the **Float64Array** type.| 6554d6c458bSopenharmony_ci| isGeneratorFunction(value: Object): boolean | Checks whether the input value is a generator function.| 6564d6c458bSopenharmony_ci| isGeneratorObject(value: Object): boolean | Checks whether the input value is a generator object.| 6574d6c458bSopenharmony_ci| isInt8Array(value: Object): boolean | Checks whether the input value is of the **Int8Array** type.| 6584d6c458bSopenharmony_ci| isInt16Array(value: Object): boolean | Checks whether the input value is of the **Int16Array** type.| 6594d6c458bSopenharmony_ci| isInt32Array(value: Object): boolean | Checks whether the input value is of the **Int32Array** type.| 6604d6c458bSopenharmony_ci| isMap(value: Object): boolean | Checks whether the input value is of the **Map** type.| 6614d6c458bSopenharmony_ci| isMapIterator(value: Object): boolean | Checks whether the input value is of the **MapIterator** type.| 6624d6c458bSopenharmony_ci| isModuleNamespaceObject(value: Object): boolean | Checks whether the input value is a module namespace object.| 6634d6c458bSopenharmony_ci| isNativeError(value: Object): boolean | Checks whether the input value is of the **Error** type.| 6644d6c458bSopenharmony_ci| isNumberObject(value: Object): boolean | Checks whether the input value is a number object.| 6654d6c458bSopenharmony_ci| isPromise(value: Object): boolean | Checks whether the input value is a promise.| 6664d6c458bSopenharmony_ci| isProxy(value: Object): boolean | Checks whether the input value is a proxy.| 6674d6c458bSopenharmony_ci| isRegExp(value: Object): boolean | Checks whether the input value is a regular expression.| 6684d6c458bSopenharmony_ci| isSet(value: Object): boolean | Checks whether the input value is of the **Set** type.| 6694d6c458bSopenharmony_ci| isSetIterator(value: Object): boolean | Checks whether the input value is of the **SetIterator** type.| 6704d6c458bSopenharmony_ci| isSharedArrayBuffer(value: Object): boolean | Checks whether the input value is of the **SharedArrayBuffer** type.| 6714d6c458bSopenharmony_ci| isStringObject(value: Object): boolean | Checks whether the input value is a string object.| 6724d6c458bSopenharmony_ci| isSymbolObject(value: Object): boolean | Checks whether the input value is a symbol object.| 6734d6c458bSopenharmony_ci| isTypedArray(value: Object): boolean | Checks whether the input value is of the **TypedArray** type.| 6744d6c458bSopenharmony_ci| isUint8Array(value: Object): boolean | Checks whether the input value is of the **Uint8Array** type.| 6754d6c458bSopenharmony_ci| isUint8ClampedArray(value: Object): boolean | Checks whether the input value is of the **Uint8ClampedArray** type.| 6764d6c458bSopenharmony_ci| isUint16Array(value: Object): boolean | Checks whether the input value is of the **Uint16Array** type.| 6774d6c458bSopenharmony_ci| isUint32Array(value: Object): boolean | Checks whether the input value is of the **Uint32Array** type.| 6784d6c458bSopenharmony_ci| isWeakMap(value: Object): boolean | Checks whether the input value is of the **WeakMap** type.| 6794d6c458bSopenharmony_ci| isWeakSet(value: Object): boolean | Checks whether the input value is of the **WeakSet** type.| 6804d6c458bSopenharmony_ci 6814d6c458bSopenharmony_ciEach format specifier in **printf()** is replaced with the converted value of the corresponding parameter. The following format specifiers are supported: 6824d6c458bSopenharmony_ci| Format Specifier| Description| 6834d6c458bSopenharmony_ci| -------- | -------- | 6844d6c458bSopenharmony_ci| %s: | String. It is used to convert all values except **BigInt**, **Object**, and **-0**.| 6854d6c458bSopenharmony_ci| %d: | Number. It is used to convert all values except **BigInt** and **Symbol**.| 6864d6c458bSopenharmony_ci| %i: | **parseInt(value, 10)**. It used to convert all values except **BigInt** and **Symbol**.| 6874d6c458bSopenharmony_ci| %f: | **parseFloat(value)**. It is used to convert all values except **Symbol**.| 6884d6c458bSopenharmony_ci| %j: | JSON. A circular reference in the parameter will be replaced with the string **'[Circular]'**.| 6894d6c458bSopenharmony_ci| %o: | A common JavaScript object represented in a string. It is similar to **util.inspect()** with the option **{ showHidden: true, showProxy: true }**. This will display a complete object, including innumerable properties and proxies.| 6904d6c458bSopenharmony_ci| %O: | Object. A string representation of an object with a common JavaScript object format. It is similar to **util.inspect()** without options. This will display a complete object, excluding innumerable properties and proxies.| 6914d6c458bSopenharmony_ci| %c: | This specifier is ignored and will jump any incoming CSS.| 6924d6c458bSopenharmony_ci| %%: | Single percent sign ('%'). This does not consume to-be-sampled parameters.| 6934d6c458bSopenharmony_ci 6944d6c458bSopenharmony_ci#### How to Use 6954d6c458bSopenharmony_ciUse the APIs as follows: 6964d6c458bSopenharmony_ci 6974d6c458bSopenharmony_ci1. readonly encoding() 6984d6c458bSopenharmony_ci 6994d6c458bSopenharmony_ci``` 7004d6c458bSopenharmony_ciimport util from '@ohos.util' 7014d6c458bSopenharmony_civar textEncoder = new util.TextEncoder(); 7024d6c458bSopenharmony_civar getEncoding = textEncoder.encoding(); 7034d6c458bSopenharmony_ci``` 7044d6c458bSopenharmony_ci2. encode() 7054d6c458bSopenharmony_ci``` 7064d6c458bSopenharmony_ciimport util from '@ohos.util' 7074d6c458bSopenharmony_civar textEncoder = new util.TextEncoder(); 7084d6c458bSopenharmony_civar result = textEncoder.encode('abc'); 7094d6c458bSopenharmony_ci``` 7104d6c458bSopenharmony_ci3. encodeInto() 7114d6c458bSopenharmony_ci``` 7124d6c458bSopenharmony_ciimport util from '@ohos.util' 7134d6c458bSopenharmony_civar textEncoder = new util.TextEncoder(); 7144d6c458bSopenharmony_civar obj = textEncoder.encodeInto('abc', dest); 7154d6c458bSopenharmony_ci``` 7164d6c458bSopenharmony_ci4. textDecoder() 7174d6c458bSopenharmony_ci``` 7184d6c458bSopenharmony_ciimport util from '@ohos.util' 7194d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7204d6c458bSopenharmony_ci``` 7214d6c458bSopenharmony_ci5. readonly encoding() 7224d6c458bSopenharmony_ci``` 7234d6c458bSopenharmony_ciimport util from '@ohos.util' 7244d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7254d6c458bSopenharmony_civar getEncoding = textDecoder.encoding(); 7264d6c458bSopenharmony_ci``` 7274d6c458bSopenharmony_ci6. readonly fatal() 7284d6c458bSopenharmony_ci``` 7294d6c458bSopenharmony_ciimport util from '@ohos.util' 7304d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7314d6c458bSopenharmony_civar fatalStr = textDecoder.fatal(); 7324d6c458bSopenharmony_ci``` 7334d6c458bSopenharmony_ci7. readonly ignoreBOM() 7344d6c458bSopenharmony_ci``` 7354d6c458bSopenharmony_ciimport util from '@ohos.util' 7364d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7374d6c458bSopenharmony_civar ignoreBom = textDecoder.ignoreBOM(); 7384d6c458bSopenharmony_ci``` 7394d6c458bSopenharmony_ci8. decode() 7404d6c458bSopenharmony_ci``` 7414d6c458bSopenharmony_ciimport util from '@ohos.util' 7424d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7434d6c458bSopenharmony_civar result = textDecoder.decode(input, {stream : true}); 7444d6c458bSopenharmony_ci``` 7454d6c458bSopenharmony_ci9. decodeWithStream() 7464d6c458bSopenharmony_ci``` 7474d6c458bSopenharmony_ciimport util from '@ohos.util' 7484d6c458bSopenharmony_civar textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); 7494d6c458bSopenharmony_civar result = textDecoder.decodeWithStream(input, {stream : true}); 7504d6c458bSopenharmony_ci``` 7514d6c458bSopenharmony_ci10. printf() 7524d6c458bSopenharmony_ci``` 7534d6c458bSopenharmony_ciimport util from '@ohos.util' 7544d6c458bSopenharmony_civar format = "%%%o%%%i%s"; 7554d6c458bSopenharmony_civar value = function aa(){}; 7564d6c458bSopenharmony_civar value1 = 1.5; 7574d6c458bSopenharmony_civar value2 = "qwer"; 7584d6c458bSopenharmony_civar result = util.printf(format,value,value1,value2); 7594d6c458bSopenharmony_ci``` 7604d6c458bSopenharmony_ci11. getErrorString() 7614d6c458bSopenharmony_ci``` 7624d6c458bSopenharmony_ciimport util from '@ohos.util' 7634d6c458bSopenharmony_civar errnum = 13; 7644d6c458bSopenharmony_civar result = util.getErrorString(errnum); 7654d6c458bSopenharmony_ci``` 7664d6c458bSopenharmony_ci12. callbackWrapper() 7674d6c458bSopenharmony_ci``` 7684d6c458bSopenharmony_ciimport util from '@ohos.util' 7694d6c458bSopenharmony_ciasync function promiseFn() { 7704d6c458bSopenharmony_ci return Promise.resolve('value'); 7714d6c458bSopenharmony_ci}; 7724d6c458bSopenharmony_civar cb = util.callbackWrapper(promiseFn); 7734d6c458bSopenharmony_cicb((err, ret) => { 7744d6c458bSopenharmony_ci expect(err).strictEqual(null); 7754d6c458bSopenharmony_ci expect(ret).strictEqual('value'); 7764d6c458bSopenharmony_ci}) 7774d6c458bSopenharmony_ci``` 7784d6c458bSopenharmony_ci13. promiseWrapper() 7794d6c458bSopenharmony_ci``` 7804d6c458bSopenharmony_ciimport util from '@ohos.util' 7814d6c458bSopenharmony_cifunction aysnFun(str1, str2, callback) { 7824d6c458bSopenharmony_ci if (typeof str1 === 'string' && typeof str1 === 'string') { 7834d6c458bSopenharmony_ci callback(null, str1 + str2); 7844d6c458bSopenharmony_ci } else { 7854d6c458bSopenharmony_ci callback('type err'); 7864d6c458bSopenharmony_ci } 7874d6c458bSopenharmony_ci} 7884d6c458bSopenharmony_cilet newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World'); 7894d6c458bSopenharmony_cinewPromiseObj.then(res => { 7904d6c458bSopenharmony_ci expect(res).strictEqual('HelloWorld'); 7914d6c458bSopenharmony_ci}) 7924d6c458bSopenharmony_ci``` 7934d6c458bSopenharmony_ci14. encodeSync() 7944d6c458bSopenharmony_ci``` 7954d6c458bSopenharmony_ciimport util from '@ohos.util' 7964d6c458bSopenharmony_civar that = new util.Base64(); 7974d6c458bSopenharmony_civar array = new Uint8Array([115,49,51]); 7984d6c458bSopenharmony_civar result = that.encodeSync(array); 7994d6c458bSopenharmony_ci``` 8004d6c458bSopenharmony_ci15. encodeToStringSync() 8014d6c458bSopenharmony_ci``` 8024d6c458bSopenharmony_ciimport util from '@ohos.util' 8034d6c458bSopenharmony_civar that = new util.Base64(); 8044d6c458bSopenharmony_civar array = new Uint8Array([115,49,51]); 8054d6c458bSopenharmony_civar result = that.encodeToStringSync(array); 8064d6c458bSopenharmony_ci``` 8074d6c458bSopenharmony_ci16. decodeSync() 8084d6c458bSopenharmony_ci``` 8094d6c458bSopenharmony_ciimport util from '@ohos.util' 8104d6c458bSopenharmony_civar that = new util.Base64() 8114d6c458bSopenharmony_civar buff = 'czEz'; 8124d6c458bSopenharmony_civar result = that.decodeSync(buff); 8134d6c458bSopenharmony_ci 8144d6c458bSopenharmony_ci``` 8154d6c458bSopenharmony_ci17. encode() 8164d6c458bSopenharmony_ci``` 8174d6c458bSopenharmony_ciimport util from '@ohos.util' 8184d6c458bSopenharmony_civar that = new util.Base64() 8194d6c458bSopenharmony_civar array = new Uint8Array([115,49,51]); 8204d6c458bSopenharmony_ciawait that.encode(array).then(val=>{ 8214d6c458bSopenharmony_ci}) 8224d6c458bSopenharmony_cidone() 8234d6c458bSopenharmony_ci``` 8244d6c458bSopenharmony_ci18. encodeToString() 8254d6c458bSopenharmony_ci``` 8264d6c458bSopenharmony_ciimport util from '@ohos.util' 8274d6c458bSopenharmony_civar that = new util.Base64() 8284d6c458bSopenharmony_civar array = new Uint8Array([115,49,51]); 8294d6c458bSopenharmony_ciawait that.encodeToString(array).then(val=>{ 8304d6c458bSopenharmony_ci}) 8314d6c458bSopenharmony_cidone() 8324d6c458bSopenharmony_ci``` 8334d6c458bSopenharmony_ci19. decode() 8344d6c458bSopenharmony_ci``` 8354d6c458bSopenharmony_ciimport util from '@ohos.util' 8364d6c458bSopenharmony_civar that = new util.Base64() 8374d6c458bSopenharmony_civar buff = 'czEz'; 8384d6c458bSopenharmony_ciawait that.decode(buff).then(val=>{ 8394d6c458bSopenharmony_ci}) 8404d6c458bSopenharmony_cidone() 8414d6c458bSopenharmony_ci``` 8424d6c458bSopenharmony_ci20. createRationalFromString() 8434d6c458bSopenharmony_ci``` 8444d6c458bSopenharmony_ciimport util from '@ohos.util' 8454d6c458bSopenharmony_civar pro = new util.RationalNumber(0, 0); 8464d6c458bSopenharmony_civar res = pro.createRationalFromString("-1:2"); 8474d6c458bSopenharmony_civar result1 = res.valueOf(); 8484d6c458bSopenharmony_ci``` 8494d6c458bSopenharmony_ci21. compareTo() 8504d6c458bSopenharmony_ci``` 8514d6c458bSopenharmony_ciimport util from '@ohos.util' 8524d6c458bSopenharmony_civar pro = new util.RationalNumber(2, 1); 8534d6c458bSopenharmony_civar proc = new util.RationalNumber(3, 4); 8544d6c458bSopenharmony_civar res = pro.compareTo(proc); 8554d6c458bSopenharmony_ci``` 8564d6c458bSopenharmony_ci22. equals() 8574d6c458bSopenharmony_ci``` 8584d6c458bSopenharmony_ciimport util from '@ohos.util' 8594d6c458bSopenharmony_civar pro = new util.RationalNumber(2, 1); 8604d6c458bSopenharmony_civar proc = new util.RationalNumber(3, 4); 8614d6c458bSopenharmony_civar res = pro.equals(proc); 8624d6c458bSopenharmony_ci``` 8634d6c458bSopenharmony_ci23. valueOf() 8644d6c458bSopenharmony_ci``` 8654d6c458bSopenharmony_ciimport util from '@ohos.util' 8664d6c458bSopenharmony_civar pro = new util.RationalNumber(2, 1); 8674d6c458bSopenharmony_civar res = pro.valueOf(); 8684d6c458bSopenharmony_ci``` 8694d6c458bSopenharmony_ci24. getCommonDivisor() 8704d6c458bSopenharmony_ci``` 8714d6c458bSopenharmony_ciimport util from '@ohos.util' 8724d6c458bSopenharmony_civar pro = new util.RationalNumber(0, 0); 8734d6c458bSopenharmony_civar res = pro.getCommonDivisor(4, 8); 8744d6c458bSopenharmony_ci``` 8754d6c458bSopenharmony_ci25. getDenominator() 8764d6c458bSopenharmony_ci``` 8774d6c458bSopenharmony_ciimport util from '@ohos.util' 8784d6c458bSopenharmony_civar pro = new util.RationalNumber(2, 1); 8794d6c458bSopenharmony_civar res = pro.getDenominator(); 8804d6c458bSopenharmony_ci``` 8814d6c458bSopenharmony_ci26. getNumerator() 8824d6c458bSopenharmony_ci``` 8834d6c458bSopenharmony_ciimport util from '@ohos.util' 8844d6c458bSopenharmony_civar pro = new util.RationalNumber(-2, 1); 8854d6c458bSopenharmony_civar res = pro.getNumerator(); 8864d6c458bSopenharmony_ci``` 8874d6c458bSopenharmony_ci27. isFinite() 8884d6c458bSopenharmony_ci``` 8894d6c458bSopenharmony_ciimport util from '@ohos.util' 8904d6c458bSopenharmony_civar pro = new util.RationalNumber(-2, 1); 8914d6c458bSopenharmony_civar res = pro.isFinite(); 8924d6c458bSopenharmony_ci``` 8934d6c458bSopenharmony_ci28. isNaN() 8944d6c458bSopenharmony_ci``` 8954d6c458bSopenharmony_ciimport util from '@ohos.util' 8964d6c458bSopenharmony_civar pro = new util.RationalNumber(-2, 1); 8974d6c458bSopenharmony_civar res = pro.isNaN(); 8984d6c458bSopenharmony_ci``` 8994d6c458bSopenharmony_ci29. isZero() 9004d6c458bSopenharmony_ci``` 9014d6c458bSopenharmony_ciimport util from '@ohos.util' 9024d6c458bSopenharmony_civar pro = new util.RationalNumber(-2, 1); 9034d6c458bSopenharmony_civar res = pro.isZero(); 9044d6c458bSopenharmony_ci``` 9054d6c458bSopenharmony_ci30. toString() 9064d6c458bSopenharmony_ci``` 9074d6c458bSopenharmony_ciimport util from '@ohos.util' 9084d6c458bSopenharmony_civar pro = new util.RationalNumber(-2, 1); 9094d6c458bSopenharmony_civar res = pro.toString(); 9104d6c458bSopenharmony_ci``` 9114d6c458bSopenharmony_ci31. updateCapacity() 9124d6c458bSopenharmony_ci``` 9134d6c458bSopenharmony_ciimport util from '@ohos.util' 9144d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9154d6c458bSopenharmony_civar result = pro.updateCapacity(100); 9164d6c458bSopenharmony_ci``` 9174d6c458bSopenharmony_ci32. toString() 9184d6c458bSopenharmony_ci``` 9194d6c458bSopenharmony_ciimport util from '@ohos.util' 9204d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9214d6c458bSopenharmony_cipro.put(2,10); 9224d6c458bSopenharmony_cipro.get(2); 9234d6c458bSopenharmony_cipro.remove(20); 9244d6c458bSopenharmony_civar result = pro.toString(); 9254d6c458bSopenharmony_ci``` 9264d6c458bSopenharmony_ci33. values() 9274d6c458bSopenharmony_ci``` 9284d6c458bSopenharmony_ciimport util from '@ohos.util' 9294d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9304d6c458bSopenharmony_cipro.put(2,10); 9314d6c458bSopenharmony_cipro.put(2,"anhu"); 9324d6c458bSopenharmony_cipro.put("afaf","grfb"); 9334d6c458bSopenharmony_civar result = pro.values(); 9344d6c458bSopenharmony_ci``` 9354d6c458bSopenharmony_ci34. length 9364d6c458bSopenharmony_ci``` 9374d6c458bSopenharmony_ciimport util from '@ohos.util' 9384d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9394d6c458bSopenharmony_cipro.put(2,10); 9404d6c458bSopenharmony_cipro.put(1,8); 9414d6c458bSopenharmony_civar result = pro.length; 9424d6c458bSopenharmony_ci``` 9434d6c458bSopenharmony_ci35. getCapacity() 9444d6c458bSopenharmony_ci``` 9454d6c458bSopenharmony_ciimport util from '@ohos.util' 9464d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9474d6c458bSopenharmony_civar result = pro.getCapacity(); 9484d6c458bSopenharmony_ci``` 9494d6c458bSopenharmony_ci36. clear() 9504d6c458bSopenharmony_ci``` 9514d6c458bSopenharmony_ciimport util from '@ohos.util' 9524d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9534d6c458bSopenharmony_cipro.put(2,10); 9544d6c458bSopenharmony_cipro.clear(); 9554d6c458bSopenharmony_ci``` 9564d6c458bSopenharmony_ci37. getCreateCount() 9574d6c458bSopenharmony_ci``` 9584d6c458bSopenharmony_ciimport util from '@ohos.util' 9594d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9604d6c458bSopenharmony_cipro.put(1,8); 9614d6c458bSopenharmony_civar result = pro.getCreateCount(); 9624d6c458bSopenharmony_ci``` 9634d6c458bSopenharmony_ci38. getMissCount() 9644d6c458bSopenharmony_ci``` 9654d6c458bSopenharmony_ciimport util from '@ohos.util' 9664d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9674d6c458bSopenharmony_cipro.put(2,10); 9684d6c458bSopenharmony_cipro.get(2) 9694d6c458bSopenharmony_civar result = pro.getMissCount(); 9704d6c458bSopenharmony_ci``` 9714d6c458bSopenharmony_ci39. getRemovalCount() 9724d6c458bSopenharmony_ci``` 9734d6c458bSopenharmony_ci 9744d6c458bSopenharmony_ciimport util from '@ohos.util' 9754d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9764d6c458bSopenharmony_cipro.put(2,10); 9774d6c458bSopenharmony_cipro.updateCapacity(2); 9784d6c458bSopenharmony_cipro.put(50,22); 9794d6c458bSopenharmony_civar result = pro.getRemovalCount(); 9804d6c458bSopenharmony_ci 9814d6c458bSopenharmony_ci``` 9824d6c458bSopenharmony_ci40. getMatchCount() 9834d6c458bSopenharmony_ci``` 9844d6c458bSopenharmony_ciimport util from '@ohos.util' 9854d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9864d6c458bSopenharmony_cipro.put(2,10); 9874d6c458bSopenharmony_cipro.get(2); 9884d6c458bSopenharmony_civar result = pro.getMatchCount(); 9894d6c458bSopenharmony_ci``` 9904d6c458bSopenharmony_ci41. getPutCount() 9914d6c458bSopenharmony_ci``` 9924d6c458bSopenharmony_ciimport util from '@ohos.util' 9934d6c458bSopenharmony_civar pro = new util.LruBuffer(); 9944d6c458bSopenharmony_cipro.put(2,10); 9954d6c458bSopenharmony_civar result = pro.getPutCount(); 9964d6c458bSopenharmony_ci``` 9974d6c458bSopenharmony_ci42. isEmpty() 9984d6c458bSopenharmony_ci``` 9994d6c458bSopenharmony_ciimport util from '@ohos.util' 10004d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10014d6c458bSopenharmony_cipro.put(2,10); 10024d6c458bSopenharmony_civar result = pro.isEmpty(); 10034d6c458bSopenharmony_ci``` 10044d6c458bSopenharmony_ci43. get() 10054d6c458bSopenharmony_ci 10064d6c458bSopenharmony_ci``` 10074d6c458bSopenharmony_ciimport util from '@ohos.util' 10084d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10094d6c458bSopenharmony_cipro.put(2,10); 10104d6c458bSopenharmony_civar result = pro.get(2); 10114d6c458bSopenharmony_ci``` 10124d6c458bSopenharmony_ci44. put() 10134d6c458bSopenharmony_ci``` 10144d6c458bSopenharmony_ciimport util from '@ohos.util' 10154d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10164d6c458bSopenharmony_civar result = pro.put(2,10); 10174d6c458bSopenharmony_ci``` 10184d6c458bSopenharmony_ci45. keys() 10194d6c458bSopenharmony_ci``` 10204d6c458bSopenharmony_ciimport util from '@ohos.util' 10214d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10224d6c458bSopenharmony_cipro.put(2,10); 10234d6c458bSopenharmony_civar result = pro.keys(); 10244d6c458bSopenharmony_ci``` 10254d6c458bSopenharmony_ci46. remove() 10264d6c458bSopenharmony_ci``` 10274d6c458bSopenharmony_ciimport util from '@ohos.util' 10284d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10294d6c458bSopenharmony_cipro.put(2,10); 10304d6c458bSopenharmony_civar result = pro.remove(20); 10314d6c458bSopenharmony_ci``` 10324d6c458bSopenharmony_ci47. contains() 10334d6c458bSopenharmony_ci``` 10344d6c458bSopenharmony_ciimport util from '@ohos.util' 10354d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10364d6c458bSopenharmony_cipro.put(2,10); 10374d6c458bSopenharmony_civar result = pro.contains(20); 10384d6c458bSopenharmony_ci``` 10394d6c458bSopenharmony_ci48. createDefault() 10404d6c458bSopenharmony_ci``` 10414d6c458bSopenharmony_ciimport util from '@ohos.util' 10424d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10434d6c458bSopenharmony_civar result = pro.createDefault(50); 10444d6c458bSopenharmony_ci``` 10454d6c458bSopenharmony_ci49. entries() 10464d6c458bSopenharmony_ci``` 10474d6c458bSopenharmony_ciimport util from '@ohos.util' 10484d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10494d6c458bSopenharmony_cipro.put(2,10); 10504d6c458bSopenharmony_civar result = pro.entries(); 10514d6c458bSopenharmony_ci``` 10524d6c458bSopenharmony_ci50. \[Symbol.iterator\]() 10534d6c458bSopenharmony_ci``` 10544d6c458bSopenharmony_ciimport util from '@ohos.util' 10554d6c458bSopenharmony_civar pro = new util.LruBuffer(); 10564d6c458bSopenharmony_cipro .put(2,10); 10574d6c458bSopenharmony_civar result = pro[symbol.iterator](); 10584d6c458bSopenharmony_ci``` 10594d6c458bSopenharmony_ci51. afterRemoval() 10604d6c458bSopenharmony_ci``` 10614d6c458bSopenharmony_ciimport util from '@ohos.util' 10624d6c458bSopenharmony_civar arr = [ ]; 10634d6c458bSopenharmony_ciclass ChildLruBuffer extends util.LruBuffer 10644d6c458bSopenharmony_ci{ 10654d6c458bSopenharmony_ci constructor() 10664d6c458bSopenharmony_ci { 10674d6c458bSopenharmony_ci super(); 10684d6c458bSopenharmony_ci } 10694d6c458bSopenharmony_ci static getInstance() 10704d6c458bSopenharmony_ci { 10714d6c458bSopenharmony_ci if(this.instance == null) 10724d6c458bSopenharmony_ci { 10734d6c458bSopenharmony_ci this.instance = new ChildLruBuffer(); 10744d6c458bSopenharmony_ci } 10754d6c458bSopenharmony_ci return this.instance; 10764d6c458bSopenharmony_ci } 10774d6c458bSopenharmony_ci afterRemoval(isEvict, key, value, newValue) 10784d6c458bSopenharmony_ci { 10794d6c458bSopenharmony_ci if (isEvict === false) 10804d6c458bSopenharmony_ci { 10814d6c458bSopenharmony_ci arr = [key, value, newValue]; 10824d6c458bSopenharmony_ci } 10834d6c458bSopenharmony_ci } 10844d6c458bSopenharmony_ci} 10854d6c458bSopenharmony_ciChildLruBuffer.getInstance().afterRemoval(false,10,30,null) 10864d6c458bSopenharmony_ci``` 10874d6c458bSopenharmony_ciA new constructor of **Scope** to implement the **compareTo** method. 10884d6c458bSopenharmony_ci 10894d6c458bSopenharmony_ci``` 10904d6c458bSopenharmony_ciclass Temperature { 10914d6c458bSopenharmony_ci constructor(value) { 10924d6c458bSopenharmony_ci this._temp = value; 10934d6c458bSopenharmony_ci } 10944d6c458bSopenharmony_ci compareTo(value) { 10954d6c458bSopenharmony_ci return this._temp >= value.getTemp(); 10964d6c458bSopenharmony_ci } 10974d6c458bSopenharmony_ci getTemp() { 10984d6c458bSopenharmony_ci return this._temp; 10994d6c458bSopenharmony_ci } 11004d6c458bSopenharmony_ci toString() { 11014d6c458bSopenharmony_ci return this._temp.toString(); 11024d6c458bSopenharmony_ci } 11034d6c458bSopenharmony_ci} 11044d6c458bSopenharmony_ci``` 11054d6c458bSopenharmony_ci 11064d6c458bSopenharmony_ci52. constructor() 11074d6c458bSopenharmony_ci 11084d6c458bSopenharmony_ci``` 11094d6c458bSopenharmony_civar tempLower = new Temperature(30); 11104d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11114d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11124d6c458bSopenharmony_ci``` 11134d6c458bSopenharmony_ci 11144d6c458bSopenharmony_ci53. toString() 11154d6c458bSopenharmony_ci 11164d6c458bSopenharmony_ci``` 11174d6c458bSopenharmony_civar tempLower = new Temperature(30); 11184d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11194d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11204d6c458bSopenharmony_civar result = range.toString() // => [30,40] 11214d6c458bSopenharmony_ci``` 11224d6c458bSopenharmony_ci 11234d6c458bSopenharmony_ci54. intersect() 11244d6c458bSopenharmony_ci 11254d6c458bSopenharmony_ci``` 11264d6c458bSopenharmony_civar tempLower = new Temperature(30); 11274d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11284d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11294d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 11304d6c458bSopenharmony_civar tempMidS = new Temperature(39); 11314d6c458bSopenharmony_civar rangeFir = new Scope(tempMiDF, tempMidS); 11324d6c458bSopenharmony_civar result = range.intersect(rangeFir) // => [35,39] 11334d6c458bSopenharmony_ci``` 11344d6c458bSopenharmony_ci 11354d6c458bSopenharmony_ci55. intersect() 11364d6c458bSopenharmony_ci 11374d6c458bSopenharmony_ci``` 11384d6c458bSopenharmony_civar tempLower = new Temperature(30); 11394d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11404d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 11414d6c458bSopenharmony_civar tempMidS = new Temperature(39); 11424d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11434d6c458bSopenharmony_civar result = range.intersect(tempMiDF, tempMidS) // => [35,39] 11444d6c458bSopenharmony_ci``` 11454d6c458bSopenharmony_ci 11464d6c458bSopenharmony_ci56. getUpper() 11474d6c458bSopenharmony_ci 11484d6c458bSopenharmony_ci``` 11494d6c458bSopenharmony_civar tempLower = new Temperature(30); 11504d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11514d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11524d6c458bSopenharmony_civar result = range.getUpper() // => 40 11534d6c458bSopenharmony_ci``` 11544d6c458bSopenharmony_ci 11554d6c458bSopenharmony_ci57. getLower() 11564d6c458bSopenharmony_ci 11574d6c458bSopenharmony_ci``` 11584d6c458bSopenharmony_civar tempLower = new Temperature(30); 11594d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11604d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11614d6c458bSopenharmony_civar result = range.getLower() // => 30 11624d6c458bSopenharmony_ci``` 11634d6c458bSopenharmony_ci 11644d6c458bSopenharmony_ci58. expand() 11654d6c458bSopenharmony_ci 11664d6c458bSopenharmony_ci``` 11674d6c458bSopenharmony_civar tempLower = new Temperature(30); 11684d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11694d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 11704d6c458bSopenharmony_civar tempMidS = new Temperature(39); 11714d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11724d6c458bSopenharmony_civar result = range.expand(tempMiDF, tempMidS) // => [30,40] 11734d6c458bSopenharmony_ci``` 11744d6c458bSopenharmony_ci 11754d6c458bSopenharmony_ci59. expand() 11764d6c458bSopenharmony_ci 11774d6c458bSopenharmony_ci``` 11784d6c458bSopenharmony_civar tempLower = new Temperature(30); 11794d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11804d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 11814d6c458bSopenharmony_civar tempMidS = new Temperature(39); 11824d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11834d6c458bSopenharmony_civar rangeFir = new Scope(tempMiDF, tempMidS); 11844d6c458bSopenharmony_civar result = range.expand(rangeFir) // => [30,40] 11854d6c458bSopenharmony_ci``` 11864d6c458bSopenharmony_ci 11874d6c458bSopenharmony_ci60. expand() 11884d6c458bSopenharmony_ci 11894d6c458bSopenharmony_ci``` 11904d6c458bSopenharmony_civar tempLower = new Temperature(30); 11914d6c458bSopenharmony_civar tempUpper = new Temperature(40); 11924d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 11934d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 11944d6c458bSopenharmony_civar result = range.expand(tempMiDF) // => [30,40] 11954d6c458bSopenharmony_ci``` 11964d6c458bSopenharmony_ci 11974d6c458bSopenharmony_ci61. contains() 11984d6c458bSopenharmony_ci 11994d6c458bSopenharmony_ci``` 12004d6c458bSopenharmony_civar tempLower = new Temperature(30); 12014d6c458bSopenharmony_civar tempUpper = new Temperature(40); 12024d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 12034d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 12044d6c458bSopenharmony_civar result = range.contains(tempMiDF) // => true 12054d6c458bSopenharmony_ci``` 12064d6c458bSopenharmony_ci 12074d6c458bSopenharmony_ci62. contains() 12084d6c458bSopenharmony_ci 12094d6c458bSopenharmony_ci``` 12104d6c458bSopenharmony_civar tempLower = new Temperature(30); 12114d6c458bSopenharmony_civar tempUpper = new Temperature(40); 12124d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 12134d6c458bSopenharmony_civar tempLess = new Temperature(20); 12144d6c458bSopenharmony_civar tempMore = new Temperature(45); 12154d6c458bSopenharmony_civar rangeSec = new Scope(tempLess, tempMore); 12164d6c458bSopenharmony_civar result = range.contains(rangeSec) // => true 12174d6c458bSopenharmony_ci``` 12184d6c458bSopenharmony_ci 12194d6c458bSopenharmony_ci63. clamp() 12204d6c458bSopenharmony_ci 12214d6c458bSopenharmony_ci``` 12224d6c458bSopenharmony_civar tempLower = new Temperature(30); 12234d6c458bSopenharmony_civar tempUpper = new Temperature(40); 12244d6c458bSopenharmony_civar tempMiDF = new Temperature(35); 12254d6c458bSopenharmony_civar range = new Scope(tempLower, tempUpper); 12264d6c458bSopenharmony_civar result = range.clamp(tempMiDF) // => 35 12274d6c458bSopenharmony_ci``` 12284d6c458bSopenharmony_ci64. isAnyArrayBuffer() 12294d6c458bSopenharmony_ci``` 12304d6c458bSopenharmony_ciimport util from '@ohos.util' 12314d6c458bSopenharmony_civar proc = new util.Types(); 12324d6c458bSopenharmony_civar result = proc.isAnyArrayBuffer(new ArrayBuffer([])) 12334d6c458bSopenharmony_ci``` 12344d6c458bSopenharmony_ci65. isArrayBufferView() 12354d6c458bSopenharmony_ci``` 12364d6c458bSopenharmony_ciimport util from '@ohos.util' 12374d6c458bSopenharmony_civar proc = new util.Types(); 12384d6c458bSopenharmony_civar result = proc.isArrayBufferView(new DataView(new ArrayBuffer(16))); 12394d6c458bSopenharmony_ci``` 12404d6c458bSopenharmony_ci66. isArgumentsObject() 12414d6c458bSopenharmony_ci``` 12424d6c458bSopenharmony_ciimport util from '@ohos.util' 12434d6c458bSopenharmony_cifunction foo() { 12444d6c458bSopenharmony_ci var result = proc.isArgumentsObject(arguments); 12454d6c458bSopenharmony_ci } 12464d6c458bSopenharmony_civar f = foo(); 12474d6c458bSopenharmony_ci``` 12484d6c458bSopenharmony_ci67. isArrayBuffer() 12494d6c458bSopenharmony_ci``` 12504d6c458bSopenharmony_ciimport util from '@ohos.util' 12514d6c458bSopenharmony_civar proc = new util.Types(); 12524d6c458bSopenharmony_civar result = proc.isArrayBuffer(new ArrayBuffer([])); 12534d6c458bSopenharmony_ci``` 12544d6c458bSopenharmony_ci68. isAsyncFunction() 12554d6c458bSopenharmony_ci``` 12564d6c458bSopenharmony_ciimport util from '@ohos.util' 12574d6c458bSopenharmony_civar proc = new util.Types(); 12584d6c458bSopenharmony_civar result = proc.isAsyncFunction(async function foo() {}); 12594d6c458bSopenharmony_ci``` 12604d6c458bSopenharmony_ci69. isBigInt64Array() 12614d6c458bSopenharmony_ci``` 12624d6c458bSopenharmony_ciimport util from '@ohos.util' 12634d6c458bSopenharmony_civar proc = new util.Types(); 12644d6c458bSopenharmony_civar result = proc.isBigInt64Array(new Int16Array([])); 12654d6c458bSopenharmony_ci``` 12664d6c458bSopenharmony_ci70. isBigUint64Array() 12674d6c458bSopenharmony_ci``` 12684d6c458bSopenharmony_ciimport util from '@ohos.util' 12694d6c458bSopenharmony_civar proc = new util.Types(); 12704d6c458bSopenharmony_civar result = proc.isBigUint64Array(new Int16Array([])); 12714d6c458bSopenharmony_ci``` 12724d6c458bSopenharmony_ci71. isBooleanObject() 12734d6c458bSopenharmony_ci``` 12744d6c458bSopenharmony_ciimport util from '@ohos.util' 12754d6c458bSopenharmony_civar proc = new util.Types(); 12764d6c458bSopenharmony_civar result = proc.isBooleanObject(new Boolean(false)); 12774d6c458bSopenharmony_ci``` 12784d6c458bSopenharmony_ci72. isBoxedPrimitive() 12794d6c458bSopenharmony_ci``` 12804d6c458bSopenharmony_ciimport util from '@ohos.util' 12814d6c458bSopenharmony_civar proc = new util.Types(); 12824d6c458bSopenharmony_civar result = proc.isBoxedPrimitive(new Boolean(false)); 12834d6c458bSopenharmony_ci``` 12844d6c458bSopenharmony_ci73. isDataView() 12854d6c458bSopenharmony_ci``` 12864d6c458bSopenharmony_ciimport util from '@ohos.util' 12874d6c458bSopenharmony_civar proc = new util.Types(); 12884d6c458bSopenharmony_ciconst ab = new ArrayBuffer(20); 12894d6c458bSopenharmony_civar result = proc.isDataView(new DataView(ab)); 12904d6c458bSopenharmony_ci``` 12914d6c458bSopenharmony_ci74. isDate() 12924d6c458bSopenharmony_ci``` 12934d6c458bSopenharmony_ciimport util from '@ohos.util' 12944d6c458bSopenharmony_civar proc = new util.Types(); 12954d6c458bSopenharmony_civar result = proc.isDate(new Date()); 12964d6c458bSopenharmony_ci``` 12974d6c458bSopenharmony_ci75. isExternal() 12984d6c458bSopenharmony_ci``` 12994d6c458bSopenharmony_ciimport util from '@ohos.util' 13004d6c458bSopenharmony_ciconst data = util.createExternalType(); 13014d6c458bSopenharmony_civar reult13 = proc.isExternal(data); 13024d6c458bSopenharmony_ci``` 13034d6c458bSopenharmony_ci76. isFloat32Array() 13044d6c458bSopenharmony_ci``` 13054d6c458bSopenharmony_ciimport util from '@ohos.util' 13064d6c458bSopenharmony_civar proc = new util.Types(); 13074d6c458bSopenharmony_civar result = proc.isFloat32Array(new Float32Array([])); 13084d6c458bSopenharmony_ci``` 13094d6c458bSopenharmony_ci77. isFloat64Array() 13104d6c458bSopenharmony_ci``` 13114d6c458bSopenharmony_ciimport util from '@ohos.util' 13124d6c458bSopenharmony_civar proc = new util.Types(); 13134d6c458bSopenharmony_civar result = proc.isFloat64Array(new Float64Array([])); 13144d6c458bSopenharmony_ci``` 13154d6c458bSopenharmony_ci78. isGeneratorFunction() 13164d6c458bSopenharmony_ci``` 13174d6c458bSopenharmony_ciimport util from '@ohos.util' 13184d6c458bSopenharmony_civar proc = new util.Types(); 13194d6c458bSopenharmony_civar result = proc.isGeneratorFunction(function* foo() {}); 13204d6c458bSopenharmony_ci``` 13214d6c458bSopenharmony_ci79. isGeneratorObject() 13224d6c458bSopenharmony_ci``` 13234d6c458bSopenharmony_ciimport util from '@ohos.util' 13244d6c458bSopenharmony_civar proc = new util.Types(); 13254d6c458bSopenharmony_cifunction* foo() {} 13264d6c458bSopenharmony_ciconst generator = foo(); 13274d6c458bSopenharmony_civar result = proc.isGeneratorObject(generator); 13284d6c458bSopenharmony_ci``` 13294d6c458bSopenharmony_ci80. isInt8Array() 13304d6c458bSopenharmony_ci``` 13314d6c458bSopenharmony_ciimport util from '@ohos.util' 13324d6c458bSopenharmony_civar proc = new util.Types(); 13334d6c458bSopenharmony_civar result = proc.isInt8Array(new Int8Array([])); 13344d6c458bSopenharmony_ci``` 13354d6c458bSopenharmony_ci81. isInt16Array() 13364d6c458bSopenharmony_ci``` 13374d6c458bSopenharmony_ciimport util from '@ohos.util' 13384d6c458bSopenharmony_civar proc = new util.Types(); 13394d6c458bSopenharmony_civar result = proc.isInt16Array(new Int16Array([])); 13404d6c458bSopenharmony_ci``` 13414d6c458bSopenharmony_ci82. isInt32Array() 13424d6c458bSopenharmony_ci``` 13434d6c458bSopenharmony_ciimport util from '@ohos.util' 13444d6c458bSopenharmony_civar proc = new util.Types(); 13454d6c458bSopenharmony_civar result = proc.isInt32Array(new Int32Array([])); 13464d6c458bSopenharmony_ci``` 13474d6c458bSopenharmony_ci83. isMap() 13484d6c458bSopenharmony_ci``` 13494d6c458bSopenharmony_ciimport util from '@ohos.util' 13504d6c458bSopenharmony_civar proc = new util.Types(); 13514d6c458bSopenharmony_civar result = proc.isMap(new Map()); 13524d6c458bSopenharmony_ci``` 13534d6c458bSopenharmony_ci84. isMapIterator() 13544d6c458bSopenharmony_ci``` 13554d6c458bSopenharmony_ciimport util from '@ohos.util' 13564d6c458bSopenharmony_civar proc = new util.Types(); 13574d6c458bSopenharmony_civar result = proc.isMapIterator(map.keys()); 13584d6c458bSopenharmony_ci``` 13594d6c458bSopenharmony_ci85. isModuleNamespaceObject() 13604d6c458bSopenharmony_ci``` 13614d6c458bSopenharmony_ciimport util from '@ohos.util' 13624d6c458bSopenharmony_civar proc = new util.Types(); 13634d6c458bSopenharmony_civar result = proc.isModuleNamespaceObject(util); 13644d6c458bSopenharmony_ci``` 13654d6c458bSopenharmony_ci86. isNativeError() 13664d6c458bSopenharmony_ci``` 13674d6c458bSopenharmony_ciimport util from '@ohos.util' 13684d6c458bSopenharmony_civar proc = new util.Types(); 13694d6c458bSopenharmony_civar result = proc.isNativeError(new TypeError()); 13704d6c458bSopenharmony_ci``` 13714d6c458bSopenharmony_ci87. isNumberObject() 13724d6c458bSopenharmony_ci``` 13734d6c458bSopenharmony_ciimport util from '@ohos.util' 13744d6c458bSopenharmony_civar proc = new util.Types(); 13754d6c458bSopenharmony_civar result = proc.isNumberObject(new Number(0)); 13764d6c458bSopenharmony_ci``` 13774d6c458bSopenharmony_ci88. isPromise() 13784d6c458bSopenharmony_ci``` 13794d6c458bSopenharmony_ciimport util from '@ohos.util' 13804d6c458bSopenharmony_civar proc = new util.Types(); 13814d6c458bSopenharmony_civar result = proc.isPromise(Promise.resolve(42)); 13824d6c458bSopenharmony_ci``` 13834d6c458bSopenharmony_ci89. isProxy() 13844d6c458bSopenharmony_ci``` 13854d6c458bSopenharmony_ciimport util from '@ohos.util' 13864d6c458bSopenharmony_civar proc = new util.Types(); 13874d6c458bSopenharmony_ciconst target = {}; 13884d6c458bSopenharmony_ciconst proxy = new Proxy(target, {}); 13894d6c458bSopenharmony_civar result = proc.isProxy(proxy); 13904d6c458bSopenharmony_ci``` 13914d6c458bSopenharmony_ci90. isRegExp() 13924d6c458bSopenharmony_ci``` 13934d6c458bSopenharmony_ciimport util from '@ohos.util' 13944d6c458bSopenharmony_civar proc = new util.Types(); 13954d6c458bSopenharmony_civar result = proc.isRegExp(new RegExp('abc')); 13964d6c458bSopenharmony_ci``` 13974d6c458bSopenharmony_ci91. isSet() 13984d6c458bSopenharmony_ci``` 13994d6c458bSopenharmony_ciimport util from '@ohos.util' 14004d6c458bSopenharmony_civar proc = new util.Types(); 14014d6c458bSopenharmony_civar result = proc.isSet(new Set()); 14024d6c458bSopenharmony_ci``` 14034d6c458bSopenharmony_ci92. isSetIterator() 14044d6c458bSopenharmony_ci``` 14054d6c458bSopenharmony_ciimport util from '@ohos.util' 14064d6c458bSopenharmony_civar proc = new util.Types(); 14074d6c458bSopenharmony_ciconst set = new Set(); 14084d6c458bSopenharmony_civar result = proc.isSetIterator(set.keys()); 14094d6c458bSopenharmony_ci``` 14104d6c458bSopenharmony_ci93. isSharedArrayBuffer() 14114d6c458bSopenharmony_ci``` 14124d6c458bSopenharmony_ciimport util from '@ohos.util' 14134d6c458bSopenharmony_civar proc = new util.Types(); 14144d6c458bSopenharmony_civar result = proc.isSharedArrayBuffer(new ArrayBuffer([])); 14154d6c458bSopenharmony_ci``` 14164d6c458bSopenharmony_ci94. isStringObject() 14174d6c458bSopenharmony_ci``` 14184d6c458bSopenharmony_ciimport util from '@ohos.util' 14194d6c458bSopenharmony_civar proc = new util.Types(); 14204d6c458bSopenharmony_civar result = proc.isStringObject(new String('foo')); 14214d6c458bSopenharmony_ci``` 14224d6c458bSopenharmony_ci95. isSymbolObject() 14234d6c458bSopenharmony_ci``` 14244d6c458bSopenharmony_ciimport util from '@ohos.util' 14254d6c458bSopenharmony_civar proc = new util.Types(); 14264d6c458bSopenharmony_ciconst symbols = Symbol('foo'); 14274d6c458bSopenharmony_civar result = proc.isSymbolObject(Object(symbols)); 14284d6c458bSopenharmony_ci``` 14294d6c458bSopenharmony_ci96. isTypedArray() 14304d6c458bSopenharmony_ci``` 14314d6c458bSopenharmony_ciimport util from '@ohos.util' 14324d6c458bSopenharmony_civar proc = new util.Types(); 14334d6c458bSopenharmony_civar result = proc.isTypedArray(new Float64Array([])); 14344d6c458bSopenharmony_ci``` 14354d6c458bSopenharmony_ci97. isUint8Array() 14364d6c458bSopenharmony_ci``` 14374d6c458bSopenharmony_ciimport util from '@ohos.util' 14384d6c458bSopenharmony_civar proc = new util.Types(); 14394d6c458bSopenharmony_civar result = proc.isUint8Array(new Uint8Array([])); 14404d6c458bSopenharmony_ci``` 14414d6c458bSopenharmony_ci98. isUint8ClampedArray() 14424d6c458bSopenharmony_ci``` 14434d6c458bSopenharmony_ciimport util from '@ohos.util' 14444d6c458bSopenharmony_civar proc = new util.Types(); 14454d6c458bSopenharmony_civar result = proc.isUint8ClampedArray(new Uint8ClampedArray([])); 14464d6c458bSopenharmony_ci``` 14474d6c458bSopenharmony_ci99. isUint16Array() 14484d6c458bSopenharmony_ci``` 14494d6c458bSopenharmony_ciimport util from '@ohos.util' 14504d6c458bSopenharmony_civar proc = new util.Types(); 14514d6c458bSopenharmony_civar result = proc.isUint16Array(new Uint16Array([])); 14524d6c458bSopenharmony_ci``` 14534d6c458bSopenharmony_ci100. isUint32Array() 14544d6c458bSopenharmony_ci``` 14554d6c458bSopenharmony_ciimport util from '@ohos.util' 14564d6c458bSopenharmony_civar proc = new util.Types(); 14574d6c458bSopenharmony_civar result = proc.isUint32Array(new Uint32Array([])); 14584d6c458bSopenharmony_ci``` 14594d6c458bSopenharmony_ci101. isWeakMap() 14604d6c458bSopenharmony_ci``` 14614d6c458bSopenharmony_ciimport util from '@ohos.util' 14624d6c458bSopenharmony_civar proc = new util.Types(); 14634d6c458bSopenharmony_civar result = proc.isWeakMap(new WeakMap()); 14644d6c458bSopenharmony_ci``` 14654d6c458bSopenharmony_ci102. isWeakSet() 14664d6c458bSopenharmony_ci``` 14674d6c458bSopenharmony_ciimport util from '@ohos.util' 14684d6c458bSopenharmony_civar proc = new util.Types(); 14694d6c458bSopenharmony_civar result = proc.isWeakSet(new WeakSet()); 14704d6c458bSopenharmony_ci``` 14714d6c458bSopenharmony_ci 14724d6c458bSopenharmony_ci## js_sys_module 14734d6c458bSopenharmony_ci### Introduction 14744d6c458bSopenharmony_cijs_sys_module provides the following classes:<br>**Process**: provides APIs to obtain multiple IDs of a process, obtain and modify the working directory of a process, and exit and stop a process. **childprocess**: provides APIs for a process to obtain the standard input and output of its child processes, send signals, and close its child processes. 14754d6c458bSopenharmony_ci### Directory Structure 14764d6c458bSopenharmony_ci 14774d6c458bSopenharmony_ci``` 14784d6c458bSopenharmony_cicommonlibrary/ets_utils/js_sys_module/ 14794d6c458bSopenharmony_ci├── Class:PROCESS # Process class 14804d6c458bSopenharmony_ci├── Uid # UID attribute 14814d6c458bSopenharmony_ci├── Gid # GID attribute 14824d6c458bSopenharmony_ci├── EUid # EUID attribute 14834d6c458bSopenharmony_ci├── EGid # EGID attribute 14844d6c458bSopenharmony_ci├── Groups # Groups attribute 14854d6c458bSopenharmony_ci├── Pid # PID attribute 14864d6c458bSopenharmony_ci├── Ppid # PPID attribute 14874d6c458bSopenharmony_ci├── chdir() # chdir method 14884d6c458bSopenharmony_ci├── uptime() # uptime method 14894d6c458bSopenharmony_ci├── kill() # kill method 14904d6c458bSopenharmony_ci├── abort() # abort method 14914d6c458bSopenharmony_ci├── on() # on method 14924d6c458bSopenharmony_ci├── tid # tid method 14934d6c458bSopenharmony_ci├── getStartRealtime() # getStartRealtime method 14944d6c458bSopenharmony_ci├── getAvailableCores() # getAvailableCores method 14954d6c458bSopenharmony_ci├── getPastCputime() # getPastCputime method 14964d6c458bSopenharmony_ci├── isIsolatedProcess() # isIsolatedProcess method 14974d6c458bSopenharmony_ci├── is64Bit() # is64Bit method 14984d6c458bSopenharmony_ci├── isAppUid() # isAppUid method 14994d6c458bSopenharmony_ci├── getUidForName() # getUidForName method 15004d6c458bSopenharmony_ci├── getThreadPriority() # getThreadPriority method 15014d6c458bSopenharmony_ci├── getSystemConfig() # getSystemConfig method 15024d6c458bSopenharmony_ci├── getEnvironmentVar() # getEnvironmentVar method 15034d6c458bSopenharmony_ci├── exit() # exit method 15044d6c458bSopenharmony_ci├── cwd() # cwd method 15054d6c458bSopenharmony_ci├── off() # off method 15064d6c458bSopenharmony_ci├── runCmd() # runCmd method 15074d6c458bSopenharmony_ci└─── Class:CHILDPROCESS # ChildProcess class 15084d6c458bSopenharmony_ci ├── close() # close method 15094d6c458bSopenharmony_ci ├── kill() # kill method 15104d6c458bSopenharmony_ci ├── getOutput() # getOutput method 15114d6c458bSopenharmony_ci ├── getErrorOutput() # getErrorOutput method 15124d6c458bSopenharmony_ci ├── wait() # wait method 15134d6c458bSopenharmony_ci ├── killed # killed attribute 15144d6c458bSopenharmony_ci ├── pid # PID attribute 15154d6c458bSopenharmony_ci ├── ppid # PPID attribute 15164d6c458bSopenharmony_ci └── exitCode # exitCode attribute 15174d6c458bSopenharmony_ci|—— Class:CONSOLE 15184d6c458bSopenharmony_ci ├── debug() # debug method 15194d6c458bSopenharmony_ci ├── log() # log method 15204d6c458bSopenharmony_ci ├── info() # info method 15214d6c458bSopenharmony_ci ├── warn() # warn method 15224d6c458bSopenharmony_ci ├── error() # error method 15234d6c458bSopenharmony_ci ├── assert() # assert method 15244d6c458bSopenharmony_ci ├── count() # count method 15254d6c458bSopenharmony_ci ├── countReset() # countReset method 15264d6c458bSopenharmony_ci ├── dir() # dir method 15274d6c458bSopenharmony_ci ├── dirxml() # dirxml method 15284d6c458bSopenharmony_ci ├── group() # group method 15294d6c458bSopenharmony_ci ├── groupCollapsed() # groupCollapsed method 15304d6c458bSopenharmony_ci ├── groupEnd() # groupEnd method 15314d6c458bSopenharmony_ci ├── table() # table method 15324d6c458bSopenharmony_ci ├── time() # time method 15334d6c458bSopenharmony_ci ├── timeEnd() # timeEnd method 15344d6c458bSopenharmony_ci ├── timeLog() # timeLog method 15354d6c458bSopenharmony_ci ├── trace() # trace method 15364d6c458bSopenharmony_ci|—— Class:TIMER 15374d6c458bSopenharmony_ci ├── setInterval() # setInterval method 15384d6c458bSopenharmony_ci ├── setTimeout() # setTimeout method 15394d6c458bSopenharmony_ci ├── clearInterval() # clearInterval method 15404d6c458bSopenharmony_ci ├── clearTimeout() # clearTimeout method 15414d6c458bSopenharmony_ci``` 15424d6c458bSopenharmony_ci 15434d6c458bSopenharmony_ci### How to Use 15444d6c458bSopenharmony_ci 15454d6c458bSopenharmony_ci#### Available APIs 15464d6c458bSopenharmony_ci| API| Description| 15474d6c458bSopenharmony_ci| -------- | -------- | 15484d6c458bSopenharmony_ci| const uid :number | Obtains the user identifier (UID) of this process.| 15494d6c458bSopenharmony_ci| const gid :number | Obtains the group identifier (GID) of this process.| 15504d6c458bSopenharmony_ci| const euid :number | Obtains the effective user identifier (EUID) of this process.| 15514d6c458bSopenharmony_ci| const egid :number | Obtains the effective group identifier (EGID) of this process.| 15524d6c458bSopenharmony_ci| const groups :number[] | Obtains an array with supplementary group IDs.| 15534d6c458bSopenharmony_ci| const pid :number | Obtains the process ID (PID) of this process.| 15544d6c458bSopenharmony_ci| const ppid :number | Obtains the parent process ID (PPID) of this process.| 15554d6c458bSopenharmony_ci| chdir(dir:string) :void | Changes the working directory of this process.| 15564d6c458bSopenharmony_ci| uptime() :number | Obtains the running time of this process, in seconds.| 15574d6c458bSopenharmony_ci| Kill(pid:number, signal:number) :boolean | Sends a signal to the specified process to terminate it. The value **true** means that the signal is sent.| 15584d6c458bSopenharmony_ci| abort() :void | Aborts a process and generates a core file. It will cause the process to exit immediately.| 15594d6c458bSopenharmony_ci| on(type:string ,listener:EventListener) :void | Stores the events triggered by the user.| 15604d6c458bSopenharmony_ci| exit(code:number):void | Terminates this process.| 15614d6c458bSopenharmony_ci| cwd():string | Obtains the working directory of this process.| 15624d6c458bSopenharmony_ci| off(type: string): boolean | Deletes the event stored by the user. The value **true** means that the event is deleted.| 15634d6c458bSopenharmony_ci| runCmd(command: string, options?: { timeout : number, killSignal : number \| string, maxBuffer : number }): ChildProcess |Forks a new process to run a shell command and returns the **ChildProcess** object. **command** indicates the shell command to run, and **options** indicates certain running parameters, such as **timeout**, **killsignal**, and **maxbuffer**, of the child process. If **timeout** is set, the child process sends the **kill** signal after the specified timeout reaches. **maxbuffer** is used to limit the maximum size of **stdout** and **stderr** that can be received.| 15644d6c458bSopenharmony_ci| wait(): Promise\<number>| Waits until the child process ends. It uses a promise to return the exit code of the child process.| 15654d6c458bSopenharmony_ci| getOutput(): Promise\<Uint8Array> | Obtains the standard output of the child process.| 15664d6c458bSopenharmony_ci| getErrorOutput(): Promise\<Uint8Array> | Obtains the standard error output of the child process.| 15674d6c458bSopenharmony_ci| const tid:number | Obtains the TID of the process.| 15684d6c458bSopenharmony_ci| getStartRealtime() :number | Obtains the duration (in milliseconds) from the time the system starts to the time the process starts.| 15694d6c458bSopenharmony_ci| getAvailableCores() :number[] | Obtains the number of CPU cores available for the process on a multi-core device.| 15704d6c458bSopenharmony_ci| getPastCputime() :number | Obtains the CPU time (in milliseconds) from the time the process starts to the current time.| 15714d6c458bSopenharmony_ci| isIsolatedProcess(): boolean | Checks whether this process is isolated.| 15724d6c458bSopenharmony_ci| is64Bit(): boolean | Checks whether this process is running in a 64-bit environment.| 15734d6c458bSopenharmony_ci| isAppUid(v:number): boolean | Checks whether a UID belongs to the current app.| 15744d6c458bSopenharmony_ci| getUidForName(v:string): number | Obtains the process UID based on the process name.| 15754d6c458bSopenharmony_ci| getThreadPriority(v:number): number | Obtains the thread priority based on the TID.| 15764d6c458bSopenharmony_ci| getSystemConfig(name:number): number | Obtains the system configuration based on the configuration name.| 15774d6c458bSopenharmony_ci| getEnvironmentVar(name:string): string | Obtains the value of an environment variable.| 15784d6c458bSopenharmony_ci| close(): void | Closes the child process in running.| 15794d6c458bSopenharmony_ci| kill(signal: number \| string): void | Sends a signal to the specified child process to terminate it.| 15804d6c458bSopenharmony_ci| readonly killed: boolean | Specifies whether the signal is sent. The value **true** means that the signal is sent.| 15814d6c458bSopenharmony_ci| readonly exitCode: number | Indicates the exit code of the child process.| 15824d6c458bSopenharmony_ci| readonly pid: number | Indicates the PID of the child process.| 15834d6c458bSopenharmony_ci| readonly ppid: number | Indicates the PPID of the process.| 15844d6c458bSopenharmony_ci| debug(message: string, ...arguments: any[]): void | print debug information. | 15854d6c458bSopenharmony_ci| log(message: string, ...arguments: any[]): void | print log information. | 15864d6c458bSopenharmony_ci| info(message: string, ...arguments: any[]): void | print info information. | 15874d6c458bSopenharmony_ci| warn(message: string, ...arguments: any[]): void | print warn information. | 15884d6c458bSopenharmony_ci| error(message: string, ...arguments: any[]): void | print error information. | 15894d6c458bSopenharmony_ci| assert(value?: Object, ...arguments: Object[]): void | if value is false,print arguments information. | 15904d6c458bSopenharmony_ci| count(label?: string): void | Counts the label name. | 15914d6c458bSopenharmony_ci| countReset(label?: string): void | Clear the count of label names. | 15924d6c458bSopenharmony_ci| dir(dir?: Object): void | Print the object content. | 15934d6c458bSopenharmony_ci| dirxml(...arguments: Object[]): void | print log information. | 15944d6c458bSopenharmony_ci| group(...arguments: Object[]): void | Indent one group | 15954d6c458bSopenharmony_ci| groupCollapsed(...arguments: Object[]): void | Indent one group | 15964d6c458bSopenharmony_ci| groupEnd(): void | Unindent one group. | 15974d6c458bSopenharmony_ci| table(tableData?: Object): void | Print data in tabular form. | 15984d6c458bSopenharmony_ci| time(label?: string): void | Start the clock. | 15994d6c458bSopenharmony_ci| timeEnd(label?: string): void | End the timer. | 16004d6c458bSopenharmony_ci| timeLog(label?: string, ...arguments: Object[]): void | Print current timing. | 16014d6c458bSopenharmony_ci| trace(...arguments: Object[]): void | Print current stack. | 16024d6c458bSopenharmony_ci| setInterval(handler: Function \| string, delay: number, ...arguments: any[]): number | Timing to call callback function. | 16034d6c458bSopenharmony_ci| setTimeout(handler: Function \| string, delay?: number, ...arguments: any[]): number | Call the callback function when the timing ends. | 16044d6c458bSopenharmony_ci| clearInterval(intervalID?: number): void | Clear the timing callback. | 16054d6c458bSopenharmony_ci| clearTimeout(timeoutID?: number): void |Clear the timing callback. | 16064d6c458bSopenharmony_ci 16074d6c458bSopenharmony_ci#### How to Use 16084d6c458bSopenharmony_ci 16094d6c458bSopenharmony_ciUse the APIs as follows: 16104d6c458bSopenharmony_ci1. uid() 16114d6c458bSopenharmony_ci``` 16124d6c458bSopenharmony_ciuid(){ 16134d6c458bSopenharmony_ci var res = Process.uid; 16144d6c458bSopenharmony_ci} 16154d6c458bSopenharmony_ci``` 16164d6c458bSopenharmony_ci2. gid() 16174d6c458bSopenharmony_ci``` 16184d6c458bSopenharmony_cigid(){ 16194d6c458bSopenharmony_ci var result = Process.gid; 16204d6c458bSopenharmony_ci} 16214d6c458bSopenharmony_ci``` 16224d6c458bSopenharmony_ci3. euid() 16234d6c458bSopenharmony_ci``` 16244d6c458bSopenharmony_cieuid(){ 16254d6c458bSopenharmony_ci var and = Process.euid; 16264d6c458bSopenharmony_ci} 16274d6c458bSopenharmony_ci``` 16284d6c458bSopenharmony_ci4. egid() 16294d6c458bSopenharmony_ci``` 16304d6c458bSopenharmony_ciegid(){ 16314d6c458bSopenharmony_ci var resb = Process.egid; 16324d6c458bSopenharmony_ci} 16334d6c458bSopenharmony_ci``` 16344d6c458bSopenharmony_ci5. groups() 16354d6c458bSopenharmony_ci``` 16364d6c458bSopenharmony_cigroups(){ 16374d6c458bSopenharmony_ci var answer = Process.groups; 16384d6c458bSopenharmony_ci} 16394d6c458bSopenharmony_ci``` 16404d6c458bSopenharmony_ci6. pid() 16414d6c458bSopenharmony_ci``` 16424d6c458bSopenharmony_cipid(){ 16434d6c458bSopenharmony_ci var result = Process.pid; 16444d6c458bSopenharmony_ci} 16454d6c458bSopenharmony_ci``` 16464d6c458bSopenharmony_ci7. ppid() 16474d6c458bSopenharmony_ci``` 16484d6c458bSopenharmony_cippid(){ 16494d6c458bSopenharmony_ci var result = Process.ppid; 16504d6c458bSopenharmony_ci} 16514d6c458bSopenharmony_ci``` 16524d6c458bSopenharmony_ci8. chdir() 16534d6c458bSopenharmony_ci``` 16544d6c458bSopenharmony_cichdir(){ 16554d6c458bSopenharmony_ci Process.chdir("123456"); 16564d6c458bSopenharmony_ci} 16574d6c458bSopenharmony_ci``` 16584d6c458bSopenharmony_ci9. uptime() 16594d6c458bSopenharmony_ci``` 16604d6c458bSopenharmony_ciuptime(){ 16614d6c458bSopenharmony_ci var num = Process.uptime(); 16624d6c458bSopenharmony_ci} 16634d6c458bSopenharmony_ci``` 16644d6c458bSopenharmony_ci10. kill() 16654d6c458bSopenharmony_ci``` 16664d6c458bSopenharmony_cikill(){ 16674d6c458bSopenharmony_ci var ansu = Process.kill(5,23); 16684d6c458bSopenharmony_ci} 16694d6c458bSopenharmony_ci``` 16704d6c458bSopenharmony_ci11. abort() 16714d6c458bSopenharmony_ci``` 16724d6c458bSopenharmony_ciabort(){ 16734d6c458bSopenharmony_ci Process.abort(); 16744d6c458bSopenharmony_ci} 16754d6c458bSopenharmony_ci``` 16764d6c458bSopenharmony_ci12. on() 16774d6c458bSopenharmony_ci``` 16784d6c458bSopenharmony_cion(){ 16794d6c458bSopenharmony_ci function add(num){ 16804d6c458bSopenharmony_ci var value = num + 5; 16814d6c458bSopenharmony_ci return value; 16824d6c458bSopenharmony_ci } 16834d6c458bSopenharmony_ci Process.on("add",add); 16844d6c458bSopenharmony_ci} 16854d6c458bSopenharmony_ci``` 16864d6c458bSopenharmony_ci13. exit() 16874d6c458bSopenharmony_ci``` 16884d6c458bSopenharmony_ciexit(){ 16894d6c458bSopenharmony_ci Process.exit(15); 16904d6c458bSopenharmony_ci} 16914d6c458bSopenharmony_ci``` 16924d6c458bSopenharmony_ci14. Cwd() 16934d6c458bSopenharmony_ci``` 16944d6c458bSopenharmony_ciCwd(){ 16954d6c458bSopenharmony_ci var result = Process.cwd(); 16964d6c458bSopenharmony_ci} 16974d6c458bSopenharmony_ci``` 16984d6c458bSopenharmony_ci15. off() 16994d6c458bSopenharmony_ci 17004d6c458bSopenharmony_ci``` 17014d6c458bSopenharmony_cioff(){ 17024d6c458bSopenharmony_ci var result = Process.off("add"); 17034d6c458bSopenharmony_ci} 17044d6c458bSopenharmony_ci``` 17054d6c458bSopenharmony_ci16. runCmd() 17064d6c458bSopenharmony_ci``` 17074d6c458bSopenharmony_cirunCmd(){ 17084d6c458bSopenharmony_ci var child = process.runCmd('echo abc') 17094d6c458bSopenharmony_ci // killSignal can be a number or string. 17104d6c458bSopenharmony_ci var child = process.runCmd('echo abc;', {killSignal : 'SIGKILL'}); 17114d6c458bSopenharmony_ci var child = process.runCmd('sleep 5; echo abc;', {timeout : 1, killSignal : 9, maxBuffer : 2}) 17124d6c458bSopenharmony_ci} 17134d6c458bSopenharmony_ci``` 17144d6c458bSopenharmony_ci17. wait() 17154d6c458bSopenharmony_ci``` 17164d6c458bSopenharmony_ciwait() 17174d6c458bSopenharmony_ci{ 17184d6c458bSopenharmony_ci var child = process.runCmd('ls') 17194d6c458bSopenharmony_ci var status = child.wait(); 17204d6c458bSopenharmony_ci status.then(val => { 17214d6c458bSopenharmony_ci console.log(val); 17224d6c458bSopenharmony_ci }) 17234d6c458bSopenharmony_ci} 17244d6c458bSopenharmony_ci``` 17254d6c458bSopenharmony_ci18. getOutput() 17264d6c458bSopenharmony_ci``` 17274d6c458bSopenharmony_cigetOutput(){ 17284d6c458bSopenharmony_ci var child = process.runCmd('echo bcd;'); 17294d6c458bSopenharmony_ci var res = child.getOutput(); 17304d6c458bSopenharmony_ci child.wait(); 17314d6c458bSopenharmony_ci res.then(val => { 17324d6c458bSopenharmony_ci console.log(val); 17334d6c458bSopenharmony_ci }) 17344d6c458bSopenharmony_ci} 17354d6c458bSopenharmony_ci``` 17364d6c458bSopenharmony_ci19. getErrorOutput() 17374d6c458bSopenharmony_ci``` 17384d6c458bSopenharmony_cigetErrorOutput(){ 17394d6c458bSopenharmony_ci var child = process.runCmd('makdir 1.txt'); // execute an error command 17404d6c458bSopenharmony_ci var res = child.getErrorOutput(); 17414d6c458bSopenharmony_ci child.wait(); 17424d6c458bSopenharmony_ci res.then(val => { 17434d6c458bSopenharmony_ci console.log(val); 17444d6c458bSopenharmony_ci }) 17454d6c458bSopenharmony_ci} 17464d6c458bSopenharmony_ci``` 17474d6c458bSopenharmony_ci20. close() 17484d6c458bSopenharmony_ci``` 17494d6c458bSopenharmony_ciclose(){ 17504d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5s;') 17514d6c458bSopenharmony_ci var result = child.close() 17524d6c458bSopenharmony_ci} 17534d6c458bSopenharmony_ci``` 17544d6c458bSopenharmony_ci21. kill() 17554d6c458bSopenharmony_ci``` 17564d6c458bSopenharmony_cikill(){ 17574d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5s;') 17584d6c458bSopenharmony_ci var result = child.kill('SIGHUP'); 17594d6c458bSopenharmony_ci child.wait(); 17604d6c458bSopenharmony_ci var temp = child.killed; 17614d6c458bSopenharmony_ci} 17624d6c458bSopenharmony_ci``` 17634d6c458bSopenharmony_ci22. killed 17644d6c458bSopenharmony_ci``` 17654d6c458bSopenharmony_ci{ 17664d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5;') 17674d6c458bSopenharmony_ci child.kill(3); 17684d6c458bSopenharmony_ci var killed_ = child.killed; 17694d6c458bSopenharmony_ci child.wait(); 17704d6c458bSopenharmony_ci} 17714d6c458bSopenharmony_ci``` 17724d6c458bSopenharmony_ci23. exitCode 17734d6c458bSopenharmony_ci``` 17744d6c458bSopenharmony_ci{ 17754d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5;') 17764d6c458bSopenharmony_ci child.kill(9); 17774d6c458bSopenharmony_ci child.wait(); 17784d6c458bSopenharmony_ci var exitCode_ = child.exitCode; 17794d6c458bSopenharmony_ci} 17804d6c458bSopenharmony_ci``` 17814d6c458bSopenharmony_ci24. pid 17824d6c458bSopenharmony_ci``` 17834d6c458bSopenharmony_cipid 17844d6c458bSopenharmony_ci{ 17854d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5;') 17864d6c458bSopenharmony_ci var pid_ = child.pid; 17874d6c458bSopenharmony_ci child.wait(); 17884d6c458bSopenharmony_ci} 17894d6c458bSopenharmony_ci``` 17904d6c458bSopenharmony_ci25. ppid 17914d6c458bSopenharmony_ci``` 17924d6c458bSopenharmony_cippid 17934d6c458bSopenharmony_ci{ 17944d6c458bSopenharmony_ci var child = process.runCmd('ls; sleep 5;') 17954d6c458bSopenharmony_ci var ppid_ = child.ppid; 17964d6c458bSopenharmony_ci child.wait(); 17974d6c458bSopenharmony_ci} 17984d6c458bSopenharmony_ci``` 17994d6c458bSopenharmony_ci26. tid 18004d6c458bSopenharmony_ci``` 18014d6c458bSopenharmony_citid(){ 18024d6c458bSopenharmony_ci var ansu = Process.tid; 18034d6c458bSopenharmony_ci} 18044d6c458bSopenharmony_ci``` 18054d6c458bSopenharmony_ci27. isIsolatedProcess() 18064d6c458bSopenharmony_ci``` 18074d6c458bSopenharmony_ciisIsolatedProcess(){ 18084d6c458bSopenharmony_ci var ansu = Process.isIsolatedProcess()(); 18094d6c458bSopenharmony_ci} 18104d6c458bSopenharmony_ci``` 18114d6c458bSopenharmony_ci28. isAppUid() 18124d6c458bSopenharmony_ci``` 18134d6c458bSopenharmony_ciisAppUid(){ 18144d6c458bSopenharmony_ci var ansu = Process.isAppUid(10000); 18154d6c458bSopenharmony_ci} 18164d6c458bSopenharmony_ci``` 18174d6c458bSopenharmony_ci29. is64Bit() 18184d6c458bSopenharmony_ci``` 18194d6c458bSopenharmony_ciis64Bit(){ 18204d6c458bSopenharmony_ci var ansu = Process.is64Bit(); 18214d6c458bSopenharmony_ci} 18224d6c458bSopenharmony_ci``` 18234d6c458bSopenharmony_ci30. getUidForName() 18244d6c458bSopenharmony_ci``` 18254d6c458bSopenharmony_cigetUidForName(){ 18264d6c458bSopenharmony_ci var buf = "root"; 18274d6c458bSopenharmony_ci var ansu = Process.getUidForName(buf); 18284d6c458bSopenharmony_ci} 18294d6c458bSopenharmony_ci``` 18304d6c458bSopenharmony_ci31. getEnvironmentVar() 18314d6c458bSopenharmony_ci``` 18324d6c458bSopenharmony_cigetEnvironmentVar(){ 18334d6c458bSopenharmony_ci var ansu = Process.getEnvironmentVar('USER'); 18344d6c458bSopenharmony_ci} 18354d6c458bSopenharmony_ci``` 18364d6c458bSopenharmony_ci32. getAvailableCores() 18374d6c458bSopenharmony_ci``` 18384d6c458bSopenharmony_cigetAvailableCores(){ 18394d6c458bSopenharmony_ci var ansu = Process.getAvailableCores(); 18404d6c458bSopenharmony_ci} 18414d6c458bSopenharmony_ci``` 18424d6c458bSopenharmony_ci33. getThreadPriority() 18434d6c458bSopenharmony_ci``` 18444d6c458bSopenharmony_cigetThreadPriority(){ 18454d6c458bSopenharmony_ci var result = Process.getTid(); 18464d6c458bSopenharmony_ci var ansu = getThreadPriority(result); 18474d6c458bSopenharmony_ci} 18484d6c458bSopenharmony_ci``` 18494d6c458bSopenharmony_ci34. getStartRealtime() 18504d6c458bSopenharmony_ci``` 18514d6c458bSopenharmony_cigetStartRealtime(){ 18524d6c458bSopenharmony_ci var ansu = Process.getStartRealtime(); 18534d6c458bSopenharmony_ci} 18544d6c458bSopenharmony_ci``` 18554d6c458bSopenharmony_ci35. getPastCputime() 18564d6c458bSopenharmony_ci``` 18574d6c458bSopenharmony_cigetPastCputime(){ 18584d6c458bSopenharmony_ci var ansu = Process.getPastCputime(); 18594d6c458bSopenharmony_ci} 18604d6c458bSopenharmony_ci``` 18614d6c458bSopenharmony_ci36. getSystemConfig() 18624d6c458bSopenharmony_ci``` 18634d6c458bSopenharmony_cigetSystemConfig(){ 18644d6c458bSopenharmony_ci var _SC_ARG_MAX = 0; 18654d6c458bSopenharmony_ci var ansu = Process.getSystemConfig(_SC_ARG_MAX) 18664d6c458bSopenharmony_ci} 18674d6c458bSopenharmony_ci``` 18684d6c458bSopenharmony_ci37.console.debug() 18694d6c458bSopenharmony_ci``` 18704d6c458bSopenharmony_ciconsole.debug("print debug log"); 18714d6c458bSopenharmony_ci} 18724d6c458bSopenharmony_ci``` 18734d6c458bSopenharmony_ci38.console.log() 18744d6c458bSopenharmony_ci``` 18754d6c458bSopenharmony_ciconsole.debug("print log"); 18764d6c458bSopenharmony_ci} 18774d6c458bSopenharmony_ci``` 18784d6c458bSopenharmony_ci39.console.info() 18794d6c458bSopenharmony_ci``` 18804d6c458bSopenharmony_ciconsole.debug("print info log"); 18814d6c458bSopenharmony_ci} 18824d6c458bSopenharmony_ci``` 18834d6c458bSopenharmony_ci40.console.warn() 18844d6c458bSopenharmony_ci``` 18854d6c458bSopenharmony_ciconsole.debug("print warn log"); 18864d6c458bSopenharmony_ci} 18874d6c458bSopenharmony_ci``` 18884d6c458bSopenharmony_ci41.console.error() 18894d6c458bSopenharmony_ci``` 18904d6c458bSopenharmony_ciconsole.debug("print error log"); 18914d6c458bSopenharmony_ci} 18924d6c458bSopenharmony_ci``` 18934d6c458bSopenharmony_ci42.console.assert() 18944d6c458bSopenharmony_ci``` 18954d6c458bSopenharmony_cifor (let number = 2; number <= 5; number++) { 18964d6c458bSopenharmony_ci console.assert(number % 2 === 0, "error"); 18974d6c458bSopenharmony_ci} 18984d6c458bSopenharmony_ci``` 18994d6c458bSopenharmony_ci43.console.count() 19004d6c458bSopenharmony_ci``` 19014d6c458bSopenharmony_ciconsole.count("myObj"); 19024d6c458bSopenharmony_ci``` 19034d6c458bSopenharmony_ci44.console.countReset() 19044d6c458bSopenharmony_ci``` 19054d6c458bSopenharmony_ciconsole.count("myObj"); 19064d6c458bSopenharmony_ciconsole.countReset("myObj"); 19074d6c458bSopenharmony_ci``` 19084d6c458bSopenharmony_ci45.console.dir() 19094d6c458bSopenharmony_ci``` 19104d6c458bSopenharmony_cifunction cat(name, age, score){ 19114d6c458bSopenharmony_ci this.name = name; 19124d6c458bSopenharmony_ci this.age = age; 19134d6c458bSopenharmony_ci this.score = score; 19144d6c458bSopenharmony_ci} 19154d6c458bSopenharmony_civar c = new cat("ohos", 2, [6,8,7]); 19164d6c458bSopenharmony_ciconsole.dir(c); 19174d6c458bSopenharmony_ci``` 19184d6c458bSopenharmony_ci46.console.dirxml() 19194d6c458bSopenharmony_ci``` 19204d6c458bSopenharmony_ciconsole.xml("print log"); 19214d6c458bSopenharmony_ci``` 19224d6c458bSopenharmony_ci47.console.group() 19234d6c458bSopenharmony_ci``` 19244d6c458bSopenharmony_ciconsole.group(); 19254d6c458bSopenharmony_ci``` 19264d6c458bSopenharmony_ci48.console.groupCollapsed() 19274d6c458bSopenharmony_ci``` 19284d6c458bSopenharmony_ciconsole.groupCollapsed(); 19294d6c458bSopenharmony_ci``` 19304d6c458bSopenharmony_ci49.console.groupEnd() 19314d6c458bSopenharmony_ci``` 19324d6c458bSopenharmony_ciconsole.groupEnd(); 19334d6c458bSopenharmony_ci``` 19344d6c458bSopenharmony_ci50.console.table() 19354d6c458bSopenharmony_ci``` 19364d6c458bSopenharmony_civar languages = { 19374d6c458bSopenharmony_ci csharp: { name: "C#", paradigm: "undefined" }, 19384d6c458bSopenharmony_ci fsharp: { name: "F#", paradigm: "functional" } 19394d6c458bSopenharmony_ci}; 19404d6c458bSopenharmony_ciconsole.table(languages); 19414d6c458bSopenharmony_ci``` 19424d6c458bSopenharmony_ci51.console.time() 19434d6c458bSopenharmony_ci``` 19444d6c458bSopenharmony_ciconsole.time("timer1"); 19454d6c458bSopenharmony_ci``` 19464d6c458bSopenharmony_ci52.console.timeEnd() 19474d6c458bSopenharmony_ci``` 19484d6c458bSopenharmony_ciconsole.time("timer1"); 19494d6c458bSopenharmony_ciconsole.timeEnd("timer1"); 19504d6c458bSopenharmony_ci``` 19514d6c458bSopenharmony_ci53.console.timeLog() 19524d6c458bSopenharmony_ci``` 19534d6c458bSopenharmony_ciconsole.time("timer1"); 19544d6c458bSopenharmony_ciconsole.timeLog("timer1"); 19554d6c458bSopenharmony_ci``` 19564d6c458bSopenharmony_ci54.console.trace() 19574d6c458bSopenharmony_ci``` 19584d6c458bSopenharmony_ciconsole.trace(); 19594d6c458bSopenharmony_ci``` 19604d6c458bSopenharmony_ci55.setInterval() 19614d6c458bSopenharmony_ci``` 19624d6c458bSopenharmony_cifunction callback() { 19634d6c458bSopenharmony_ci console.log("setInterval"); 19644d6c458bSopenharmony_ci}; 19654d6c458bSopenharmony_cisetInterval(callback, 100); 19664d6c458bSopenharmony_ci``` 19674d6c458bSopenharmony_ci55.setTimeout() 19684d6c458bSopenharmony_ci``` 19694d6c458bSopenharmony_cifunction callback() { 19704d6c458bSopenharmony_ci console.log("setTimeout"); 19714d6c458bSopenharmony_ci}; 19724d6c458bSopenharmony_cisetTimeout(callback, 100); 19734d6c458bSopenharmony_ci``` 19744d6c458bSopenharmony_ci55.clearInterval() 19754d6c458bSopenharmony_ci``` 19764d6c458bSopenharmony_cifunction callback() { 19774d6c458bSopenharmony_ci console.log("clearInterval"); 19784d6c458bSopenharmony_ci}; 19794d6c458bSopenharmony_civar myVar = setInterval(callback, 1000); 19804d6c458bSopenharmony_ciclearInterval(myVar); 19814d6c458bSopenharmony_ci``` 19824d6c458bSopenharmony_ci56.clearTimeout() 19834d6c458bSopenharmony_ci``` 19844d6c458bSopenharmony_cifunction callback() { 19854d6c458bSopenharmony_ci console.log("clearTimeout"); 19864d6c458bSopenharmony_ci}; 19874d6c458bSopenharmony_civar myVar = setTimeout(callback, 1000); 19884d6c458bSopenharmony_ciclearTimeout(myVar); 19894d6c458bSopenharmony_ci``` 19904d6c458bSopenharmony_ci 19914d6c458bSopenharmony_ci## js_concurrent_module 19924d6c458bSopenharmony_ci 19934d6c458bSopenharmony_ci### Introduction 19944d6c458bSopenharmony_ci 19954d6c458bSopenharmony_ciThe js_concurrent_module provides the worker thread, which communicates with the host thread through **postMessage**. 19964d6c458bSopenharmony_ci 19974d6c458bSopenharmony_ci### Available APIs 19984d6c458bSopenharmony_ciFor details about the API implementation, see js_worker_module/worker. 19994d6c458bSopenharmony_ci 20004d6c458bSopenharmony_ci#### Worker Object 20014d6c458bSopenharmony_ci 20024d6c458bSopenharmony_ciObject used by the host thread to communicate with the worker thread. 20034d6c458bSopenharmony_ci 20044d6c458bSopenharmony_ci##### APIs 20054d6c458bSopenharmony_ci 20064d6c458bSopenharmony_ci1. 20074d6c458bSopenharmony_ci 20084d6c458bSopenharmony_ci- API 20094d6c458bSopenharmony_ci 20104d6c458bSopenharmony_ci|constructor(scriptURL:string, options?:WorkerOptions) | Constructor used to create a worker object.| 20114d6c458bSopenharmony_ci|---|---| 20124d6c458bSopenharmony_ci 20134d6c458bSopenharmony_ci- Example 20144d6c458bSopenharmony_ci 20154d6c458bSopenharmony_ciYou can use any of the following methods to create a worker thread, depending on whether the **workers** directory that stores the **worker.ts** file is at the same level as the **pages** directory and whether the FA or stage model is in use: 20164d6c458bSopenharmony_ci 20174d6c458bSopenharmony_ci(1) In the FA model, the **workers** and **pages** directories are at the same level. 20184d6c458bSopenharmony_ci``` 20194d6c458bSopenharmony_ciimport worker from "@ohos.worker"; 20204d6c458bSopenharmony_ciconst workerInstance = new worker.Worker("workers/worker.js", {name:"first worker"}); 20214d6c458bSopenharmony_ci``` 20224d6c458bSopenharmony_ci(2) In the FA model, the **workers** and **pages** directories are at different levels. 20234d6c458bSopenharmony_ci``` 20244d6c458bSopenharmony_ciimport worker from "@ohos.worker"; 20254d6c458bSopenharmony_ciconst workerInstance = new worker.Worker("../workers/worker.js", {name:"first worker"}); 20264d6c458bSopenharmony_ci``` 20274d6c458bSopenharmony_ci(3) In the stage model, the **workers** and **pages** directories are at the same level. 20284d6c458bSopenharmony_ci``` 20294d6c458bSopenharmony_ciimport worker from "@ohos.worker"; 20304d6c458bSopenharmony_ciconst workerInstance = new worker.Worker('entry/ets/workers/worker.ts'); 20314d6c458bSopenharmony_ci``` 20324d6c458bSopenharmony_ci(4) In the stage model, the **workers** and **pages** directories are at different levels. 20334d6c458bSopenharmony_ci``` 20344d6c458bSopenharmony_ciimport worker from "@ohos.worker"; 20354d6c458bSopenharmony_ciconst workerInstance = new worker.Worker('entry/ets/pages/workers/worker.ts'); 20364d6c458bSopenharmony_ci``` 20374d6c458bSopenharmony_ciscriptURL in the stage model — Description of "entry/ets/workers/worker.ts". 20384d6c458bSopenharmony_ci- **entry**: value of the **name** attribute under **module** in the **module.json5** file. 20394d6c458bSopenharmony_ci- **ets**: programming language in use. 20404d6c458bSopenharmony_ci- **worker.ts**: file for the module. You can also use a **worker.js** file. 20414d6c458bSopenharmony_ci 20424d6c458bSopenharmony_ciDepending on whether the **workers** and **pages** directories are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file. 20434d6c458bSopenharmony_ci 20444d6c458bSopenharmony_ci(1) If the **workers** and **pages** directories are at the same level, the configuration is optional. 20454d6c458bSopenharmony_ci 20464d6c458bSopenharmony_ciIn the FA model: 20474d6c458bSopenharmony_ci``` 20484d6c458bSopenharmony_ci "buildOption": { 20494d6c458bSopenharmony_ci "sourceOption": { 20504d6c458bSopenharmony_ci "workers": [ 20514d6c458bSopenharmony_ci "./src/main/ets/MainAbility/workers/worker.js" 20524d6c458bSopenharmony_ci ] 20534d6c458bSopenharmony_ci } 20544d6c458bSopenharmony_ci } 20554d6c458bSopenharmony_ci``` 20564d6c458bSopenharmony_ciIn the stage model: 20574d6c458bSopenharmony_ci``` 20584d6c458bSopenharmony_ci "buildOption": { 20594d6c458bSopenharmony_ci "sourceOption": { 20604d6c458bSopenharmony_ci "workers": [ 20614d6c458bSopenharmony_ci "./src/main/ets/workers/worker.ts" 20624d6c458bSopenharmony_ci ] 20634d6c458bSopenharmony_ci } 20644d6c458bSopenharmony_ci } 20654d6c458bSopenharmony_ci``` 20664d6c458bSopenharmony_ci(2) If the **workers** and **pages** directories are at different levels, the configuration is mandatory. 20674d6c458bSopenharmony_ci 20684d6c458bSopenharmony_ciIn the FA model: 20694d6c458bSopenharmony_ci``` 20704d6c458bSopenharmony_ci "buildOption": { 20714d6c458bSopenharmony_ci "sourceOption": { 20724d6c458bSopenharmony_ci "workers": [ 20734d6c458bSopenharmony_ci "./src/main/ets/workers/worker.js" 20744d6c458bSopenharmony_ci ] 20754d6c458bSopenharmony_ci } 20764d6c458bSopenharmony_ci } 20774d6c458bSopenharmony_ci``` 20784d6c458bSopenharmony_ciIn the stage model (the following assumes that the **workers** directory is under the **pages** directory): 20794d6c458bSopenharmony_ci``` 20804d6c458bSopenharmony_ci "buildOption": { 20814d6c458bSopenharmony_ci "sourceOption": { 20824d6c458bSopenharmony_ci "workers": [ 20834d6c458bSopenharmony_ci "./src/main/ets/pages/workers/worker.ts" 20844d6c458bSopenharmony_ci ] 20854d6c458bSopenharmony_ci } 20864d6c458bSopenharmony_ci } 20874d6c458bSopenharmony_ci``` 20884d6c458bSopenharmony_ci2. 20894d6c458bSopenharmony_ci 20904d6c458bSopenharmony_ci- API 20914d6c458bSopenharmony_ci 20924d6c458bSopenharmony_ci| postMessage(message:Object, options?:PostMessageOptions) | Sends a message to the worker thread. | 20934d6c458bSopenharmony_ci|---|---| 20944d6c458bSopenharmony_ci| postMessage(message:Object, transfer:ArrayBuffer[]) | Sends a message to the worker thread. | 20954d6c458bSopenharmony_ci 20964d6c458bSopenharmony_ci- Examples 20974d6c458bSopenharmony_ci 20984d6c458bSopenharmony_ci``` 20994d6c458bSopenharmony_ci// Example 1 21004d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21014d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21024d6c458bSopenharmony_ciworker.postMessage("hello world"); 21034d6c458bSopenharmony_ci 21044d6c458bSopenharmony_ci// Example 2 21054d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21064d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21074d6c458bSopenharmony_civar buffer = new ArrayBuffer(8); 21084d6c458bSopenharmony_ciworker.postMessage(buffer, [buffer]); 21094d6c458bSopenharmony_ci``` 21104d6c458bSopenharmony_ci 21114d6c458bSopenharmony_ci3. 21124d6c458bSopenharmony_ci 21134d6c458bSopenharmony_ci- API 21144d6c458bSopenharmony_ci 21154d6c458bSopenharmony_ci| on(type:string, listener:EventListener) | Adds an event listener to the worker. | 21164d6c458bSopenharmony_ci|---|---| 21174d6c458bSopenharmony_ci 21184d6c458bSopenharmony_ci- Examples 21194d6c458bSopenharmony_ci 21204d6c458bSopenharmony_ci``` 21214d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21224d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21234d6c458bSopenharmony_ciworker.on("alert", (e)=>{ 21244d6c458bSopenharmony_ci console.log("worker on..."); 21254d6c458bSopenharmony_ci}); 21264d6c458bSopenharmony_ci``` 21274d6c458bSopenharmony_ci 21284d6c458bSopenharmony_ci4. 21294d6c458bSopenharmony_ci 21304d6c458bSopenharmony_ci- API 21314d6c458bSopenharmony_ci 21324d6c458bSopenharmony_ci| once(type:string, listener:EventListener) | Adds an event listener to the worker and removes the event listener automatically after it is invoked once. | 21334d6c458bSopenharmony_ci|---|---| 21344d6c458bSopenharmony_ci 21354d6c458bSopenharmony_ci- Examples 21364d6c458bSopenharmony_ci 21374d6c458bSopenharmony_ci``` 21384d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21394d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21404d6c458bSopenharmony_ciworker.once("alert", (e)=>{ 21414d6c458bSopenharmony_ci console.log("worker once..."); 21424d6c458bSopenharmony_ci}); 21434d6c458bSopenharmony_ci``` 21444d6c458bSopenharmony_ci 21454d6c458bSopenharmony_ci5. 21464d6c458bSopenharmony_ci 21474d6c458bSopenharmony_ci- API 21484d6c458bSopenharmony_ci 21494d6c458bSopenharmony_ci| off(type:string, listener?:EventListener) | Removes an event listener for the worker. | 21504d6c458bSopenharmony_ci|---|---| 21514d6c458bSopenharmony_ci 21524d6c458bSopenharmony_ci- Examples 21534d6c458bSopenharmony_ci 21544d6c458bSopenharmony_ci``` 21554d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21564d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21574d6c458bSopenharmony_ciworker.off("alert"); 21584d6c458bSopenharmony_ci``` 21594d6c458bSopenharmony_ci 21604d6c458bSopenharmony_ci6. 21614d6c458bSopenharmony_ci 21624d6c458bSopenharmony_ci- API 21634d6c458bSopenharmony_ci 21644d6c458bSopenharmony_ci| terminate() | Terminates the worker thread to stop the worker from receiving messages. | 21654d6c458bSopenharmony_ci|---|---| 21664d6c458bSopenharmony_ci 21674d6c458bSopenharmony_ci- Example 21684d6c458bSopenharmony_ci 21694d6c458bSopenharmony_ci``` 21704d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21714d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21724d6c458bSopenharmony_ciworker.terminate(); 21734d6c458bSopenharmony_ci``` 21744d6c458bSopenharmony_ci 21754d6c458bSopenharmony_ci7. 21764d6c458bSopenharmony_ci 21774d6c458bSopenharmony_ci- API 21784d6c458bSopenharmony_ci 21794d6c458bSopenharmony_ci| removeEventListener(type:string, listener?:EventListener) | Removes an event listener for the worker. | 21804d6c458bSopenharmony_ci|---|---| 21814d6c458bSopenharmony_ci 21824d6c458bSopenharmony_ci- Example 21834d6c458bSopenharmony_ci 21844d6c458bSopenharmony_ci``` 21854d6c458bSopenharmony_ciimport worker from "@ohos.worker" 21864d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 21874d6c458bSopenharmony_ciworker.removeEventListener("alert", (e)=>{ 21884d6c458bSopenharmony_ci console.log("worker removeEventListener..."); 21894d6c458bSopenharmony_ci}); 21904d6c458bSopenharmony_ci``` 21914d6c458bSopenharmony_ci 21924d6c458bSopenharmony_ci8. 21934d6c458bSopenharmony_ci 21944d6c458bSopenharmony_ci- API 21954d6c458bSopenharmony_ci 21964d6c458bSopenharmony_ci| dispatchEvent(event: Event) | Dispatches the event defined for the worker. | 21974d6c458bSopenharmony_ci|---|---| 21984d6c458bSopenharmony_ci 21994d6c458bSopenharmony_ci- Example 22004d6c458bSopenharmony_ci 22014d6c458bSopenharmony_ci``` 22024d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22034d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22044d6c458bSopenharmony_ciworker.dispatchEvent({type:"alert"}); 22054d6c458bSopenharmony_ci``` 22064d6c458bSopenharmony_ci 22074d6c458bSopenharmony_ci9. 22084d6c458bSopenharmony_ci 22094d6c458bSopenharmony_ci- API 22104d6c458bSopenharmony_ci 22114d6c458bSopenharmony_ci| removeAllListener() | Removes all event listeners for the worker. | 22124d6c458bSopenharmony_ci|---|---| 22134d6c458bSopenharmony_ci 22144d6c458bSopenharmony_ci- Example 22154d6c458bSopenharmony_ci 22164d6c458bSopenharmony_ci``` 22174d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22184d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22194d6c458bSopenharmony_ciworker.removeAllListener(); 22204d6c458bSopenharmony_ci``` 22214d6c458bSopenharmony_ci 22224d6c458bSopenharmony_ci##### Attributes 22234d6c458bSopenharmony_ci 22244d6c458bSopenharmony_ci1. 22254d6c458bSopenharmony_ci 22264d6c458bSopenharmony_ci- Attribute 22274d6c458bSopenharmony_ci 22284d6c458bSopenharmony_ci| onexit?:(code:number)=>void | Event handler to be called when the worker thread exits. The handler is executed in the host thread. | 22294d6c458bSopenharmony_ci|---|---| 22304d6c458bSopenharmony_ci 22314d6c458bSopenharmony_ci- Example 22324d6c458bSopenharmony_ci 22334d6c458bSopenharmony_ci``` 22344d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22354d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22364d6c458bSopenharmony_ciworker.onexit = function(e) { 22374d6c458bSopenharmony_ci console.log("onexit..."); 22384d6c458bSopenharmony_ci} 22394d6c458bSopenharmony_ci``` 22404d6c458bSopenharmony_ci 22414d6c458bSopenharmony_ci2. 22424d6c458bSopenharmony_ci 22434d6c458bSopenharmony_ci- Attribute 22444d6c458bSopenharmony_ci 22454d6c458bSopenharmony_ci| onerror?:(ev:ErrorEvent)=>void | Event handler to be called when an exception occurs during worker execution. The event handler is executed in the host thread. | 22464d6c458bSopenharmony_ci|---|---| 22474d6c458bSopenharmony_ci 22484d6c458bSopenharmony_ci- Example 22494d6c458bSopenharmony_ci 22504d6c458bSopenharmony_ci``` 22514d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22524d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22534d6c458bSopenharmony_ciworker.onerror = function(e) { 22544d6c458bSopenharmony_ci console.log("onerror..."); 22554d6c458bSopenharmony_ci} 22564d6c458bSopenharmony_ci``` 22574d6c458bSopenharmony_ci 22584d6c458bSopenharmony_ci3. 22594d6c458bSopenharmony_ci 22604d6c458bSopenharmony_ci- Attribute 22614d6c458bSopenharmony_ci 22624d6c458bSopenharmony_ci| onmessage?:(ev:MessageEvent)=>void | Event handler to be called when the host thread receives a message sent by the worker thread through **parentPort.postMessage**. The event handler is executed in the host thread.| 22634d6c458bSopenharmony_ci|---|---| 22644d6c458bSopenharmony_ci 22654d6c458bSopenharmony_ci- Example 22664d6c458bSopenharmony_ci 22674d6c458bSopenharmony_ci``` 22684d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22694d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22704d6c458bSopenharmony_ciworker.onmessage = function(e) { 22714d6c458bSopenharmony_ci console.log("onmessage..."); 22724d6c458bSopenharmony_ci} 22734d6c458bSopenharmony_ci``` 22744d6c458bSopenharmony_ci 22754d6c458bSopenharmony_ci4. 22764d6c458bSopenharmony_ci 22774d6c458bSopenharmony_ci- Attribute 22784d6c458bSopenharmony_ci 22794d6c458bSopenharmony_ci| onmessageerror?:(event:MessageEvent)=>void | Event handler to be called when the worker thread receives a message that cannot be serialized. The event handler is executed in the host thread.| 22804d6c458bSopenharmony_ci|---|---| 22814d6c458bSopenharmony_ci 22824d6c458bSopenharmony_ci- Example 22834d6c458bSopenharmony_ci 22844d6c458bSopenharmony_ci``` 22854d6c458bSopenharmony_ciimport worker from "@ohos.worker" 22864d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 22874d6c458bSopenharmony_ciworker.onmessageerror = function(e) { 22884d6c458bSopenharmony_ci console.log("onmessageerror..."); 22894d6c458bSopenharmony_ci} 22904d6c458bSopenharmony_ci``` 22914d6c458bSopenharmony_ci 22924d6c458bSopenharmony_ci#### parentPort Object 22934d6c458bSopenharmony_ci 22944d6c458bSopenharmony_ciObject of the worker thread used to communicate with the host thread. 22954d6c458bSopenharmony_ci 22964d6c458bSopenharmony_ci##### APIs 22974d6c458bSopenharmony_ci 22984d6c458bSopenharmony_ci1. 22994d6c458bSopenharmony_ci 23004d6c458bSopenharmony_ci- API 23014d6c458bSopenharmony_ci 23024d6c458bSopenharmony_ci| postMessage(message:Object, options?:PostMessageOptions) | Sends a message to the host thread from the worker thread.| 23034d6c458bSopenharmony_ci|---|---| 23044d6c458bSopenharmony_ci| postMessage(message:Object, transfer:ArrayBuffer[]) | Sends a message to the host thread from the worker thread. | 23054d6c458bSopenharmony_ci 23064d6c458bSopenharmony_ci- Example 23074d6c458bSopenharmony_ci 23084d6c458bSopenharmony_ci``` 23094d6c458bSopenharmony_ci// main.js 23104d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23114d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 23124d6c458bSopenharmony_ciworker.postMessage("hello world"); 23134d6c458bSopenharmony_ci 23144d6c458bSopenharmony_ci// worker.js 23154d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23164d6c458bSopenharmony_ciconst parentPort = worker.parentPort; 23174d6c458bSopenharmony_ciparentPort.onmessage = function(e) { 23184d6c458bSopenharmony_ci parentPort.postMessage("hello world from worker.js"); 23194d6c458bSopenharmony_ci} 23204d6c458bSopenharmony_ci``` 23214d6c458bSopenharmony_ci 23224d6c458bSopenharmony_ci2. 23234d6c458bSopenharmony_ci 23244d6c458bSopenharmony_ci- API 23254d6c458bSopenharmony_ci 23264d6c458bSopenharmony_ci| close() | Terminates the worker thread to stop the worker from receiving messages. | 23274d6c458bSopenharmony_ci|---|---| 23284d6c458bSopenharmony_ci 23294d6c458bSopenharmony_ci- Example 23304d6c458bSopenharmony_ci 23314d6c458bSopenharmony_ci``` 23324d6c458bSopenharmony_ci// main.js 23334d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23344d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 23354d6c458bSopenharmony_ciworker.postMessage("hello world"); 23364d6c458bSopenharmony_ci 23374d6c458bSopenharmony_ci// worker.js 23384d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23394d6c458bSopenharmony_ciconst parentPort = worker.parentPort; 23404d6c458bSopenharmony_ciparentPort.onmessage = function(e) { 23414d6c458bSopenharmony_ci parentPort.close(); 23424d6c458bSopenharmony_ci} 23434d6c458bSopenharmony_ci``` 23444d6c458bSopenharmony_ci 23454d6c458bSopenharmony_ci##### Attributes 23464d6c458bSopenharmony_ci 23474d6c458bSopenharmony_ci1. 23484d6c458bSopenharmony_ci 23494d6c458bSopenharmony_ci- Attribute 23504d6c458bSopenharmony_ci 23514d6c458bSopenharmony_ci| onmessage?:(event:MessageEvent)=>void | Event handler to be called when the host thread receives a message sent by the worker thread through **parentPort.postMessage**. The event handler is executed in the worker thread. | 23524d6c458bSopenharmony_ci|---|---| 23534d6c458bSopenharmony_ci 23544d6c458bSopenharmony_ci- Example 23554d6c458bSopenharmony_ci 23564d6c458bSopenharmony_ci``` 23574d6c458bSopenharmony_ci// main.js 23584d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23594d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 23604d6c458bSopenharmony_ciworker.postMessage("hello world"); 23614d6c458bSopenharmony_ci 23624d6c458bSopenharmony_ci// worker.js 23634d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23644d6c458bSopenharmony_ciconst parentPort = worker.parentPort; 23654d6c458bSopenharmony_ciparentPort.onmessage = function(e) { 23664d6c458bSopenharmony_ci console.log("receive main.js message"); 23674d6c458bSopenharmony_ci} 23684d6c458bSopenharmony_ci``` 23694d6c458bSopenharmony_ci 23704d6c458bSopenharmony_ci2. 23714d6c458bSopenharmony_ci 23724d6c458bSopenharmony_ci- Attribute 23734d6c458bSopenharmony_ci 23744d6c458bSopenharmony_ci| onerror?:(ev: ErrorEvent)=>void | Event handler to be called when an exception occurs during worker execution. The event handler is executed in the worker thread. | 23754d6c458bSopenharmony_ci|---|---| 23764d6c458bSopenharmony_ci 23774d6c458bSopenharmony_ci- Example 23784d6c458bSopenharmony_ci 23794d6c458bSopenharmony_ci``` 23804d6c458bSopenharmony_ci// main.js 23814d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23824d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 23834d6c458bSopenharmony_ciworker.postMessage("hello world"); 23844d6c458bSopenharmony_ci 23854d6c458bSopenharmony_ci// worker.js 23864d6c458bSopenharmony_ciimport worker from "@ohos.worker" 23874d6c458bSopenharmony_ciconst parentPort = worker.parentPort; 23884d6c458bSopenharmony_ciparentPort.onerror = function(e) { 23894d6c458bSopenharmony_ci console.log("onerror..."); 23904d6c458bSopenharmony_ci} 23914d6c458bSopenharmony_ci 23924d6c458bSopenharmony_ci``` 23934d6c458bSopenharmony_ci 23944d6c458bSopenharmony_ci3. 23954d6c458bSopenharmony_ci 23964d6c458bSopenharmony_ci- Attribute 23974d6c458bSopenharmony_ci 23984d6c458bSopenharmony_ci| onmessageerror?:(event: MessageEvent)=>void | Event handler to be called when the worker thread receives a message that cannot be deserialized. The event handler is executed in the worker thread. | 23994d6c458bSopenharmony_ci|---|---| 24004d6c458bSopenharmony_ci 24014d6c458bSopenharmony_ci- Example 24024d6c458bSopenharmony_ci 24034d6c458bSopenharmony_ci``` 24044d6c458bSopenharmony_ci// main.js 24054d6c458bSopenharmony_ciimport worker from "@ohos.worker" 24064d6c458bSopenharmony_ciconst worker = new worker.Worker("workers/worker.js"); 24074d6c458bSopenharmony_ciworker.postMessage("hello world"); 24084d6c458bSopenharmony_ci 24094d6c458bSopenharmony_ci// worker.js 24104d6c458bSopenharmony_ciimport worker from "@ohos.worker" 24114d6c458bSopenharmony_ciconst parentPort = worker.parentPort; 24124d6c458bSopenharmony_ciparentPort.onmessageerror = function(e) { 24134d6c458bSopenharmony_ci console.log("onmessageerror..."); 24144d6c458bSopenharmony_ci} 24154d6c458bSopenharmony_ci``` 24164d6c458bSopenharmony_ci 24174d6c458bSopenharmony_ci 24184d6c458bSopenharmony_ci# Repositories Involved 24194d6c458bSopenharmony_ci 24204d6c458bSopenharmony_ci[arkcompiler_ets_runtime](https://gitee.com/openharmony/arkcompiler_ets_runtime/blob/master/README_zh.md) 24214d6c458bSopenharmony_ci[arkui_ace_engine](https://gitee.com/openharmony/arkui_ace_engine/blob/master/README_zh.md) 24224d6c458bSopenharmony_ci[arkui_napi](https://gitee.com/openharmony/arkui_napi/blob/master/README_zh.md) 24234d6c458bSopenharmony_ci 24244d6c458bSopenharmony_ci# Licenses 24254d6c458bSopenharmony_ci 24264d6c458bSopenharmony_ci**js_api_module** can be used under the [Mozilla Public License](https://www.mozilla.org/en-US/MPL/). For the complete license text, see [License](https://gitee.com/openharmony/commonlibrary_ets_utils/blob/master/js_api_module/mozilla_docs.txt). 24274d6c458bSopenharmony_ci 24284d6c458bSopenharmony_ci**js_util_module** can be used under the [Mozilla Public License](https://www.mozilla.org/en-US/MPL/). For the complete license text, see [License](https://gitee.com/openharmony/commonlibrary_ets_utils/blob/master/js_util_module/mozilla_docs.txt). 24294d6c458bSopenharmony_ci 24304d6c458bSopenharmony_ci**js_concurrent_module** can be used under the [Mozilla Public License](https://www.mozilla.org/en-US/MPL/). For the complete license text, see [License](https://gitee.com/openharmony/commonlibrary_ets_utils/blob/master/js_concurrent_module/mozilla_docs.txt). 24314d6c458bSopenharmony_ci 24324d6c458bSopenharmony_ci**js_sys_module** can be used under the [Mozilla Public License](https://www.mozilla.org/en-US/MPL/). For the complete license text, see [License](https://gitee.com/openharmony/commonlibrary_ets_utils/blob/master/js_sys_module/mozilla_docs.txt).