0

I recently encountered an problem where I am not creating an array correctly for objects.

So I tried to do the following thing in javascript:

function Mesh()
{
    this.buffer = [10, 10, 10];
}

Mesh.prototype.getBuffer = function ()
{
    return this.buffer;
};

function main()
{
    var model = new Mesh();
    var modelBuffer = model.getBuffer;
    document.getElementById("textSection").innerHTML = modelBuffer[0];
}

I'm trying to avoid using global variable hence I've created the array the way I did in my Mesh() constructor. But when I try to retrieve the data from slot 0 it prints "undefined". How do I get this work? I really have no idea why this is happening...

1 Answer 1

3

Try:

var modelBuffer = model.getBuffer();

Instead of:

var modelBuffer = model.getBuffer;

You were assigning the function to the variable, instead of calling the function.

It's always a good idea to console.log() variables that don't return what you expect them to. In this case:

console.log(modelBuffer);

Logged:

// function ()
// {
//     return this.buffer;
// }

Pointing me into the direction of the modelBuffer assignment, for example.

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

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.