question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] arrow functions with Ember.computed have this as undefined

See original GitHub issue

Declaring a computed property as

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

it is transpiled into,

define('hello-world/components/a-name', ['exports', 'ember', 'hello-world/templates/components/a-name'], function (exports, Ember, layout) {

    'use strict';

    var _this = undefined;

    exports['default'] = Ember['default'].Component.extend({
        firstName: "sivakumar",

        lastName: "kailasam",

        fullName: Ember['default'].computed("firstName", "lastName", function () {
                      return this.get("firstName") + ' ' +this.get("");
        })

    });

});

_this = undefined seems to be wrong to me, is this a genuine issue or am I using arrow functions in a wrong way?

//cc @stefanpenner

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
gordonkristancommented, Apr 16, 2015

@bartocc Yes, you must use the old-style anonymous function, not the arrow functions. Arrow functions, by definition, are bound to their lexical scope and the scope cannot be changed. If you need to access this in your property, an arrow function simply won’t work.

0reactions
SathishkumarThangavelcommented, Dec 22, 2016

does this problem solved now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Computed property on fat arrow functions - Ember.JS
I'm just wondering why fat arrow functions don't work with computed properties. For instance: export default Ember.
Read more >
Computed Properties And Arrow Functions? Not A Good Idea
As great as it sounds, arrow function expressions cannot be used in all cases. One example would be Ember computed properties.
Read more >
Using arrow functions as observers fails? - ember.js
Arrow functions is a ES6 feature which is transpiled in Ember.js using Babel.js. Arrow functions is a shorter notation that also implicitly bind...
Read more >
Handling common JavaScript problems - MDN Web Docs
Expand the showHeroes scope — you can see from this that the heroes variable is undefined , indicating that accessing the members property...
Read more >
Cannot read property search of undefined this props location ...
Somehow (I guess because of Strict Mode) - the function always gets called twice. ... You get an error that says: 'this is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found