Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions content/questions/functions-and-this/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: functions, constructors and `this`
tags:
- function
- this
- constructor
order: 72
date: Thu Oct 31 2019 23:44:19 GMT-0600 (Central Standard Time)
answers:
- 'undefined // correct'
- 'Thinker Bell'
- '{ play: "Thinker Bell", sound: "la" }'
- NaN
---


Let's say you have a fancy defined constructor function `HellBells`.

```javascript
function HellBells(song) {
const sound = ‘la’
this = {
play: song,
sound,
};
}

```

And then it comes octocat and tries to use the function as follows.
```javascript
console.log(HellBells(‘Thinker Bell’));

```

What is the output of that `console.log`?

<!-- explanation -->

This function has a little thing, that it has no `return`, which doesn't metter that much if you use it as a constructor ussing `new`, but unfortunatly ocotcat didn't do that.

In order to have an output different of `undefined` he should do:

```javascript
console.log(new HellBells(‘Thinker Bell’));
// {
// play: 'Thinker Bell',
// sound: 'la'
// }
```