Using 'this' in a templated string for computed property fails?

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

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

ironMan2 = Person.create({
  firstName: '1Tony',
  lastName:  'Stark'
});

ironMan2.get('fullName')
(unknown) Uncaught TypeError: Cannot read property 'indexOf' of undefined
    at e.func (vendor-9da94635b0aa517f1541cc071480ab63.js:12)
    at e.get (vendor-9da94635b0aa517f1541cc071480ab63.js:11)
    at Object.o [as isPath] (vendor-9da94635b0aa517f1541cc071480ab63.js:12)
    at n (vendor-9da94635b0aa517f1541cc071480ab63.js:12)
    at i.Ember.Object.extend.fullName.Ember.computed (<anonymous>:7:20)
    at c.f.get (vendor-9da94635b0aa517f1541cc071480ab63.js:11)
    at Object.n [as get] (vendor-9da94635b0aa517f1541cc071480ab63.js:12)
    at i.get (vendor-9da94635b0aa517f1541cc071480ab63.js:15)
    at <anonymous>:1:10

what am I doing wrong?

found the problem, it’s visibility of this inside arrow functions.

1 Like