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// FunctionDeclaration 17export function greet(name: string): string { 18 return `Hello, ${name}`; 19} 20 21// MethodDeclaration inside a class 22class Person { 23 private _age: number; 24 // Constructor 25 constructor(public name: string, age: number) { 26 this._age = age; 27 } 28 // MethodDeclaration 29 greet(): string { 30 return `Hi, my name is ${this.name}`; 31 } 32 // GetAccessor 33 get age(): number { 34 return this._age; 35 } 36 // SetAccessor 37 set age(age: number) { 38 if (age > 0) { 39 this._age = age; 40 } 41 } 42} 43 44// FunctionExpression assigned to a variable 45export const add = function(x: number, y: number): number { 46 return x + y; 47}; 48 49// ArrowFunction assigned to a variable 50const multiply = (x: number, y: number): number => x * y; 51 52// Instance of class Person 53const person = new Person("John Doe", 30);