I am relatively new to javascript and I have watched two tutorial videos by Douglas Crockford on the subject. He recommends creating object oriented design in javascript in the following way, through the use of nameless functions and closures:
function Class() {
var privateVar1,
privateVar2;
function privateMethod1() {
}
function privateMethod2() {
}
return {
var publicVar1,
publicVar2;
publicMethod1: function () {
}
publicMethod2: function() {
}
};
}
The trouble here is when I make a new class, such as
var a = Class();
I get errors when I attempt to use the public methods I declared in the object literal I'm returning in the class definition. More specifically, I get the error Class.publicMethod1 is not a function. Can anyone see what's wrong here? I must be missing something here bc surely Douglas Crockford can't be this blatantly incorrect about this.
EDIT: It was late at night when I was posting that code snippet and I made some syntactical errors. Sorry to have wasted your time. This is a snippet of the actual code that I am having trouble with.
return {
//public methods
getNextMoveValues: function(board, player) {
currentBoard = board;
if(isBoardValid()) {
var completeURL = assembleString(board, player);
return queryServer(completeURL);
} else {
console.err("board arg not valid in MoveGenerator::getNextMoveValues");
}
},
toString: function () {
var rtn = "currentPlayer: " + currentPlayer + "\t" + "currentBoard: " +
currentBoard + "\t" + "gameOption:" + gameOption + "\n";
return rtn;
}
};
When I run the code, the only error I get is "moveGen.getNextMoveValues(STARTING_BOARD, true) is undefined" when I execute the following command: console.log(moveGen.getNextMoveValues(STARTING_BOARD, true).response);