Yes
Javascript uses Prototypal inheritance . Thus, essentially objects inherit objects (and all objects)
Here are some links that should help you find the point.
Here is the main module template:
var MODULE = (function (my) { my.anotherMethod = function () {
Then you can do something like this to simulate inheritance:
var MODULE_TWO = (function (old) { var my = {}, key; for (key in old) { if (old.hasOwnProperty(key)) { my[key] = old[key]; } } var super_moduleMethod = old.moduleMethod; my.moduleMethod = function () {
This coding style is a bit getting used to, but I definitely prefer its classical inheritance at this point. If this code doesn't make sense, check out Douglas Crockford's lectures and it should clear up most of this.
appeal to editing: You can create various instances of these objects using the new operator.
OR
I would recommend using this small method that extends the prototype of the object (again, if that doesn't make sense, see the Douglas Crockford video). I forget the exact reasons why it is so highly recommended to them, but at least it eliminates some of the confusion that the new operator is slightly different from classical languages. Needless to say, using only with the new operator is not enough.
What this function does is extend the prototype of the object using the create method. Then he ...
- Defines a function F in the containing namespace.
- Assigns a prototype of function
F to the passed object - returns the newly created object.
(best described by Douglas Crockford himself in a prototype inheritance link)
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } newObject = Object.create(oldObject);
So using your code ...
var a = Object.create(MODULE_TWO), var b = Object.create(MODULE_TWO);