Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of backtick in ember computed properties

The guides show a computed property written with backticks. I am not sure if they are necessary.

Could this:

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

Be rewritten as this:

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

?

For me, that's less obscure. What are the plus / cons of each method?

like image 561
blueFast Avatar asked Dec 31 '15 10:12

blueFast


2 Answers

The back ticks have nothing to do with Ember. They are part of ES6 called template strings. They simply make it easier for string interpolation. You can have any valid js statement in the curly braces and they get evaluated. They also allow multi line strings.

One major point that I am aware of is that template strings get evaluated immediately. So cannot be reused by assigning it to a variable. The variable will only get the evaluated result.

Here is more info about them in MDN.

like image 82
blessanm86 Avatar answered Nov 20 '22 20:11

blessanm86


They are basically the same. In fact, for now, the backtick syntax, or es6 template strings is being transpiled back into the second version in final code.

Some could argue the first form is more logical and, when using simpler variable names, more readable. It also allows code-scanning i18n libs, such as gettext-based ones, to find them easily. I doubt they can make anything useful of that until the backtick syntax is widely supported by browsers though. It's coming. Chrome, Firefox, Safari, Edge support it.

In the end use them if you like, don't if you don't like them. Does not matter.

(formal definition in specs)

like image 23
spectras Avatar answered Nov 20 '22 19:11

spectras