爲什麼我的this.pPos
被設置爲包含函數代碼的字符串,而不是函數返回值?Javascript變量設置爲字符串
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);
這將返回一個隨機數,但它返回函數的文本。
因爲你沒有調用這個函數,'asd.pPos()' – elclanrs