Reputation: 33
I have been racking my brain on this one for hours now and I have looked at probably 30 online tutorials by now. As far as I can tell, I am not doing anything wrong, but yet I am having problems. I have some test code:
TestPulse = function() {};
TestPulse.prototype.go = function() { alert('You just pulsed'); };
TestPulse.go();
I also tried:
function TestPulse() {};
TestPulse.prototype.go = function() { alert('You just pulsed'); };
TestPulse.go();
Finally fed up, I just ripped some code from a few prototyping and namespace tutorials around the web, and no matter what I do, I get the following error:
Uncaught TypeError: Object function TestPulse(){} has no method 'go'
Like I said, I am not sure I am doing anything wrong...so what exactly is going on here? When I debug, I do see a prototype object attached to the function, with the constructor and all, so I know its there. Where is the issue? Am I not understanding how prototyping works?
Upvotes: 3
Views: 109
Reputation: 5813
TestPulse is your (let's say) class. You need to create an instance from it.
var myObject = new TestPulse();
myObject.go();
That should work.
Upvotes: 1
Reputation: 41715
You don't have an instance of TestPulse...
TestPulse = function() {};
TestPulse.prototype.go = function() { alert('You just pulsed'); };
new TestPulse().go();
Upvotes: 6
Reputation: 35793
You need to create an instance of your TestPulse object to access the prototype methods on it.
TestPulse = function() {};
TestPulse.prototype.go = function() { alert('You just pulsed'); };
var testPulse = new TestPulse();
testPulse.go();
Upvotes: 5