---
# System prepended metadata

title: 函式和類別
tags: [nodejs]

---

---
title: 函式和類別
tags: nodejs
---

# function
* 有意義的函式名稱
* 小寫開頭的駝峰命名規則

```typescript=
function add(x: number, y: number) {
  return x + y;
}

const subtract = (x: number, y: number) => {
  return x - y;
};

const multiple = (x: number, y: number) => x * y;

const data1 = add(1, 2);
const data2 = subtract(3, 4);
const data3 = multiple(5, 6);

console.log(data1, data2, data3);

```
# 匿名函式

# 箭頭函式

# Class
* 新增mymath.ts
```typescript=
export class mymath {
  add(x: number, y: number) {
    return x + y;
  }

  subtract = (x: number, y: number) => {
    return x - y;
  };

  multiple = (x: number, y: number) => x * y;
}

```
* 修改呼叫方式
```typescript=
import { mymath } from './mymath';

const m = new mymath();
const data1 = m.add(1, 2);
const data2 = m.subtract(3, 4);
const data3 = m.multiple(5, 6);

console.log(data1, data2, data3);
```
