0

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

6
  • Shouldn't you be doing a.publicMethod1() instead of Class.publicMethod1()? (And stop idolizing some random guy, it doesn't make you look very smart) Commented Oct 17, 2010 at 9:48
  • 1
    Douglas Crockford is NOT some random guy. Is half-god of javascript... Or something like that... Commented Oct 17, 2010 at 9:51
  • @petraszd: Never heard of him. Commented Oct 17, 2010 at 9:52
  • @Matti Virkkunen: Doesn't mean he is not important or well known: en.wikipedia.org/wiki/Douglas_Crockford Commented Oct 17, 2010 at 9:56
  • 1
    @Matti Virkkunen: But it does not hurt to know. And giving credits to the ones who deserve it is a good thing. Anyway... nevermind. Commented Oct 17, 2010 at 10:01

3 Answers 3

2

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.

0
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.

1
  • 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. Commented Oct 17, 2010 at 18:24
0

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!)
    };                                 // }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.