It might be a wiki question, but still. Which approach conforms more to the Javascript spirit:
var Report = function(data) {
var that = this;
that.send = function() { ... };
};
var r = new Report(data); // create and validate the data
r.send(); // encode and send the data
or
var sendReport = function(data) {
...
// create, validate, encode and send the data
...
};
The first approach seems to be more OO, the second more functional? In the first it might seem that the functions of Report are more testable (though for example we have only send() at this stage). Also, I like to see object of type Report in debugger when required to examine the state.
The second approach might be more “simple”, but it seems to be less testable.
I am personally with the first approach, but the question is if I try to apply the OO “principles” in the “wrong” domain (please help me to word my question better).
Your question doesn’t actually seem to be about the
newoperator; it’s more about whether you should use OO, or functional programming.In this regard, OO is completely within the scope of JavaScript. You can see from the API (e.g. e.g.) that JavaScript is a very object orientated language.
HTML5 has taken this further and given you better control on your objects, by adding new methods to the
Objectobject.Touching on the use of
new, and whether to avoid it or not, I’d point you to this excellent answer (and tell you to usenew).