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

# this в JS

В JS this работает только в контексте метода, в контексте объекта this работать не будет (он будет иметь значение undefined или объект window)

```javascript
let user = {
    name: "Gizmo",
    ref: this,
};

// Выведет undefined или Window{...}
console.log(user.ref);
```

Он будет "правильно" работать только из метода...<br>

```javascript
let user = {
    name: "Gizmo",
    ref: function (){
        return this;
    },
};

// Выведет {name: "Gizmo", ref: function}
console.log(user.ref());
```
