BusyRich
BusyRich

Reputation: 33

Function does not have my method available

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

Answers (4)

Emir Akaydın
Emir Akaydın

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

lostyzd
lostyzd

Reputation: 4523

Try

var a = new TestPulse;
a.go();

or

TestPulse.prototype.go();

Upvotes: 2

canon
canon

Reputation: 41715

You don't have an instance of TestPulse...

TestPulse = function() {};
TestPulse.prototype.go = function() { alert('You just pulsed'); };
new TestPulse().go();

http://jsfiddle.net/HYWPk/

Upvotes: 6

Richard Dalton
Richard Dalton

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();

http://jsfiddle.net/H2dnv/

Upvotes: 5

Related Questions