> For the complete documentation index, see [llms.txt](https://petercheng7788.gitbook.io/developer-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://petercheng7788.gitbook.io/developer-note/programming-language/javascript/class.md).

# Class

* Classes in JS are built on [prototypes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) but also have some syntax and semantics that are unique to classes.&#x20;
* Its nature is actually an object with prototypes chain

<pre class="language-javascript"><code class="lang-javascript"><strong>class Animal {
</strong>  constructor(name){
    this._name = name;
  }
  callName(){
    console.log(this._name);
  }
  get name(){
    // not allowed 
    // return this.name;
    return this._name;
  }
  set name(input){
    this._name = input;
  }
}

class Cat extends Animal {
  meow(){
    console.log("meow");
  }
  
}

const mimi = new Cat("mimi");
// inherit from parent
mimi.callName();
// setter
mimi.name = 'mimi1';
// getter
console.log(mimi.name);
</code></pre>

* The `contructor` can also be have return value, but must be in object format

```javascript
class Animal {
  constructor(name){
    this._name = name;
    return {test:1}
  }
}

const mimi = new Animal("mimi");
console.log(mimi); // {test : 1 }
```
