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.

Skam
  • 7,298
  • 4
  • 22
  • 31
  • There's nothing wrong with this snippet you provided. I ran `new Foo().bar({value:"hello"});` and it logged `hello` twice. The only way to get your error is if `JSONObject.value` is being unset within `this.bar`. Can you post your entire code, the code that actually will get an error? – Dave Chen Jul 19 '15 at 03:16
  • 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. –  Jul 19 '15 at 03: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. –  Jul 19 '15 at 03:29
  • JSON - JavaScript Object Notation. It's a way of representing objects in text (for storage/transmission) – brettwhiteman Jul 19 '15 at 03:38
  • 1
    "The object that I'm passed is definitely JSON" --- no it's not, it's just an object. – zerkms Jul 19 '15 at 03:45
  • 1
    @zerkms and torazaburo Sorry for being dumb. **Thank you very much** for the help. There's a good [SO thread](http://stackoverflow.com/questions/8294088/javascript-object-vs-json) on the difference between the two that I just read. – Skam Jul 19 '15 at 03: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. – Michael Geary Jul 19 '15 at 03:56

1 Answers1

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
Sebastian Nette
  • 7,364
  • 2
  • 17
  • 17