Another function from Backbone.js
escape: function(attr) {
var html;
if (html = this._escapedAttributes[attr]) return html;
...
What’s the advantage of doing it as above ?, not as per below?
var html = this._escapedAttributes[attr];
if( html ) return html;
Whether this is an advantage or confusing, is pretty much up to you, your team and your coding conventions.
Another example where this might be really useful
where you would use it like
The same thing goes for
if statements. Sometimes it can make sense or is helpful to assign a value to some variable within a condition, to directly have that access within the case. Of course this might not be convinient for some folks who is not used to it, but again, if your conventions and your team agrees on stuff like this, it can be pretty neat.Other languages offer this “feature” by default. For instance, a special variable name like
$_or->just automatically refers the thing you took your hands on.