I have multiple objects in a hierarchy which have common properties and methods inherited from the superclass, and object-specific properties and methods in the subclasses. I’m still new to OOP javascript, so there is probably a much better approach to this. I’m using jQuery for the AJAX, but not sure if that makes any difference.
function Obj(input) {
this.in = input;
this.out = {
content: this.in,
category: {},
owner: utils.getValidUser(),
state: 0,
meta: {}
};
this.process = function() {
console.log("No Process Defined");
}
}
function MovieObj(input) {
this.inheritFrom = Obj;
this.inheritFrom();
this.out.type = "movie";
}
function ActionMovie(input) {
this.inheritFrom = MovieObj;
this.inheritFrom();
this.out.genre = "action";
this.process = function() {
console.log("movie search");
$.getJSON("/api/search/"+ escape(this.out.content),
function(data) {
/*
I want to modify the object properties according to
what comes back from the ajax call.
*/
});
}
}
Here is a prototypical approach to my earlier code, AND a simple reference to the calling object, which solves both the inheritance issues and the scope issues.
This is now actually working, but there are some caveats. As written, the constructors for the superclass are called everytime a new object is defined, so a lot of unnecessary calls are made.
This code is based on information contained in the following link, which also describes a workaround that is Mozilla specific:
http://www.spheredev.org/wiki/Prototypes_in_JavaScript