0

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
7
  • 2
    Please show the actual line of code that triggers the error. Commented Jan 22, 2016 at 20:26
  • Copying your code into the console and then calling var vid = new videoAPI('A'); works with no errors. Commented Jan 22, 2016 at 20:27
  • 2
    seems you need console.log(vid.print()); Commented Jan 22, 2016 at 20:32
  • Hmmm that works @Grundy but i have two other functions that dont need the prefix 'vid.' is there any reason why this needs it? Commented Jan 22, 2016 at 20:34
  • 2
    those two other functions probably exist globally off the window object. Commented Jan 22, 2016 at 20:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.