I am just getting my feet wet where JavaScript Prototyping is involved, and I am having some trouble.
I need to create a _LEAVE object from a LEAVE prototype for a system I am working on based on a prototype object. The _LEAVE object has a function named Ready, which should fire when the document is ready. The system already has similar functionality in some of it’s older code, and I am trying to keep it uniform.
Here is the code I am trying, but I keep getting an error:
var LEAVE = function () {
}
$(document).ready(function () {
_LEAVE.Ready();
});
var _LEAVE = function (params) {
this.Ready = function () {
alert ("Leave Ready");
};
}
_LEAVE.prototype = new LEAVE();
Error:
SCRIPT438: Object doesn’t support property or method ‘Ready’
leave.js, line 6 character 5
I’m not sure where I am going wrong, as this seems to be what is happening in other parts of the system. At least, something similar is happening, but I am struggling to wrap my mind around the old code…
Would appreciate any advice anyone could give me! 🙂
I’m not sure if I’ve understood you correctly, but are you attempting to create an instance of a
LEAVEobject? If so,LEAVEneeds to be a constructor function, andReadyshould be a method on theprototypeof that:Now, you can instantiate
LEAVEby calling the constructor with thenewoperator:Methods declared as properties of the
prototypeobject are shared by all instances. So all instances ofLEAVEwill have a.Readymethod available to them, but they will share one copy of the function in memory (the copy that was assigned to the property ofLEAVE.prototype).