Why does my this.pPos is set to a string containing the function code, instead of the function return value?
function game(mode, boardDim) {
//mod
this.mode = mode;
//dim tabla
this.boardDim = boardDim;
//pozitii initiale elemente
if (this.mode == 'easy') {
//creez pozitii specifice
this.pPos = function () {
var pPos = Math.floor(Math.random() * Math.pow(this.boardDim, 2));
return pPos;
};
}
}
var asd = new game('easy');
alert(asd.pPos);
This should return a random number, but it returns the function's text.
asked Jul 13, 2014 at 9:14
George Irimiciuc
4,64110 gold badges49 silver badges89 bronze badges
1 Answer 1
You have to call the function.
alert(asd.pPos());
Alerting the function itself will implicitly call toString() on it.
answered Jul 13, 2014 at 9:15
Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
salezica
+1, it's not text. It's just that Javascript gives you the function's code when you attempt to turn it into a string, like
alert() does.George Irimiciuc
I need to wait 10 minutes.
lang-js
asd.pPos()