Dennington-bear
Dennington-bear

Reputation: 1782

javascript prototype method not defined

Im trying to become more object orientated in my approach to doing things and ive run into an error where the console says a function is undefined. I cant see how because i have other functions that run that have the same makeup.

i set up my object like this in my app.js:

var vid = new videoAPI(whichOs.type());

Then i set up my constructor in my lib.js:

function videoAPI(osParam)
{
    this.os = osParam;
}

And create my methods the one I have issues with is this one in my lib.js:

videoAPI.prototype.print = function()
{
    return this.os;
}

I then call in my app.js

console.log(print()); 

I have two other functions that use this setup but i pass variables in. Can anyone tell me what im doing wrong?

**edit. The line it fails on is is print(); I didnt mention i have this in a node.js project linking two js files together. this is the error:

ReferenceError: print is not defined
    at Object.<anonymous> (C:\Users\denis\Desktop\videoServer\app.js:36:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Upvotes: 0

Views: 921

Answers (2)

Seth
Seth

Reputation: 10454

You need to actually call the method off of the videoAPI instance.

var vid = new videoAPI('something');
console.log(vid.print()) // returns: something

If you're calling other functions without prefixing them with the videoAPI instance variable, then they exist globally.

Upvotes: 1

michael
michael

Reputation: 758

Based on the code provided, print is a prototype on the videoAPI object, but in the console log you're simply calling print. Change:

console.log(print());

To:

console.log(vid.print());

It should fix your problem.

EDIT: Looks like Seth beat me to it :P

Upvotes: 1

Related Questions