I’m trying to build some objects to make my life a bit easier, but somehow I can’t get it working. I got the following code:
function Paragraph(className, innerHTML, parentId) {
this.className = className;
this.innerHTML = innerHTML;
this.parentId = parentId;
}
Paragraph.generateParagraph = function() {
console.debug(this.parentId); // Expect 'testDiv'
alert(this.parentId); // Expect 'testDiv'
};
function initialize() {
var paragraph = new Paragraph('testClass', 'testTitle', 'testDiv');
paragraph.generateParagraph;
}
window.onload = initialize;
When I try to execute this code nothing happens. I expect the console.debug and alert in the generateParagraph method to be executed.
Any help would be appreciated!
Add methods to the constructor’s prototype, not the constructor itself.