> For the complete documentation index, see [llms.txt](https://nicollaslopes.gitbook.io/estudos/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nicollaslopes.gitbook.io/estudos/javascript/classes.md).

# Classes

* this

Objetos não contem um escopo próprio.

Fazendo dessa forma, o this referencia o window, que é escopo global.

&#x20;Arrow functions não se conectam com o this da mesma forma que function declaration.

```javascript
const person = {
    name: "Nicollas",
    getName: () => {
        return this.name
    }
}

console.log(person.getName()) // undefined
```

Fazendo assim, funciona corretamente

```javascript
const person = {
    name: "Nicollas",
    getName: function() {
        return this.name
    }
}

console.log(person.getName())  // Nicollas
```

Evitando o this

```javascript
const person = {
    name: "Nicollas",
    getName: () => person.name
}

 console.log(person.getName()) 
```

* Prototype

Em JavaScript, quando um objeto é criado, ele possui automaticamente uma propriedade que referencia um outro objeto. Esse outro objeto é chamado de prototype. Todo objeto em JavaScript herda propriedades e métodos do seu prototype ascendente. Ou seja, o objeto acessa propriedades e métodos do seu protótipo ascendente. Essas propriedades não pertencem ao objeto em si, mas sim ao prototype do objeto.&#x20;

* Desestruturação de objetos

```javascript
const person = {
    name: 'Nicollas',
    age: 26
}

const { age } = person

console.log(age)  // 26
```

Se quiser renomear, pode-se utilizar:

```javascript
const { age: idade } = person

console.log(idade)  // 26
```

* JSON

```
{
    "nome": Nicollas,
    "idade": 26,
    "hobbies": ["Academia", "Ler"],
    "estudaJavascript": true
}
```

* POO

```javascript
class Person {
    constructor(firstName, lastName, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

const person = new Person('Joao', 'Da silva', 23);
console.log(person); // Person { firstName: 'Joao', lastName: 'Da silva', age: 23 }
```

* super, constructor, extends

```javascript
class Animal {
    constructor(name) {
        this.name = name
    }

    speak() {
        console.log(`${this.name} made some noise`);
    }
}

class Cat extends Animal {
    constructor(name) {
        super(name);
    }
}

const cat = new Cat('Ari');
cat.speak();
```

* static

```javascript
class Person {
    
    static speak() {
        console.log('Hello World!');
    }
}

Person.speak(); // Hello World!
```

* get/set


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nicollaslopes.gitbook.io/estudos/javascript/classes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
