views:

37

answers:

3

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);

A: 
function Class(arg1, arg2) {
  var privateVar1 = arg1,
      privateVar2;

  function privateFunction1() {
    // use arg2
  }

  function privateFunction2() { 
  }

  return {
    publicVar1: 'something',
    publicVar2: 'something',
    publicMethod1: function () { },
    publicMethod2: function() { }
  }
}

The point is that privateVar* and privateFunctions* are not members of just-returned object. They are variables and/or functions (closures) that can be accessed only by returned object. Only returned object can access scope where privateVar* and privateFunctions* are defined.

petraszd
Thanks for your help. The error does not stem from any private functions but public functions. More specifically, the evaluator is complaining about one of my public functions being undefined.
Sam
A: 

You need to understand the object literal notation.

function Class() {

    var privateVar1, privateVar2;

    function privateMethod1() {}
    function privateMethod2() {}

    return {                           // {
        publicVar1: "value",           // name: value,
        publicVar2: 5,                 // name: value,
        publicMethod1: function () {}, // name: value,
        publicMethod2: function () {}  // name: value (no comma at the end!)
    };                                 // }
}
galambalazs
+2  A: 

Your syntax is invalid:

 return {
  var publicVar1,
   publicVar2;

  publicMethod1: function () {
  }

  publicMethod2: function() {
  }
 }; 

should be

 return {
  publicVar1,
  publicVar2,  
  publicMethod1: function () {},
  publicMethod2: function() {}
 }; 

You are using an object literal here, which always has the form:

{member: value, member: value}

where value can also be omitted: {member, member: value}


Read more about object initializers.

Felix Kling