0

Are there any special measures needed to pass an object from one function to another? I am relatively new to Object Oriented JavaScript so forgive me if there is an easy fix. Here is some sample code for a problem I am having.

function foo {

    var x = 0;
    var y = 0;
    /* this is the JavaScript Object that is passed in
    cell = {
            left: {x: someInt, y: someInt},
            up: {x: someInt, y: someInt},
            right: {x: someInt, y: someInt},
            down: {x: someInt, y: someInt},
            x: someInt,
            y: someInt
        }
    */    

    this.turn = function(cell){
        console.log(cell);
        processNeighbors(cell);
        smartMove(x,y, cell);

    function smartMove(x,y,cell) {
         // make a smart decision
    }

    function processNeighbors(x, y, cell) {
        console.log(cell); // this is the line of the undefined error
        // process all neighbors
    }
}

I expected that both outputs would be the same, however, the console.log() inside the processNeighbors function returns a valid response and the bar function returns a 'cannot read property "value" of undefined.

So when an object is passed from one function to the next, does it go out of scope? I don't change the object itself in any of functions.

7
  • 2
    This is not a "JSON Object". It's a "JavaScript object". It's worthwhile learning this distinction. JSON is a (usually string-based) format that is used for data interchange. Commented Jul 19, 2015 at 3:26
  • 1
    If objects or any other values "went of of scope" when being passed from one function to another, then the entire web would come to a screeching halt. Commented Jul 19, 2015 at 3:29
  • 1
    "The object that I'm passed is definitely JSON" --- no it's not, it's just an object. Commented Jul 19, 2015 at 3:45
  • 1
    @zerkms and torazaburo Sorry for being dumb. Thank you very much for the help. There's a good SO thread on the difference between the two that I just read. Commented Jul 19, 2015 at 3:51
  • 1
    @SeeDart - no need to apologize! The difference and relation between JSON and a JavaScript object is a very common point of confusion. Thanks for posting the link to that other thread - it should be very helpful for other people who run into the same question. Commented Jul 19, 2015 at 3:56

1 Answer 1

2

Look at your code again:

function processNeighbors(x, y, cell) {
    console.log(cell); // this is the line of the undefined error
    // process all neighbors
}

processNeighbors creates the var cell in the functions scope. (third argument)

So when you call processNeighbors(cell); the x parameter if your function will be cell and the y and cell parameters will be undefined.

Either remove cell from the arguments:

function processNeighbors(x, y) {
    console.log(cell);
}

// or - if that was the intended way to call the function
function processNeighbors(cell) {
    console.log(cell);
}

Or call it with the correct parameters:

processNeighbors(x,y,cell);

I'm not commenting on any of the other errors in your code, because I assume that those are just copy & paste mistakes.

As a really simple example:

var x = 10;
function fn(x) {
    alert(x);
}
function fn2() {
    alert(x);
}
fn(5); // will alert 5
fn(); // will be undefined
fn(x); // will alert 10
fn2(); // will alert 10
Sign up to request clarification or add additional context in comments.

5 Comments

Bingo! Good catch. @SeeDart, this is the answer to your question; be sure to upvote and accept it.
Wow... That was it. Sorry my question ended up being debugging not an actual question.
Don't worry, it's always like that. If I had a dollar for every time I was on the wrong track on a debugging issue, I'd be a rich man by now!
No problem, little mistakes like that often happen. Look at how many people didn't see that problem either. Sometimes we just turn blind for the most obvious issues ;)
Why don't you make a snippet so we can run it?

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.