Reputation: 6117
I am new in using OOP
methodology in javascript
, I use the following code below as a constructor
.
var post = function post() {}
I call the constructor inside a jQuery
event handler
post = new post();
At first run or on each page refresh, it works well because a method
invocation
after calling the constructor
gets executed. But on clicking the button that triggers the event the second time, it would not execute anymore, I instead get the following error message in firebug
TypeError: post is not a constructor
So why does it not work again after the first run, without a page refresh?
And how will I make it work continuously without page refresh?
Upvotes: 0
Views: 1266
Reputation: 1074694
So why does it not work again after the first run
Because you've overwritten the post
symbol. This line:
post = new post();
Calls your post
constructor and assigns the resulting object to the post
variable. Now post
isn't a constructor function anymore, it's the object created via new post
.
Just use a different name:
var p = new post();
Side note: The overwhelming convention in JavaScript is that constructor functions start with an initial upper case letter, e.g. Post
rather than post
. You're free to ignore that convention, but it may make it difficult for others to read your code (for instance, here on SO).
Upvotes: 5