###### tags: `javascript` `oop` `class` `object` # javascript class宣告與物件使用方式 *本篇內容以朝向已經學過python物件導向的人為主* 廢話不多說,先上示範 ```js class Player{ constructor(name, hp, location){ this.name = name this.hp = hp this.location = location } } playerA = new Player("Akka", 100, { x: 100, y: 75 }) ``` 首先,如同python的 `__init__()`魔法函數,js也有這麼一個函數,叫做建構子`constructor`。用法差不多,就是在建立物件的時候class的引數會傳入建構子函數,並且執行建構子裡面的敘述。 在python中,`self`被當作[指涉class自己](https://hackmd.io/lUaXzTV4TSOAGxpFTX-plg)的關鍵字,而在js中`this`與其有著同等的地位。 值得注意的是,在js中經常會出現以字典作為引數的用法,因為js的字典[比起python是滿好用的。](https://hackmd.io/bMpOyC0XTUu-XRSbaInBmA)