简体   繁体   中英

Creating local state for object oriented programming in javascript

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

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM