Using KineticJS I figured out how to pass a function as the Gameboard function but all the this’s inside the Gameboard function think’s its now the object that got the function :(.
function Gameboard(){
//.... creates placeholders for stage layers and all objects
this.dice_layer=new Kinetic.Layer();
this.rolldice=function(){
alert(this.toString());
//..alter images
this.dice_layer.draw();//<~~ thinks this is circle once passed through setUpGameBoard says dice_layer is undefined. alert(this.toString()); shows this to be circle.
};
this.setUpGameBoard=function(){
// ...draws board pawns creates a circle object
var obj=this;//<~~ are there memory issues with this? Is there a better way?
circle.on("click",**obj**.rolldice.**bind**(obj);//** == ANSWER!!!!
};
};
The problem is this line:
You are declaring
doSomethingas a function with a single parameter,fnctionbut when you call it, you are passing two – a string and a function.will behave as you expect it.
jsFiddle demo
Based on your “solution” to the second problem, it looks like you want to use ES5’s
bind. It allows to you specify thethisfor a particular function call since JavaScript really doesn’t have “methods”, you have to specify the object they operate on.An example of malfunctioning code can be compared to the fix with bind.