While it's quite possible to do it the way you have written and intend, there are lots of reasons to avoid classical inheritance (see Classical Vs prototypal inheritance), and lots of reasons to favor composition, instead. Let's take a look at what this might be with prototypal inheritance:
var base = {
get: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
post: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
put: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
destroy: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
}
};
// Use a factory to instantiate instances:
function baseService(resource) {
var instance = Object.create(base);
instance.apiEndpoint = "/api/" + resource + "/";
return instance;
}
var withAccount = {
getAll: function(searchString) {
// Uses the BaseService method. Lovely!
return this.get(null, {
searchString: searchString || ""
});
}
};
function accountService(resource) {
var instance = mixIn({}, base, withAccount);
instance.apiEndpoint = "/api/" + resource + "/";
return instance;
}
Doing it this way allows for considerably more flexibility down the road. If you use the Stampit library, it becomes even easier:
var base = stampit().methods({
get: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
post: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
put: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
},
destroy: function (resource, data, parameterOverrides) {
return aPromisedLandOfDragons();
}
}).enclose(function () {
this.apiEndpoint = '/api/' + this.resource + '/';
delete this.resource;
});
var withAccount = stampit().methods({
getAll: function(searchString) {
// Uses the BaseService method. Lovely!
return this.get(null, {
searchString: searchString || ""
});
}
});
var accountService = stampit.compose(base, withAccount);
// Watch it work:
var myAccountService = accountService({resource: 'foo'});
// { apiEndpoint: '/api/foo/' }