EN - - BuyEPUB/PDF Search Search Share 1. Tutorial 2. 3. 30th April 2021 # Reference Type In-depth language feature This article covers an advanced topic, to understand certain edge-cases better. It’s not important. Many experienced developers live fine without knowing it. Read on if you want to know how things work under the hood. A dynamically evaluated method call can lose `this`. For instance: Reference type explained Looking closely, we may notice two operations in `obj.method()` statement: 1. First, the dot `'.'` retrieves the property `obj.method`. 2. Then parentheses `()` execute it. So, how does the information about `this` get passed from the first part to the second one? If we put these operations on separate lines, then `this` will be lost for sure: Summary Reference Type is an internal type of the language. Reading a property, such as with dot `.` in `obj.method()` returns not exactly the property value, but a special “reference type” value that stores both the property value and the object it was taken from. That’s for the subsequent method call `()` to get the object and set `this` to it. For all other operations, the reference type automatically becomes the property value (a function in our case). The whole mechanics is hidden from our eyes. It only matters in subtle cases, such as when a method is obtained dynamically from the object, using an expression. ## Tasks ### Syntax check importance: 2 What is the result of this code? let user = { name: "John", go: function() { alert(this.name) } } (user.go)() P.S. There’s a pitfall :) solution **Error**! Try it: Explain the value of "this" importance: 3 In the code below we intend to call `obj.go()` method 4 times in a row. But calls `(1)` and `(2)` works differently from `(3)` and `(4)`. Why? Previous lessonNext lesson Share ## Comments read this before commenting… - If you have suggestions what to improve - please [submit a GitHub issue](https://github.com/javascript-tutorial/en.javascript.info/issues/new) or a pull request instead of commenting. - If you can't understand something in the article – please elaborate. - To insert few words of code, use the `` tag, for several lines – wrap them in `
` tag, for more than 10 lines – use a sandbox ([plnkr](https://plnkr.co/edit/?p=preview), [jsbin](https://jsbin.com), [codepen](http://codepen.io)…)



#### Chapter

- Miscellaneous

#### Lesson navigation

- Reference type explained
- Summary

- Tasks (2)
- Comments

Share



Edit on GitHub

- about the project
- contact us
- terms of usage
- privacy policy