0

I have declared a variable in my main html page as such:

var ColorBuffer;

I pass this variable into a function:

myFunc("example.txt", ColorBuffer);

Within this function a property of the variable is set as shown:

function myFunc(file, colBuf)
{
    ...

    colBuf.items = 4;
}

However when I refer to 'ColorBuffer.items' within my main html page I get the error 'Cannot read property 'items' of undefined'.

So it seems that using the parameter name colBuf does not affect the argument ColorBuffer passed in...how could I set the items property of ColorBuffer from within my function?

Many thanks.

1
  • You forgot to give your variable a value. Commented Nov 6, 2014 at 21:30

2 Answers 2

1

Setting ColorBuffer as an object will resolve this.

var ColorBuffer = {};
Sign up to request clarification or add additional context in comments.

Comments

0

The variable is undefined because it has no value. What will the variable be, an array? String? Plain Object?

Whatever it may be, give it a basic value:

var ColorBuffer = {}; //For object
var ColorBuffer = 0; //For number
var ColorBuffer = []; //For array
//etc etc

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.