I recently started using prototyping for a project. It is one of my first times using prototyping, so still finding my legs 🙂 I come from an object oriented background, so excuse me if some of my terminology is not correct.
One of my main reasons for using it is to create a scoping framework for the project, to avoid any conflicts later on. I also use it to allow me to create objects spread over several files.
I am however having trouble with something.
I declare my base “class” in one file. In another file, I then declare an extension to the class. In this extension I declare a function, which I then try to call from the base class. Lastly I declare an instance of the extension inside the base class to allow me to call the extended functions from the base class.
When I try and create the instance, however, I get the following error:
SCRIPT445: Object doesn’t support this action leave.js, line 2
character 5
Here is a snippet of my code:
leave.js
var _LEAVE = function () {
this.WORK_LIST = new this._WORK_LIST();
}
worklist.js
_LEAVE.prototype._WORK_LIST = function (params) {
var Render = function(){
...
}
}
Any suggestions about what I am doing wrong and how to fix it would be greatly appreciated.
Alright, firstly if you don’t understand prototype-based inheritance then I suggest that you learn about it. It’s really simple actually.
Secondly, and please don’t take this in the wrong way – your code abhors me. I understand that you’ve come from a classical background and I respect the effort you’re making. However, truly speaking I wouldn’t want to read your code even if I was paid to.
This is what I would have done (correct me if my program isn’t what you’re looking for, but I really don’t know what’s happening in your code):
In addition, since you’re from a classical background it may help you to write code that’s more in line with the classical style of inheritance. Read this answer. It will help you a lot.
Edit: Using my script you can create classes in JavaScript as follows.
baseClass.js:extension.js:You may tinker with the fiddle.