I came across a code snippet that involves defining a constructor like this:
var Resource = function(data) {
angular.extend(this, data);
}
Afterward, a method is defined for it as well.
Resource.query = function(url) {
console.log(url);
}
I'm curious about how this works. I am aware that functions are considered objects too. Is this similar to the code below? And if yes, what happens to the constructor function?
var data = {};
data.query = function(url) {
console.log(url);
}
Additionally, why not simply define it on the prototype instead?
Resource.prototype.query = function(url) {
console.log(url);
}
You can find the code snippet here.