Jump to content

Passing Scope Correctly?


jjwallace
 Share

Recommended Posts

I think i am doing something wrong with passing scope, can someone point me in the right direction here?

//Game.js

var myGame = function(){

    var fish = [];

    BasicGame.Game.prototype = {
        create: function () { 
        
            //makeFish is being called from another class

            makeFish: function(game, superThis, x, y, fishSize, angle, pSize){
                    fish.push(new Fish(game, superThis, fish.length, x, y, fishSize, angle, pSize));
            }
            
            function eatSound(){
                var biteMe = Math.floor((Math.random() * 3) + 1);
                console.log('fishscope: ' + bite1);

                if(biteMe == 1){bite1.play();}
                if(biteMe == 2){bite2.play();}
                if(biteMe == 3){bite3.play();}
            }
            
        }
    }
}

Other Class

//Fish.js

var Fish = function (game, superThis, index, x, y, size, angle, pSize) {

}

Fish.prototype.die = function(superThis){
    
    superThis.eatSound();
    
}

ERROR: Cannot read property 'eatSound' of undefined

Link to comment
Share on other sites

Why do you call "new" to a function? Just use it Fish(params) with no "new".

And by the way you create die() function to the prototype of a function. Not sure what are you trying to achieve?

And the error says, no property 'eatSound' in superThis. What do you send as argument for superThis in makeFish() function?

What scope, scope of what?

JS ES5 has only function scope and variable hoisting(all variables are stuck at the top of the function.)

And where do you call die() ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...