• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the 'License');
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an 'AS IS' BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// Data Types
17var name = "Alice";
18let age = 25;
19const pi = 3.14;
20let isSame = true;
21let u = undefined;
22let n = null;
23const mySymbol = Symbol();
24let val1 = 10n;
25let obj = {
26    mySymbol : "prop",
27};
28let arr = [1, 2, 3];
29//Function Type
30var sum = function(num1, num2) {
31    return num1 + num2;
32};
33sum(1, 2);
34// Date Type
35let now = new Date();
36
37console.log(name, age, pi, u, n, mySymbol, val1, obj, arr[1]);
38
39// if Statement
40if (age >= 18) {
41    console.log("Adult");
42} else {
43    console.log("Minor");
44}
45
46// Switch Statement
47let day = "Monday";
48switch (day) {
49    case "Monday":
50      console.log("Start of the week");
51      break;
52    case "Friday":
53      console.log("End of the week");
54      break;
55    default:
56      console.log("Midweek");
57}
58
59// For Statement
60for (let i = 0; i < 5; i++) {
61    console.log(i);
62}
63
64// While Statement
65let i = 0;
66while (i < 5) {
67  console.log(i);
68  i++;
69}
70
71do {
72    console.log(i);
73    i++;
74} while (i < 5);
75
76// Arrow Function
77const greet = (name) => {
78    console.log("Hello, " + name);
79};
80greet("js");
81
82// Object
83const person = {
84    name: "Alice",
85    age: 25,
86    greet() {
87      console.log("Hello, " + this.name);
88    }
89};
90person.greet();
91
92// Template string
93const temp = "Js";
94const greeting = `Hello, ${temp}!`;
95
96// Destructuring assignment
97const student = { gender: "男", grade: "2"};
98const { gender, grade } = person;
99
100// Default parameters
101function defaultGreet(name = "js") {
102    console.log("Hello, " + name);
103}
104defaultGreet();
105
106// Extended operator
107const arr1 = [1, 2, 3];
108const arr2 = [...arr1, 4, 5];
109
110// Class
111class Person {
112    constructor(name, age) {
113      this.name = name;
114      this.age = age;
115    }
116
117    greet() {
118      console.log("Hello, " + this.name);
119    }
120}
121const alice = new Person("Alice", 25);
122alice.greet();
123
124// Asynchronous function
125function fetchData() {
126    return new Promise((resolve, reject) => {
127        setTimeout(() => {
128            resolve('Data fetched successfully!');
129        }, 1000);
130    });
131}
132
133fetchData().then(data => {
134    console.log(data);
135}).catch(error => {
136    console.error('An error occurred:', error);
137});
138
139async function fetchAndDisplayData() {
140    try {
141        const data = await fetchData();
142        console.log(data);
143    } catch (error) {
144        console.error('An error occurred:', error);
145    }
146}
147fetchAndDisplayData();
148
149class TestA {
150	constructor(public value: number) {}
151}
152
153export class ClassA {
154	a = new TestA(100);
155}
156
157// Exception Handling Statement
158a.then(result => {
159	console.error('YY start throw error');
160	console.log('yy execute then');
161}).catch((error: Error) => {
162	console.error('yy occur error');
163}).finally(()=>{
164	console.error('yy occur finally');
165});
166
167// Chain Expression
168let result = obj.
169foo().
170bar[0].
171baz();
172
173// SuperExpression
174class Parent {
175    sayHello(): string {
176        return 'Hello from Parent';
177    }
178}
179
180class Child extends Parent {
181    sayHello(): string {
182        return super.
183        sayHello() + ' and Child';
184    }
185}
186
187const d = new Child();
188console.log(d.sayHello());
189
190// Exception Handling Chain Split
191somePromise
192  .then(value => {
193    console.log('resolved', value);
194  })
195  .
196  catch(err => {
197    console.error('caught', err);
198  }).
199  finally(() => {
200    console.log('cleanup done');
201  });
202
203// Object Property Access Split: Dot Before or After Line Break
204let x = someObj
205.
206very.deep.property
207.
208toString();
209
210let y = someObj.
211prop1?.
212prop2().
213method?.();
214
215// Chained Function Call with Computed Property Split
216let res = getContext().
217  config?.
218  ['someKey'].
219  toLowerCase();