Backbone has totally changed all of that - learnt so much about design patterns in such a short space of time - shout out to Addy Osmani for writing such down-to-earth, approachable content.
Backbone has totally changed all of that - learnt so much about design patterns in such a short space of time - shout out to Addy Osmani for writing such down-to-earth, approachable content.
var Typeahead = ...
Typeahead.prototype = {
constructor: Typeahead
...
rather than the usual function Typeahead() { ...
Typeahead.prototype = { ...
is a mystery to me.I'm kind of ignorant at the moment of proper best practices of js, but eager to learn more, so if you could explain to me why one is better than the other, that'd be rad :)
matcher: function (item) {
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
}
Why there is "~" bitwise operator used in this method?I do agree that it's bad style, because it doesn't clearly communicate the intention. If you want something that returns true or false, you should return true or false.
Basically, it's a smug version of:
return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1;
Writing it like this is a lot clearer. Furthermore it even returns `true` and `false` instead of some truthy/falsy integer.Bitwise NOT on -1 will give you 0 -- for any other number, it'll return a truthy value.
It's the same as:
`return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1`
If you're really dead set on doing things this way, it's safer to prefix with !! to coerce a real boolean:
`return !!~item.toLowerCase().indexOf(this.query.toLowerCase())`