I created an external .js file with prototype:
function Logger() {
var log = new Array("");
}
Logger.prototype.AddLine = function (value) {
if (value) {
Logger.log.push("\t" + value);
}
}
Logger.prototype.ReadLog = function () {
return this.log.join("");
}
And now I try to use it on my page. I included my file in the header. And I have simple js:
$(document).ready(function () {
var log = new Logger();
log.AddLine("User entered the page");
});
Firebug error:
TypeError: Logger.log is undefined
[Break On This Error]
Logger.log.push(“\t” + value);
Can anyone please explain why this is happening?
To set
logon theLoggerinstance to an empty array, usethis.log = [], notvar log. The latter creates a private variable, which you can’t access outside the function.Also,
Logger.logmeans: access thelogproperty on theLoggerfunction. Here you want the instance instead, so usethis.log(like you did inReadLog).