# \[CODE GOLF\] Avoiding use of 'this'

**URL:** <https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434>\
**Category:** Design\
**Created:** [January 15, 2016, 6:16am UTC](https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434 "2016-01-15T06:16:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)\
**Post date:** [January 15, 2016, 6:16am UTC](https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434/1 "2016-01-15T06:16:00Z")

</div>

Watching a presentation by Douglas Crockford he suggested as an exercise to avoid using `this` inside Javascript programs.

[![](https://img.youtube.com/vi/PSGEjv3Tqo0/maxresdefault.jpg "Nordic.js 2014 • Douglas Crockford - The Better Parts") ](https://www.youtube.com/watch?v=PSGEjv3Tqo0)

This got me thinking. How to not use `this` in day to day ember code. I find myself using it all the time, perhaps almost ritualistically.

Would love to see folk’s before and after refactor examples following a no `this` principle.

Start with an example

```auto
Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: Ember.computed('firstName', 'lastName', function() {
    return `${this.get('firstName')} ${this.get('lastName')}`;
  })
});

```

I am not even sure how to remove `this.get` pattern inside that computed property. Thoughts?

---

<div class="post-metadata">

**Author:** ![spectras](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@spectras](https://discuss.emberjs.com/u/spectras)\
**Post date:** [January 15, 2016, 9:27am UTC](https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434/2 "2016-01-15T09:27:56Z")

</div>

I don’t really see that applicable that way. An object model with no use of this would most probably rely on creating closures and dynamically letling methods close over some variable it would use as a replacement to `this`.

```
function createThing(options) {
    const self = {
        msg: options.msg
    };
    self.sayHi = function () {
        window.alert(self.msg);
    };
    return self;
}
const obj = createThing({msg: 'Hello'});
obj.sayHi(); // popup 'Hello'

```

It has some advantages. For instance, you can close over variables directly in `createThing`, effectively making them inaccessible outside of the methods, acting as “private” variables. And some drawbacks too, such as `instanceof` not working and inheritance in general being more complicated (though feasible).

You won’t be doing that in Ember. For starters, even if you did, Ember itself uses `this` and a prototype-based object model, defeating any attempt you will make.

Though interesting, the video you linked is only relevant to a very specific context: you want to load and execute untrusted JS code. And to do that, the solution he used is to bar ads’ untrusted JS code from using `this`. Now, you won’t be loading an Ember application as an ad, will you?

---

<div class="post-metadata">

**Author:** ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)\
**Post date:** [January 15, 2016, 10:16am UTC](https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434/3 "2016-01-15T10:16:25Z")

</div>

Yes, this was why I was asking this question. Thanks for this response.

Certainly not suggesting it would be practical let alone even possible within the Ember system.

---

<div class="post-metadata">

**Author:** ![rtm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rtm/32/10953_2.png) [@rtm](https://discuss.emberjs.com/u/rtm)\
**Post date:** [January 21, 2016, 6:49am UTC](https://discuss.emberjs.com/t/code-golf-avoiding-use-of-this/9434/4 "2016-01-21T06:49:15Z")

</div>

With the Ember.Object approach, you of course cannot avoid the use of `this` and should not even try. Ember uses a sort of OOP approach which is predicated on “classes” and subclassing. Crockford’s discussion of approaches not using `this` is predicated on NOT adopting this kind of “classic” OOP approach.
