Using the baseline has many places where they will initialize the function if it is passed to get the value, for example.
Backbone.Model.extend({ url: function() { return 'myurl.aspx'; } });
This is smart if you need to do some calculations / fulfill some conditions before you know what the url is.
Backbone.Model.extend({ url: function() { if ( this.get('name') ) { return 'service1.aspx'; } else { return 'service2.aspx'; } } });
Your first example sends an anonymous function as the first argument to myFunction , and the second example sends an object as the first argument.
myFunction(function() { return { foo: 'bar' }; });
If you are talking about closure, the main difference is that you can have private variables inside closures:
var setGet = (function() { var v = null; return { get: function() { return v; }, get: function(val) { v=val; }, }; });
In the first example, you cannot access the variable v without using .get / .set on setGet , and in example 2. I can simply change it by setting setGet.v = 'new_val';
source share