tags:

views:

37

answers:

4

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!

+1  A: 

You are not executing the generateParagraph function, as parenthesis are missing...

paragraph.generateParagraph

This gets the function as a callback...
To call it:

paragraph.generateParagraph();
Macmade
still wont work, method needs to be added to ctor prototype, not ctor.
no
+1  A: 

you're not invoking the function.

paragraph.generateParagraph();
David Hedlund
+3  A: 

Add methods to the constructor's prototype, not the constructor itself.

function Paragraph(className, innerHTML, parentId) {
    this.className = className;
    this.innerHTML = innerHTML;
    this.parentId = parentId;
}

Paragraph.prototype.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;
no
-1, I can't see how this is any different to the code posted in the question. In any case, it's not the correct answer.
Andy E
@Andy E, actually, this answer is entirely correct.
J-P
You are completely right! Thanks for the answer!
Stegeman
@I-llumination, mark it as correct :)
J-P
@J-P: it wasn't, when I downvoted and left my comment. It was edited within the 5 minute editing window to add the parenthesis to `paragraph.generateParagraph()`. The irony is that those edits don't leave an audit trail so I'm unable to remove my downvote until the answer is "properly" edited
Andy E
@Andy, edit it yourself (by adding some white-space or something...) and then you'll be able to reverse the downvote. :)
J-P
` ` @J-P: Done.
Andy E
+2  A: 

change

Paragraph.generateParagraph = function() {   

to

Paragraph.prototype.generateParagraph = function() {   

You added the function as a property of the constructor function, rather than to the prototype. By adding the function to the prototype it will be part of the object created when calling the constructor function using the new keyword.

Also, you need parentheses to the call to generateParagraph, otherwise you get a reference to the function, rather than calling the function:

paragraph.generateParagraph();
Mario Menger