Using backtrack in ember computed properties

Guides show a computed property written with reverse windows. I am not sure that they are necessary.

Can this:

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

It is rewritten as follows:

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

?

For me it is less obscure. What are the pros / cons of each method?

+5
source share
2 answers

Reverse ticks have nothing to do with Amber. They are part of ES6 called pattern strings. They simply simplify string interpolation. You can have any valid js operator in braces and get an estimate. They also allow multiline strings.

One of the main points that I know of is that the template strings are immediately evaluated. Therefore, it cannot be reused by assigning it to a variable. The variable will only get the result.

Read more about them in MDN.

+4
source

They are basically the same. In fact, at the moment, the backtick syntax or lines of the es6 template are passed to the second version in the final code.

Some may argue that the first form is more logical and is easier to read when using simpler variable names. It also allows you to scan i18n libs, such as gettext-based, to find them easily. I doubt they can do anything useful as long as the backtick syntax is not widely supported by browsers. It happens. Chrome, Firefox, Safari, Edge support it.

In the end, use them if you want, do not do them if you do not like them. Never mind.

(formal definition in specification )

+4
source

Source: https://habr.com/ru/post/1239570/


All Articles